Luawrapper for C ++ released

zhaozj2021-02-16  103

Integrated Lua script writers in C : Mu Feng (Second Life members) Copyright reproduced, please indicate the original source Home: Second Life http://www.d2-life.com http: //www.d2-life. COM / LBS / BlogView.asp? logid = 41 Why use Lua as a script? Use Lua as a script, mainly because it is small and exquisite (small size, fast running), and its grammar is relatively simple. However, using Luaapi to integrate Lua Engine into the program, there is indeed inconvenient - in words with the landing of the wind, it is "As a compilation". Of course, now you don't have to worry so hard because you can use Luawrapper for C . Using this tool, integrating Lua scripts in C is a light. Your original C function and class can hardly share with Lua scripts. Let's explain it by example, how to integrate Lua scripts into your program with Luawrapper. 1. Create Lua Engine Luawrap Lua; or Luawrap * Lua = New Luawrap; create a LuawRap object, which is created a Lua script engine. And according to the characteristics of Lua, you can create any plurality of LUA engines, or even distribute them in different threads. 2. Load and execute the script You can load Lua scripts from the buffer: Lua.LoadString ("PRINT ('Hello World')"); of course, you can also load from the file and perform Lua scripts: Lua. LOADFILE ("./ Test.lua"); Lua's script can be a source code or after compiled intermediate code. Maybe you are more interested in compiled middle code - if you don't want to make the source code red naked reveal in front of everyone. 3. Get and set the content of the LUA variable to get and set the script variable, is the most basic function. You can use the getGlobal and setglobal functions to do this: (1) Get variables: int a = lua.getglobal ); luatable table = Lua.getglobal ("t"); here The type of <> is the type of variable you want. (2) Setting the variable: Lua.setglobal ("a", a); Lua.setglobal ("t", table); 4. Call the lua function Using the Call function, you can easily call the Lua function from your program. : Lua.call ("Print", "Hello World"); int sum = Lua.call ("add", 2, 3); Here, the type of <> is the type of return value. 5. How to make Lua can also call the C function brilliant place.

If there is a function below: int Add (int A, int b) {RETURN A B;} If you want it to allow Lua to use it, you can register it to the Lua engine: Lua.RegisterFunc "add", int, int, add); this way, LUA can be used directly: (Lua script) SUM = Add (1, 3) (*) RegisterFunc's function, let you put C The function is registered into Lua for the Lua script. The first parameter is a function name that wants to use in Lua. The second parameter is the prototype of the function in C ; C allows the function to be overloaded, you can use the function prototype to select the function you need to register into the Lua engine. The third parameter is the pointer of the function in C . 6. How to make C class in Lua to take a look at this C class: class myarray {std :: vector array; public: void setValue; Double GetValue (int index) ); Int size (); const char * toString ();}; You are ready to let Lua can freely access and operate this class. Very simple, you just need to add a few macro definitions: class myarray {std :: vector array; public: void setValue; Double getValue; int size () Const char * toString (); // is easy to use a Class as a Lua object, just add the following macro definition. Define_typename ("my.array"); begin_reglualib ("array") lualib_item_create ("New", myarray) // Create MyArray LUALIB_ITEM_DESTROY ("DEL", MyArray) // Eliminate MyArray. END_REGLUALIB () BEGIN_REGLUALIB_MEMBER () LUALIB_ITEM_FUNC ( "size", int (MyArray *), & MyArray :: size) LUALIB_ITEM_FUNC ( "__ getindex", double (MyArray *, int), & MyArray :: getvalue) LUALIB_ITEM_FUNC ( "__ newindex", void ( MyArray *, int, double, & myarray :: setValue) LUALIB_ITEM_FUNC ("__ toString", const char * (myarray *), & myarray :: toString) LUALIB_ITEM_DESTROY ("__ gc", myarray) // is used to eliminate objects.

END_REGLUALIB_MEMBER ()}; as long as these macro definitions, this class is the class that can be used in Lua, we can register this class in Lua: Lua.Register () After registration, we are in Lua This class can be used: a = array.new () - Create an object, equivalent to a = new myarray a [1] = 10 - call __newindex, which is C A-> setValue (1, 10) a [2] = 20 - call __newindex, that is, A-> setValue (2, 20) Print (A, - call __tostring, also in C A-> toString () : size (), - A-> size () A [1] in C , - call __getindex, also in C A-> getValue (1) a [2]) - call _ _GetIndex, that is, A-> getValue in C (2) Array.del (a) - Clear object, equivalent to delete a a = nil - Clear A, very like C A = null is of course, you can also Don't use Del this object, but wait for Lua to help you automatically collect. When trash recovery in Lua, it will automatically call the __gc of this object, which is equivalent to Delete. So, what should I do if I want to create myArray objects in C and how to pass it to Lua global variables? Just like the same, use setglobal: myarray * a = new myarray; lua.setglobal ("a", a); to get the object, the same, should use getGlobal: myarray * a = Lua.getglobal ("a"); For objects passing to Lua, let Lua manage the object's living cycle. If you have to delete it, you can use DelglobalObject: Lua.delglobalObject ("a"); but do this, you should understand what you are doing, because in Lua's script, you may already be more This object is quoted. Deleting one of them will cause other reference objects to fail, which may cause the system to crash. (1) Define_typename ("my.array"); defines the name of the type. In Lua, this type name is unique to identify C types, you must give different names for different objects. (2) Begin_REGLUALIB ("array") ... End_Reglualib () You can define a library for an object, "array" is the name of the library. The function defined in the library is a global function. In Lua, use this function, you need to add the name of the library before the function, such as: array.new (). Typically, the library will contain methods for creating objects.

Such as: lualib_item_create ("New", MyArray) // Create MyArray

This way, you can also create MyArray: a = array.new () in Lua, you can also choose to add a delete object action: lualib_item_destroy ("DEL", myarray) // Remove MyArray, you can delete an object directly. : Array.del (a) (3) Begin_REGLUALIB_MEMBER () ... END_REGLUALIB_MEMBER () here, where you can define the member function of the object, or overload the operator of the object - Yes, it is like C Operator overload. For example: lualib_item_func ("__ newindex", void (MyArray *, int, double), & myarray :: setValue is the overload Operator [] operator. There are still many operators in Lua, such as: __getindex: operator [], support read access, such as v = a [10] __NewDex: operator [], support assignment access, such as A [10] = 1.22 __toString: Convert the variable into a string __add: equivalent to operator __add: operator __sub: operator - __mul: operator × __div: operator ÷ __pow: operator ^ (multiplication) __UNM: One dollar operator - __concat: Operator .. (String Connection) __eq: Operator == (A ~ = B) __lt: Operator = b is equivalent to B <= A, pay attention to, if "__le" is not defined, Lua will try to convert a <= B into NOT (B

There are many compilers that currently support this feature. In the VisualStudo product line, only VC7.1 can support this feature, so you are using VisualStudio, please confirm that you use VisualStudio2003. If you think Luawrapper for C can help you, I will feel very honored. I am willing to share this library to everyone. By the way, if you find bug during use, or have a good suggestion, I hope you can contact me. You are in the process of use, please do not delete signature information in the file; if you modify the library, please add your modification in the modified file. Of course, I will welcome you to feed the modified program. I will continue to optimize and improve it. Need to download Luawrapper for C , please go to http://www.d2-life.com/lbs/blogview.asp?logid=41

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

New Post(0)