I'm having trouble finding out what subtype is being used in my script templates when they are not primitive types, like an application class object, etc...
Here is the registrations for the template:
//The array type
r = engine->RegisterObjectType("array<T>",0,asOBJ_REF | asOBJ_GC | asOBJ_TEMPLATE); assert( r >= 0);
//Factories and callbacks
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_FACTORY, "array<T>@ f(int&in)", asFUNCTIONPR(IrrArrayFactory, (asIObjectType*), void), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_FACTORY, "array<T>@ f(int&in, u32)", asFUNCTIONPR(IrrArrayFactory2, (asIObjectType*, u32), void), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_FACTORY, "array<T>@ f(int&in, const array<T> &in)", asFUNCTIONPR(IrrArrayFactory3, (asIObjectType*, const array<class T>), void), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_TEMPLATE_CALLBACK, "bool f(int&in)", asFUNCTION(TemplateCallback), asCALL_CDECL); assert( r >= 0 );
//These are just to get the type to register properly for testing...I will implement a proper wrapper or something later
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_ADDREF, "void f()", asFUNCTION(_NullFunc), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_RELEASE, "void f()", asFUNCTION(_NullFunc), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(_NullFunc), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(_NullFunc), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(_NullFunc), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(_NullFunc), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(_NullFunc), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//The factory function itself
void IrrArrayFactory(asIObjectType *ot) //This is void for testing, later it will return an array
{
s32 typeId = ot->GetSubTypeId(); //s32 is just an int typedef
stringc typeName = ot->GetName(); //stringc is my 8 bit char string class (in contrast to 16 bit stringw)
cout<<"IrrArrayFactory()\n";
cout<<"Object: "<<typeName.c_str()<<" Type: "<<typeIdToStr(typeId)<<"\n";
return;
}
Because I do not know the template subtype yet, I can't instance an array<T> object yet. So I planned on doing something like this in the factory functions:
switch (subtype) {
case stringc:
array *a = new array<stringc>(/* params */);
case etc...:
//continue for rest of defined types
}
But that's not too flexible...is there a way to find the type of the object in the application and dynamically instance a type in c++? Or am I going about this in the wrong way?
As a side note, I wrote a function to find the subtype id name as such:
char* typeIdToStr(u32 id)
{
cout<<"id was: "<<hex<<id<<" \n";
switch (id) {
case asTYPEID_VOID:
return "void";
case asTYPEID_BOOL:
return "bool";
case asTYPEID_INT8:
return "int8";
case asTYPEID_INT16:
return "int16";
case asTYPEID_INT32:
return "int32";
case asTYPEID_INT64:
return "int64";
case asTYPEID_UINT8:
return "uint8";
case asTYPEID_UINT16:
return "uint16";
case asTYPEID_UINT32:
return "uint32";
case asTYPEID_UINT64:
return "uint64";
case asTYPEID_FLOAT:
return "float";
case asTYPEID_DOUBLE:
return "double";
case asTYPEID_OBJHANDLE:
return "objhandle";
case asTYPEID_HANDLETOCONST:
return "handletoconst";
case asTYPEID_MASK_OBJECT:
return "mask_object";
case asTYPEID_APPOBJECT:
return "appobject";
case asTYPEID_SCRIPTOBJECT:
return "scriptobject";
case asTYPEID_TEMPLATE:
return "template";
case asTYPEID_MASK_SEQNBR:
return "mask_seqnbr";
default:
return "unknown";
}
}
However, when executing the following script that makes an array of stringc's (an application registered string class), it returns:
IrrArrayFactory()
id was: 67108892
Object: array Type: unknown
Test Script:
print("Testing array object stuff...\n");
array<stringc> the_strings; //Don't actually do anything with it...there's no real factory yet...
Does this indicate the the array subtype is invalid? It didn't throw an error like I would expect, nor is it any of the predefined values. (Note that this works fine for primitives):
print("Testing array object stuff...\n");
array<u32> the_strings;
Result:
IrrArrayFactory()
id was: 8
Object: array Type: uint32