From 7fa5b92992f9426ad43cb47a686b51d8d9218b51 Mon Sep 17 00:00:00 2001 From: Julien Malik <julien.malik@c-s.fr> Date: Sat, 4 Jun 2011 00:35:08 +0200 Subject: [PATCH] ENH: put swig macros in specific files --- Code/Wrappers/SWIG/CMakeLists.txt | 1 + Code/Wrappers/SWIG/RefCountMacro.i | 57 +++++++++++++ Code/Wrappers/SWIG/core.i | 125 +++++------------------------ Code/Wrappers/SWIG/itkMacro.i | 29 +++++++ 4 files changed, 107 insertions(+), 105 deletions(-) create mode 100644 Code/Wrappers/SWIG/RefCountMacro.i create mode 100644 Code/Wrappers/SWIG/itkMacro.i diff --git a/Code/Wrappers/SWIG/CMakeLists.txt b/Code/Wrappers/SWIG/CMakeLists.txt index af59e8d8da..e7784e49ea 100644 --- a/Code/Wrappers/SWIG/CMakeLists.txt +++ b/Code/Wrappers/SWIG/CMakeLists.txt @@ -16,5 +16,6 @@ FOREACH(swigfile ${swigfiles}) ENDFOREACH(swigfile ${swigfiles}) +# TODO: add build dependency with the macro files SWIG_ADD_MODULE(core python core.i) SWIG_LINK_LIBRARIES(core ${PYTHON_LIBRARIES} OTBWrapperCore) diff --git a/Code/Wrappers/SWIG/RefCountMacro.i b/Code/Wrappers/SWIG/RefCountMacro.i new file mode 100644 index 0000000000..136d3c1bb2 --- /dev/null +++ b/Code/Wrappers/SWIG/RefCountMacro.i @@ -0,0 +1,57 @@ + +%include <exception.i> +%include <typemaps.i> + +// This macro replaces the use of itk::SmartPointer. +// class_name is class name without namespace qualifiers. +// Reference: http://www.nabble.com/attachment/16653644/0/SwigRefCount.i +%define DECLARE_REF_COUNT_CLASS(class_name) + + // pointers and references + %typemap(out) class_name *, class_name & { + // always tell SWIG_NewPointerObj we're the owner + $result = SWIG_NewPointerObj((void *) $1, $1_descriptor, 1); + if ($1) { + $1->Register(); + } + } + + // transform smart pointers in raw pointers + %typemap(out) class_name##_Pointer { + // get the raw pointer from the smart pointer + class_name * ptr = $1; + // always tell SWIG_NewPointerObj we're the owner + $result = SWIG_NewPointerObj((void *) ptr, $descriptor(class_name *), 1); + // register the object, it it exists + if (ptr) { + ptr->Register(); + } + } + + // transform smart pointers in raw pointers + %typemap(out) class_name##_Pointer & { + // get the raw pointer from the smart pointer + class_name * ptr = *$1; + // always tell SWIG_NewPointerObj we're the owner + $result = SWIG_NewPointerObj((void *) ptr, $descriptor(class_name *), 1); + // register the object, it it exists + if (ptr) { + ptr->Register(); + } + } + + // make "deletion" in scripting language just decrement ref. count + %extend class_name { + public: + ~class_name() {self->UnRegister();}; + } + + %ignore class_name::~class_name; + + %ignore class_name##_Pointer; + + %pythoncode { + def class_name##_New(): + return class_name.New() + } +%enddef diff --git a/Code/Wrappers/SWIG/core.i b/Code/Wrappers/SWIG/core.i index 4ebdbca009..faf17cc275 100644 --- a/Code/Wrappers/SWIG/core.i +++ b/Code/Wrappers/SWIG/core.i @@ -1,62 +1,5 @@ - -%include <exception.i> -%include <typemaps.i> - - -// This macro replaces the use of itk::SmartPointer. -// class_name is class name without namespace qualifiers. -// Reference: http://www.nabble.com/attachment/16653644/0/SwigRefCount.i -%define DECLARE_REF_COUNT_CLASS(class_name) - - // pointers and references - %typemap(out) class_name *, class_name & { - // always tell SWIG_NewPointerObj we're the owner - $result = SWIG_NewPointerObj((void *) $1, $1_descriptor, 1); - if ($1) { - $1->Register(); - } - } - - // transform smart pointers in raw pointers - %typemap(out) class_name##_Pointer { - // get the raw pointer from the smart pointer - class_name * ptr = $1; - // always tell SWIG_NewPointerObj we're the owner - $result = SWIG_NewPointerObj((void *) ptr, $descriptor(class_name *), 1); - // register the object, it it exists - if (ptr) { - ptr->Register(); - } - } - - // transform smart pointers in raw pointers - %typemap(out) class_name##_Pointer & { - // get the raw pointer from the smart pointer - class_name * ptr = *$1; - // always tell SWIG_NewPointerObj we're the owner - $result = SWIG_NewPointerObj((void *) ptr, $descriptor(class_name *), 1); - // register the object, it it exists - if (ptr) { - ptr->Register(); - } - } - - // make "deletion" in scripting language just decrement ref. count - %extend class_name { - public: - ~class_name() {self->UnRegister();}; - } - - %ignore class_name::~class_name; - - %ignore class_name##_Pointer; - - %pythoncode { - def class_name##_New(): - return class_name.New() - } -%enddef - +%include "itkMacro.i" +%include "RefCountMacro.i" %module core %{ @@ -88,9 +31,7 @@ enum DefaultValueMode */ ABSOLUTE }; - - - + } } @@ -104,62 +45,36 @@ class Parameter : public itkObject { public: - /** Standard class typedef */ typedef Parameter Self; typedef itkObject Superclass; typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<const Self> ConstPointer; - /** Defining ::New() static method */ static Parameter_Pointer New(void); - /** Set the parameter name */ - void SetName (const char*); + itkSetStringMacro(Name); + itkGetStringMacro(Name); - /** Get the parameter name */ - const char* GetName() const; + itkSetStringMacro(Description); + itkGetStringMacro(Description); - /** Set the parameter description */ - void SetDescription (const char*); + itkSetStringMacro(Key); + itkGetStringMacro(Key); - /** Get the parameter description */ - const char* GetDescription() const; + itkSetMacro(Mandatory,bool); + itkGetMacro(Mandatory,bool); + itkBooleanMacro(Mandatory); - /** Set the parameter key */ - void SetKey (const char*); + itkSetEnumMacro(DefaultValueMode, otb::Wrapper::DefaultValueMode); + itkGetEnumMacro(DefaultValueMode, otb::Wrapper::DefaultValueMode); - /** Get the parameter key */ - const char* GetKey() const; - - /** Set the parameter mandatory flag */ - void SetMandatory (bool); - - /** Get the parameter mandatory flag */ - bool GetMandatory(); - - /** Toogle the parameter mandatory flag */ - void MandatoryOn (); - void MandatoryOff (); - - /** Set the default value mode */ - virtual void SetDefaultValueMode (const otb::Wrapper::DefaultValueMode _arg); - - /** Get the default value mode */ - otb::Wrapper::DefaultValueMode GetDefaultValueMode() const; - - /** Reset to the the default value. Default implementation does - * nothing - */ virtual void Reset(); - protected: - +protected: Parameter(); - - /** Destructor */ //~Parameter(); - private: +private: Parameter(const Parameter &); void operator =(const Parameter&); @@ -179,10 +94,10 @@ class Parameter_Pointer Parameter * GetPointer() const; bool operator<(Parameter_Pointer const & r) const; bool operator>(Parameter_Pointer const & r) const; -// bool operator<=(Parameter_Pointer const & r) const; -// bool operator>=(Parameter_Pointer const & r) const; -// Parameter_Pointer & operator=(Parameter_Pointer const & r); -// Parameter_Pointer & operator=(Parameter * r); + bool operator<=(Parameter_Pointer const & r) const; + bool operator>=(Parameter_Pointer const & r) const; + Parameter_Pointer & operator=(Parameter_Pointer const & r); + Parameter_Pointer & operator=(Parameter * r); Parameter * Print(std::ostream & os) const; private: void Register(); diff --git a/Code/Wrappers/SWIG/itkMacro.i b/Code/Wrappers/SWIG/itkMacro.i new file mode 100644 index 0000000000..8c52508486 --- /dev/null +++ b/Code/Wrappers/SWIG/itkMacro.i @@ -0,0 +1,29 @@ + +%define itkSetStringMacro(name) + void Set##name (const char* _arg); +%enddef + +%define itkGetStringMacro(name) + const char* Get##name () const; +%enddef + +%define itkSetMacro(name, type) + void Set##name (type _arg); +%enddef + +%define itkGetMacro(name, type) + type Get##name (); +%enddef + +%define itkBooleanMacro(name) + void name##On (); + void name##Off (); +%enddef + +%define itkSetEnumMacro(name, type) + void Set##name (const type _arg); +%enddef + +%define itkGetEnumMacro(name, type) + type Get##name () const; +%enddef -- GitLab