AngelScript

Automatic wrapper functions

Path: /sdk/add_on/autowrapper/aswrappedcall.h

This header file declares some macros and template functions that will let the application developer automatically generate wrapper functions using the generic calling convention with a simple call to a preprocessor macro. This is useful for those platforms where the native calling conventions are not yet supported.

The macros are defined as:

// Wrap a global function with implicit or explicit signature
#define WRAP_FN(name)
#define WRAP_FN_PR(name, Parameters, ReturnType)
// Wrap a class method with implicit or explicit signature
#define WRAP_MFN(ClassType, name)
#define WRAP_MFN_PR(ClassType, name, Parameters, ReturnType)
// Wrap a global function that will emulate a class method and receives the 'this' pointer as the first argument
#define WRAP_OBJ_FIRST(name)
#define WRAP_OBJ_FIRST_PR(name, Parameters, ReturnType)
// Wrap a global function that will emulate a class method and receives the 'this' pointer as the last argument
#define WRAP_OBJ_LAST(name)
#define WRAP_OBJ_LAST_PR(name, Parameters, ReturnType)
// Wrap a constructor with an explicit signature
#define WRAP_CON(ClassType, Parameters)
// Wrap a destructor
#define WRAP_DES(ClassType)

As you can see they are very similar to the asFUNCTION and asMETHOD macros, and are used the same way.

Unfortunately the template functions needed to perform this generation are quite complex and older compilers may not be able to handle them. One such example is Microsoft Visual C++ 6, though luckily it has no need for them as AngelScript already supports native calling conventions for it.

# Example usage

#include "aswrappedcall.h"
// The application function that we want to register
void DoSomething(std::string param1, int param2);
// Registering the wrapper with AngelScript
void RegisterWrapper(asIScriptEngine *engine)
{
int r;
// The WRAP_FN macro automatically implements and returns the function pointer of the generic wrapper
// function. Observe, that the calling convention should be set as asCALL_GENERIC in this case.
r = engine->RegisterGlobalFunction("void DoSomething(string, int)", WRAP_FN(DoSomething), asCALL_GENERIC); assert( r >= 0 );
}

# Adding support for more parameters

The aswrappedcall.h header file is by default prepared to support functions with up to 4 arguments. If you have a need for more arguments then you can use the generator that you find in the sub-directory to prepare a new header file.

Open the generateheader.cpp in an editor and set the max_args variable to the number of arguments you need and then compile and run the code. It will print the new header file to the standard output so you just need to direct this to a file.