Torque 3D 3.8
_DefineMethodTrampoline Macro
#define _DefineMethodTrampoline( className, name, returnType, args ) \ TORQUE_API EngineTypeTraits< returnType >::ReturnValueType \ fn ## className ## _ ## name ( className* object, _EngineMethodTrampoline< _ ## className ## name ## frame, returnType args >::Args a ) \ { \ _CHECK_ENGINE_INITIALIZED( className::name, returnType ); \ return EngineTypeTraits< returnType >::ReturnValue( \ _EngineMethodTrampoline< _ ## className ## name ## frame, returnType args >::jmp( object, a ) \ ); \ }

The next thing is a bit tricky. DefineEngineMethod allows to make the 'object' (=this) argument to the function implicit which presents quite an obstacle for the macro internals as the engine export system requires the name of a DLL symbol that represents an extern "C" function with an explicit first object pointer argument. 

Even if we ignored the fact that we don't have a guarantee how the various C++ compilers implement implicit 'this' arguments, we could still not just use a C++ method for this as then we would have to get past the C++ compiler's mangling to get to the function symbol name (let alone the fact that typing this method correctly would be tricky). 

So, the trick employed here is to package all but the implicit 'this' argument in a structure and then define an extern "C" function that takes the object pointer as a first argument and the struct type as the second argument. This will result in a function with an identical stack call frame layout to the function we want. 

Unfortunately, that still requires that function to chain on to the real user-defined function. To do this cleanly and portably, _EngineMethodTrampoline is used to unpack and jump the call from extern "C" into C++ space. In optimized builds, the compiler should be smart enough to pretty much optimize all our trickery here away.

Copyright (c) 2015. All rights reserved.
What do you think about this topic? Send feedback!