• Create Account

### #ActualAndreas Jonsson

Posted 07 May 2013 - 07:58 PM

There is no need to wrap a function that receives a pointer. Just tell AngelScript that the pointer is really a reference. Pointers and references in C++ use the same ABI so there is really no difference.

// C++
void foo(Bar*);

engine->RegisterGlobalFunction("void foo(Bar &)", asFUNCTION(foo), asCALL_CDECL);


If you're not worried about the script writer not doing the proper memory management you can certainly use asOBJ_NOCOUNT. This flag just tells AngelScript that the type is not reference counted and that the application will be responsible for making sure the memory is properly cleaned up.

Another option is to register a value type to hold a Bar*, e.g.

void ClearPtr(void *p) { *reinterpret_cast<void**>(p) = 0; }
engine->RegisterObjectType("BarPtr", sizeof(Bar*), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_PRIMITIVE);
engine->RegisterObjectBehaviour("BarPtr", "void f()", asFUNCTION(ClearPtr), asCALL_CDECL_OBJLAST);

void foo(Bar*);
engine->RegisterGlobalFunction("void foo(BarPtr)", asFUNCTION(foo), asCALL_CDECL);


It is definitely possible to use external reference counting. The AddRef and Release behaviours can for example use the object pointer to quickly find the external reference counter in a hash map. This look up is obviously going to add an overhead, but depending on what you intend to use the scripting for, this overhead is possibly quite negligible.

Although I've never tried it. If you want to use std::function, you ought to be able to do so by registering it with the calling convention asCALL_THISCALL_ASGLOBAL, and using the asMETHOD to take the address of the operator() method.

### #1Andreas Jonsson

Posted 07 May 2013 - 12:23 PM

There is no need to wrap a function that receives a pointer. Just tell AngelScript that the pointer is really a reference. Pointers and references in C++ use the same ABI so there is really no difference. If you want to use std::function, you ought to be able to do so by registering it with the calling convention asCALL_THISCALL_ASGLOBAL, and using the asMETHOD to take the address of the operator() method.

PARTNERS