From d346f0672266ebb7c8476a3cc3b3a75ac160d86a Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Thu, 22 Apr 2004 20:42:37 +0000 Subject: [PATCH] unnamed enum member variables - less verbose code generation that has a single solution for all compilers. Will only fail silently (no printfs like before) when using compiler switches that force enums to be different size to integers. If anyone ever does this, I will eat my hat git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5883 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Lib/swig.swg | 1 - SWIG/Lib/unnamed.swg | 52 ---------------------------------------- SWIG/Source/Swig/cwrap.c | 12 +++++----- 3 files changed, 6 insertions(+), 59 deletions(-) delete mode 100644 SWIG/Lib/unnamed.swg diff --git a/SWIG/Lib/swig.swg b/SWIG/Lib/swig.swg index eb2b6bc3e..3ec865450 100644 --- a/SWIG/Lib/swig.swg +++ b/SWIG/Lib/swig.swg @@ -402,4 +402,3 @@ static int NAME(TYPE x) { %} %enddef -%include unnamed.swg diff --git a/SWIG/Lib/unnamed.swg b/SWIG/Lib/unnamed.swg deleted file mode 100644 index 0f327b911..000000000 --- a/SWIG/Lib/unnamed.swg +++ /dev/null @@ -1,52 +0,0 @@ -/* - 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 -// safe assigment, for enums it should work. -#define swig_assign_unnamed(eval, val) \ - if(sizeof(eval) == sizeof(val)) \ - memcpy(&(eval), &(val), sizeof(eval)); \ - else \ - printf("unnamed member assigment not supported\n") -#else -// much much safer choice :) -#define swig_assign_unnamed(eval, val) \ - printf("unnamed member assigment not supported\n") -#endif -#else /*** we have a 'good' compiler ***/ -// This is much safer, but some old compiler could not like it -template -inline -void swig_assign_unnamed(T& eval, const V& val) -{ - // it 'should' be: eval = static_cast(val); - // but just in case we found an old compiler - eval = (T)val; -} -#endif -#else -#define swig_assign_unnamed(eval, val) eval = val -#endif -%} diff --git a/SWIG/Source/Swig/cwrap.c b/SWIG/Source/Swig/cwrap.c index f9a4cfd0d..3c9a4e05a 100644 --- a/SWIG/Source/Swig/cwrap.c +++ b/SWIG/Source/Swig/cwrap.c @@ -409,10 +409,10 @@ Swig_cppconstructor_director_call(String_or_char *name, ParmList *parms) { /* ----------------------------------------------------------------------------- * Swig_cattr_search() * - * This function search for the class attribute 'attr' in the class + * This function searches for the class attribute 'attr' in the class * 'n' or recursively in its bases. * - * if you define SWIG_FAST_REC_SEARCH, the method will set the found + * If you define SWIG_FAST_REC_SEARCH, the method will set the found * 'attr' in io the target class 'n'. If not, the method will set the * 'noattr' one. This prevents of having to navigate the entire * hierarchy tree everytime, so, it is an O(1) method... or something @@ -423,7 +423,7 @@ Swig_cppconstructor_director_call(String_or_char *name, ParmList *parms) { * while searching. This could be slower for large projects with very * large hierarchy trees... or maybe not. But it will be cleaner. * - * Maybe latter a swig option can be added to switch at runtime. + * Maybe later a swig option can be added to switch at runtime. * * ----------------------------------------------------------------------------- */ @@ -556,12 +556,12 @@ Swig_cmemberset_call(String_or_char *name, SwigType *type, String_or_char *self) else self = NewString(self); Replaceall(self,"this",Swig_cparm_name(0,0)); if (SwigType_type(type) != T_ARRAY) { - if (!Strstr(type,"$unnamed")) { + if (!Strstr(type,"enum $unnamed")) { Printf(func,"if (%s) %s%s = %s",Swig_cparm_name(0,0), self,name, Swig_wrapped_var_deref(type, Swig_cparm_name(0,1))); } else { - Printf(func,"if (%s) swig_assign_unnamed(%s%s, %s)",Swig_cparm_name(0,0), self,name, - Swig_cparm_name(0,1)); + Printf(func,"if (%s && sizeof(int) == sizeof(%s%s)) *(int*)(void*)&(%s%s) = %s", + Swig_cparm_name(0,0), self, name, self, name, Swig_cparm_name(0,1)); } } Delete(self);