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
This commit is contained in:
William S Fulton 2004-04-22 20:42:37 +00:00
commit d346f06722
3 changed files with 6 additions and 59 deletions

View file

@ -402,4 +402,3 @@ static int NAME(TYPE x) {
%}
%enddef
%include unnamed.swg

View file

@ -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 <class T, class V>
inline
void swig_assign_unnamed(T& eval, const V& val)
{
// it 'should' be: eval = static_cast<T>(val);
// but just in case we found an old compiler
eval = (T)val;
}
#endif
#else
#define swig_assign_unnamed(eval, val) eval = val
#endif
%}

View file

@ -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);