more safer memcpy use

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5813 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2004-03-31 11:57:02 +00:00
commit 50f01cbe95

View file

@ -24,22 +24,25 @@
// //
// //
#ifdef SWIG_UNNAMED_USE_MEMCPY #ifdef SWIG_UNNAMED_USE_MEMCPY
// "dangerous" assigment, but for enums it should work. // safe assigment, for enums it should work.
// it is dangerous because you could get a bad value, #define swig_assign_unnamed(eval, val) \
// but it is safer than "*(T*)(void*)&x = t;", which if(sizeof(eval) == sizeof(val)) \
// could produce a seg. fault. memcpy(&(eval), &(val), sizeof(eval)); \
#define swig_assign_unnamed(eval, val) memcpy(&(eval),&(val), sizeof(eval)) else \
printf("unnamed member assigment not supported\n")
#else #else
// much much safer choice :) // much much safer choice :)
#define swig_assign_unnamed(eval, val) printf("unnamed member assigment not supported\n") #define swig_assign_unnamed(eval, val) \
printf("unnamed member assigment not supported\n")
#endif #endif
#else #else /*** we have a 'good' compiler ***/
// This is much safer, but some old compiler could not like it
template <class T, class V> template <class T, class V>
inline inline
void swig_assign_unnamed(T& eval, const V& val) void swig_assign_unnamed(T& eval, const V& val)
{ {
// it 'should' be: eval = static_cast<T>(val); // it 'should' be: eval = static_cast<T>(val);
// but just in case we found a very very old compiler // but just in case we found an old compiler
eval = (T)val; eval = (T)val;
} }
#endif #endif