Apply Lua in your game (3): Using Lua in CPP (Package Stack Operation)

xiaoxiao2021-03-06  41

The previous mentioned that Lua and host programs exchange information through a runtime stack, so we make a simple package to the stack.

We use the function overload mechanism from the C to package these operations, and the overload provides us with a mechanism for processing the operation in a unified manner.

Passing information to Lua is done by stacking, so we define some push () functions:

Inline void push (Lua_State * L, int value); Inline Void Push (Lua_State * L, Bool Value); ...

For a simple C built-in type, we implements the same PUSH function, as for the implementation of the function, it is very simple, as long as the API provided by Lua can be implemented, for example:

Inline void push (Lua_State * L, int value) {Lua_PushNumber (L, Value);

The advantage of this way is that in our code we can process stack operations in a unified manner, if there is a type without defined stack operation, a compile period error will be generated.

Later I will mention how to pass a user-defined type pointer to Lua, in which case our basic code does not have to change, as long as a corresponding PUSH () function can be added.

Remember the CLOSE-Open principle, it means that it is closed, which is open to the expansion, and good class library design allows you to expand it without the need to modify its implementation, and even do not need to recompile.

A book of "C generic design new thinking" mentioned a technology called Type2Type, its essence is very simple:

Template struct type2type {typedef t u;};

As you can see, it doesn't have any data members, and its existence is just to carry type information.

Mapping of types to types is very useful when applying to the overload function, using type2type, you can implement the class assignment.

Let's take a look at how we use Type2Type when you get Lua information from your stack:

Test Type: Since the LUA type system is different from C , we have to test the information in the stack.

Inline Bool Match (Type2Type , Lua_State * L, Int IDX) {RETURN Lua_Type (L, IDX) == Lua_TBOOLEAN;

Similarly, we have to provide the corresponding MATCH function for the CPP's built-in type:

Inline Bool Match (Type2Type , Lua_State * L, Int Idx); Inline Bool Match (Type2Type , Lua_State * L, Int iDX);

...

It can be seen that the existence of Type2Type is just in order to resolution to the correct function when calling Match, because it does not have any members, there is no runtime cost.

Similarly, we provide get () functions for the CPP built-in type:

Inline Bool Get (Type2Type , Lua_State * L, INT IDX) {Return Lua_Toboolean (L, IDX);

Inline int GET (Type2Type , Lua_State * L, int IDX) {returnus static_cast (Lua_tonumber (L, IDX));

...

I think you may notice that there is a transformation action in int GET (Type2Type ), because Lua's type system is different from the CPP type, so the transformation action must be. In addition, there is a small detail in the GET overload function (S), the return value of each GET function is different, because the overload mechanism is identified by the different parameters, not return value.

As mentioned earlier, some basic package, we will show how to register a multi-parameter C function to Lua. do you remember? Using Lua's API can only register int (* ua_state *) C function, don't forget, Lua is written with C.

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

New Post(0)