Original Post
I was running the test framework on MinGW and came upon a portability problem with the offsetof() macro in stddef.h. The following code works on MSVC but not on MinGW: The error I get is:
//
// This test was designed to test the asOBJ_IS_COMPLEX flag
//
// Author: Andreas Jönsson
//
#include "angelscript.h"
#include <stdio.h>
#include <stddef.h> // offsetof(class, property)
#define TESTNAME "TestComplex"
// A complex class is a class that has a defined constructor or
// destructor, or an overridden assignment operator. Compilers
// normally treat these classes differently in that they are
// returned by reference even though they are small enough to
// fit in registers. This is because of the need of exception
// handling in case something goes wrong.
class CComplexClass
{
public:
CComplexClass() { a = 0xDEADC0DE; }
unsigned long a;
};
static CComplexClass cfunction()
{
CComplexClass c;
return c;
}
static CComplexClass c;
class COutStream : public asIOutputStream
{
public:
void AS_CALL Write(const char *text) {printf(text);}
};
bool TestComplex()
{
bool ret = false;
asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
// If asOBJ_IS_COMPLEX is not specified AngelScript will try to determine the
// type of object by looking at what behaviours are registered for the object.
// Since we are not registering any behaviours this time AngelScript would fail.
engine->RegisterObjectType("complex", sizeof(CComplexClass), asOBJ_IS_COMPLEX);
engine->RegisterObjectProperty("complex", "bits a", offsetof(CComplexClass, a));
engine->RegisterGlobalProperty("complex c", &c);
engine->RegisterGlobalFunction("complex cfunction()", asFUNCTION(cfunction), asCALL_CDECL);
c.a = 0;
COutStream out;
engine->ExecuteString(0, "c = cfunction();", &out, 100, 0);
if( c.a != 0xDEADC0DE )
{
printf("%s: Failed to assign complex object returned from function\n", TESTNAME);
ret = true;
}
engine->Release();
return ret;
}
g++ -o obj/testcomplex.o -c ../../source/testcomplex.cpp
../../source/testcomplex.cpp: In function `bool TestComplex()':
../../source/testcomplex.cpp:56: warning: invalid offsetof from non-POD type `
class CComplexClass'; use pointer to member instead
Have anyone else come upon this problem? What did you do to circumvent it? I'd prefer not to define my own macro, but if that is necessary then I'll do it. Regards, Andreas