Advertisement

Registering struct's properties

Started by May 31, 2009 04:19 PM
1 comment, last by Wavesonics 15 years, 6 months ago
I'm trying to register this struct with AS:

struct Vector2d {
    float x;
    float y;

    Vector2d();
    Vector2d( const Vector2d& v );
    Vector2d( float argX, float argY );

    float length() const;
    Vector2d normalize() const;
    float distanceTo( const Vector2d& v ) const;
    float dot( const Vector2d& v ) const;
};

And here is how I am trying to do the registration:

    // Register our Vector
    r = m_engine->RegisterObjectType("Vector2d", sizeof(Vector2d), asOBJ_VALUE | asOBJ_APP_CLASS | asOBJ_APP_CLASS_CONSTRUCTOR | asOBJ_APP_CLASS_DESTRUCTOR ); assert( r >= 0 );
    r = m_engine->RegisterObjectBehaviour("Vector2d", asBEHAVE_CONSTRUCT, "void Vector2d()", asFUNCTION(DefaultCtor<Vector2d>), asCALL_CDECL_OBJLAST); assert( r >= 0 );
    r = m_engine->RegisterObjectBehaviour("Vector2d", asBEHAVE_DESTRUCT, "void ~Vector2d()", asFUNCTION(DumbyFunc), asCALL_CDECL_OBJLAST); assert( r >= 0 );
    
    r = m_engine->RegisterObjectProperty("Vector2d", "float x", offsetof(Vector2d,x)); assert( r >= 0 );
    r = m_engine->RegisterObjectProperty("Vector2d", "float y", offsetof(Vector2d,y)); assert( r >= 0 );

    r = m_engine->RegisterObjectMethod("Vector2d", "float length() const", asMETHOD(Vector2d, length), asCALL_THISCALL); assert( r >= 0 );
    r = m_engine->RegisterObjectMethod("Vector2d", "Vector2d normalize() const", asMETHOD(Vector2d, normalize), asCALL_THISCALL); assert( r >= 0 );
    r = m_engine->RegisterObjectMethod("Vector2d", "float distanceTo( const Vector2d@ v ) const", asMETHOD(Vector2d, distanceTo), asCALL_THISCALL); assert( r >= 0 );
    r = m_engine->RegisterObjectMethod("Vector2d", "float dot( const Vector2d@ v ) const", asMETHOD(Vector2d, dot), asCALL_THISCALL); assert( r >= 0 );

But I get the following compiler warning:
Quote: warning: invalid access to non-static data member 'Vector2d::x' of NULL object warning: (perhaps the 'offsetof' macro was used incorrectly) warning: invalid access to non-static data member 'Vector2d::y' of NULL object warning: (perhaps the 'offsetof' macro was used incorrectly)
Any ideas why this might be?
==============================
A Developers Blog | Dark Rock Studios - My Site
The offsetof() macro that is defined in stddef.h (if I recall correctly) produces some code that GNUC doesn't like very much. It does give the correct result though.

It may have to do with the fact that your class has constructors, which by specifications turns it into a non-pod structure.

There ought to be some other way of determining the offsets, without using the offsetof() macro, but I've never searched for it since the macro is working (albeit with warnings on GNUC).

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Yep I just did a bunch of reading up on it, it appears you are completely right.

Apparently, as long as it's a simple class, even multiple inheritance is fine, as long as it doesn't have any virtual inheritance. Everything I've read said that every compiler people knew about implemented things so it would still work in this case.

But the spec still allows for some leeway in the memory layout of the class once constructors are introduced, so in theory, a compiler could lay it out in a way that would break this. And that is what GCC is telling me.

Now I just need to find a way to pragma out the warning or something :P
==============================
A Developers Blog | Dark Rock Studios - My Site

This topic is closed to new replies.

Advertisement