Applying Lua in your game (3): Using Lua in CPP (basic data type, pointer, and reference)

xiaoxiao2021-03-06  41

The current question now is that we hope to get a template parameter t in a compile period whether T is not a pointer or a reference? Fortunately, the Boost library has provided such a function, and Boost provides a set of type_traits that allows us to get the type of template parameter T during the compile period.

You can download the BOOST library from http://boost.sourceforge.net/index.sourceforge.net/index.sourceforge.net/index.htm; we need a group of Boost's Type_Traits.

After downloading the Boost library, set the included path, let us start modifying our Call function, making appropriate changes to pointers and references. After having a Boost library, test a template parameter T is a pointer or a reference to another simple:

#include using namespace boost;

Template INT CALL (RT (* FUNC) (P1), Lua_State * L, Int IDX) {is_point :: value || is_reference :: value;}

Now we have been able to get a BOOL value. If this value is true, we do not try to get such a parameter from the Lua stack, but first give it to the call function processing, then hand it over to Lua, and return At the time, it is a copy of the content referring to this pointer. If we leave, we only give it to the call function as a parameter.

In order to be able to do a static assignment of the compile period, we use the overload mechanism of the function. First, we need to define a BOOL to type mapping to distinguish between parameters:

Template Struct Bool2Type {Enum {Value = B};

For the sake of clarity, we make a Typedef in both forms:

TypedEf Bool2Type ResultType; typedef bool2type NormalType;

For a parameter, if its flag is ResultType, then it will be handed over to Lua, if it is a second form, then it is only given to the modulated function.

Now we have to provide two versions of the _call function for a Call function to distinguish between its pointer parameters:

Template INT _CALL (RT (* FUNC) (P1), ResultType, Lua_State * L, Int IDX) {}

Template INT _CALL (RT (* FUNC) (P1), NormalType, Lua_State * L, Int IDX) {}

Now we can handle directly to _call in the Call function:

Template Int Call (rt (* FUNC) (P1), Lua_State * L, Int IDX) {Return_Call (* Func, Bool2Type :: Value || Is_Reference : : value> (), L, IDX);

转载请注明原文地址:https://www.9cbs.com/read-68735.html

New Post(0)