diff --git a/Examples/test-suite/python/cpp_enum_runme.py b/Examples/test-suite/python/cpp_enum_runme.py new file mode 100644 index 000000000..ae8b35bba --- /dev/null +++ b/Examples/test-suite/python/cpp_enum_runme.py @@ -0,0 +1,14 @@ +import cpp_enum + +f = cpp_enum.Foo() + +f.hola = f.Hi +if f.hola != f.Hi: + print f.hola + raise RuntimeError + +f.hola = f.Hello + +if f.hola != f.Hello: + print f.hola + raise RuntimeError diff --git a/Lib/swig.swg b/Lib/swig.swg index e376cbf07..eb2b6bc3e 100644 --- a/Lib/swig.swg +++ b/Lib/swig.swg @@ -402,37 +402,4 @@ static int NAME(TYPE x) { %} %enddef - -/* - Auxiliary methods for assigning unnamed type C++ variables, where - swig doesn't know the casting type, but C++ can derived it: - - struct Foo { enum {Hi, Hello } hola; }; - - Activate the following for compilers that do not support unnamed - template parameters (under your own risk). - - #define SWIG_UNNAMED_USE_MEMCPY -*/ -%insert("runtime") %{ -#ifdef __cplusplus -/* here, the C++ compilers that do not accept unnamed template parameters */ -#if defined(_SGI_COMPILER_VERSION) -#ifdef SWIG_UNNAMED_USE_MEMCPY -/* dangerous assigment, but for enums could work */ -#define swig_assign_unnamed(eval, val) memcpy(&(eval),&(val), sizeof(eval)) -#else -#define swig_assign_unnamed(eval, val) printf("unnamed member assigment not sopperted") -#endif -#else -template -inline -void swig_assign_unnamed(T& eval, const V& val) -{ - eval = static_cast(val); -} -#endif -#else -#define swig_assign_unnamed(eval, val) eval = val -#endif -%} +%include unnamed.swg diff --git a/Lib/unnamed.swg b/Lib/unnamed.swg new file mode 100644 index 000000000..71dfd9d40 --- /dev/null +++ b/Lib/unnamed.swg @@ -0,0 +1,42 @@ +/* + Auxiliary methods for assigning unnamed type C++ variables, where + swig doesn't know the casting type, but C++ can derived it: + + struct Foo { enum {Hi, Hello } hola; }; + + These are very specific macros, that could keep growing as more + compilers require especial support, or could vanish latter if the + enum mapping changes drastically. Therefore, better we put them in + this separate file, so swig.swg doesn't change every time we update + something here. + +*/ + +%insert("runtime") %{ +#ifdef __cplusplus +// here, the C++ compilers that do not accept unnamed template parameters +#if defined(_SGI_COMPILER_VERSION) +// +// Activate the following macro for compilers that do not support +// unnamed template parameters, but where memcpy works. +// +//#define SWIG_UNNAMED_USE_MEMCPY +// +#ifdef SWIG_UNNAMED_USE_MEMCPY +// dangerous assigment, but for enums it should work. +#define swig_assign_unnamed(eval, val) memcpy(&(eval),&(val), sizeof(eval)) +#else +#define swig_assign_unnamed(eval, val) printf("unnamed member assigment not supported\n") +#endif +#else +template +inline +void swig_assign_unnamed(T& eval, const V& val) +{ + eval = static_cast(val); +} +#endif +#else +#define swig_assign_unnamed(eval, val) eval = val +#endif +%}