Registering a value type

When registering a value type, the size of the type must be given so that AngelScript knows how much space is needed for it. If the type doesn't require any special treatment, i.e. doesn't contain any pointers or other resource references that must be maintained, then the type can be registered with the flag asOBJ_POD. In this case AngelScript doesn't require the default constructor, assignment behaviour, or destructor as it will be able to automatically handle these cases the same way it handles built-in primitives.

If the type will be passed to and from the application by value using native calling conventions, it is important to inform AngelScript of its real type in C++, otherwise AngelScript won't be able to determine exactly how C++ is treating the type in a parameter or return value. There are a few different flags for this:

asOBJ_APP_CLASS   The C++ type is a class, struct, or union
asOBJ_APP_CLASS_CONSTRUCTOR   The C++ type has a defined constructor
asOBJ_APP_CLASS_DESTRUCTOR   The C++ type has a defined destructor
asOBJ_APP_CLASS_ASSIGNMENT   The C++ type has a defined assignment operator
asOBJ_APP_PRIMITIVE   The C++ type is a C++ primitive, but not a float or double
asOBJ_APP_FLOAT   The C++ type is a float or double

Todo:
The use of asOBJ_APP_CLASS and related flags is not clear to newcomers. It must be explained better.
// Register a primitive type, that doesn't need any special management of the content
r = engine->RegisterObjectType("pod", sizeof(pod), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_PRIMITIVE); assert( r >= 0 );

// Register a class that must be properly initialized and uninitialized
r = engine->RegisterObjectType("val", sizeof(val), asOBJ_VALUE | asOBJ_APP_CLASS_CDA); assert( r >= 0 );

See also:
The string object (STL) or vector3 add-on for examples of value types

Constructor and destructor

If a constructor or destructor is needed they shall be registered the following way:

void Constructor(void *memory)
{
  // Initialize the pre-allocated memory by calling the
  // object constructor with the placement-new operator
  new(memory) Object();
}

void Destructor(void *memory)
{
  // Uninitialize the memory by calling the object destructor
  ((Object*)memory)->~Object();
}

// Register the behaviours
r = engine->RegisterObjectBehaviour("val", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Constructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("val", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(Destructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

The assignment behaviour is registered the same way as for reference types.


Generated on Mon Mar 30 22:13:56 2009 for AngelScript by  doxygen 1.5.6