How to integrate Lua script (luaplus) in C ++ time: 14:39:54 Source: Network Author: Unknown CLICK: 214 times last year I made a C ++ package for Lua script, I am flattered that many of my friends are interested and try to use it. As a matter of fact, I have made a lot of defects in packaging and learning. To avoid detours, I recommend using luaplus as a C ++ package. Last year, I made a C ++ package for the Lua script. I was flattered that many of my friends were interested in it and tried it. As a matter of fact, I have made a lot of defects in packaging and learning. To avoid detours, I recommend using luaplus as a C ++ package. Luaplus is the c ++ enhancement of Lua. That is to say, luaplus itself is enhanced on the Lua source code. Cooperation with C ++ is a good choice. Luaplus current version: luaplus for Lua 5.01 distribution build 1080 (February 28,200 4 ). You can download to the http://luaplus.org/Site: Source code (http://wwhiz.com/LuaPlus/LuaPlus50_Build1081.zip) Http://wwhiz.com/LuaPlus/LuaPlus50_Build1081_Win32Binaries.zip (target code) I will explain how to use luaplus and how to make it easier for luaplus to work with C ++ classes. 1. Call the Lua script // Create a Lua interpreter: Luastateowner state; // Execute the Lua script: State-> dostring ("Print ('Hello world \ n ')"); // Load the Lua script file and execute: State-> dofile ("C: \ test. Lua "); // Load the compiled Lua script file and execute it: State-> dofile ("C: \ test. luac "); 2. Interaction with Lua scripts // Set variables for Lua scripts State-& gt; getglobals (). setnumber ("myvalue", 123456 ); // Obtain the Lua variable value Int myvalue = state-> getglobal ("myvalue"). getinteger (); // Call the Lua Function Luafunction <int> luaprint = state-> getglobal ("print "); Luaprint ("Hello world \ n "); // Let Lua call the C language function Int add (int A, int B) {return a + B ;} State-> getglobals (). registerdirect ("add", add ); State-> dostring ("Print (add (3, 4 ))"); // Let Lua call C ++ class member functions Class test {public: int add (int A, int B) {return a + B ;}}; Test test; State-> getglobals (). registerdirect ("add", test, add ); State-> dostring ("Print (add (3, 4 ))"); 3. Use the C ++ class in the Lua script This is a little troublesome. However, I wrapped a luaplushelper. h file, which can easily complete this job. Its implementation is also very simple. You can obtain from the source code how to use pure luaplus to implement the same function. However, there is still a restriction that cannot be solved: Virtual member functions cannot be used. However, considering that we only call the C ++ function in Lua, it is not necessary to import C ++ to Lua perfectly. This restriction is completely acceptable. In addition, class member variables cannot be directly accessed in Lua, and can be accessed through class member functions (such as setvalue/getvalue ). // The following is a simple C ++ class: Class Logger { Public: Void logmember (const char * message) { Printf ("in member function: % s \ n", message ); } Logger () { Printf ("constructing (% P)... \ n", this ); V = 10; } Virtual ~ Logger () { Printf ("destructing (% P)... \ n", this ); } Logger (int n) { Printf ("-- Constructing [% d] (% P)... \ n", N, this ); } Logger (logger * logger) { Printf ("-- Constructing [% P] (% P)... \ n", logger, this ); Logger-> logmember ("call from constructor \ n "); } Int setvalue (INT Val) { V = val; } Int getvalue () { Return V; } Public: Int V; }; // Import the script to Lua: Luaclass <logger> (state) . Create ("logger") // defines the constructor logger: logger () . Create <int> ("logger2") // defines the constructor logger: logger (INT) . Create <logger *> ("logger3") // defines the constructor logger: logger (logger *) . Destroy ("free") // defines the Destructor logger ::~ Logger () . Destroy ("_ GC") // defines the Destructor logger ::~ Logger () . Def ("lm", & logger: logmember) // defines the member function logger: logmember (const char *) . Def ("setvalue", & logger: setvalue) . Def ("getvalue", & logger: getvalue ); // Use the logger class in Lua (1 ): State-> dostring ( "L = logger ();" // call the constructor logger: logger () "L. LM ('Hello world 1');" // call the member function logger: logmember (const char *) "L. Free ();" // call the Destructor logger ::~ Logger () ); // Use the logger class in Lua (2 ): State-> dostring ( "M = logger (10);" // call the constructor logger: logger (INT) "M. LM ('Hello world 2');" // call the member function logger: logmember (const char *) "N = logger (m);" // call the constructor logger: logger (logger *) "N. LM ('Hello world 3');" // call the member function logger: logmember (const char *) "M. setvalue (11 );" "Print (M. getvalue ());" "M, n = nil, nil;" // M, N will be recycled by Lua's vertex pole to call the destructor ); 4. classify a group of C functions into the Lua module. // Like above, I use luaplushelper. h to simplify it: Luamodule (state, "mymodule ") . Def ("add", add) . Def ("Add2", test, add ); State-> dostring ( "Print (mymodule. Add (3, 4 ));" "Print (mymodule. Add2 (3, 4 ));" ); 5. Use the Lua table data type // Create a table in Lua Luaobject table = state-> getglobals (). createtable ("mytable "); Table. setinteger ("M", 10 ); Table. setnumber ("F", 1.99 ); Table. setstring ("S", "Hello World "); Table. setwstring ("ch", l "hello "); Table. setstring (1, "What "); // Equivalent: // Mytable = {M = 10, F = 1.99, S = "Hello World", CH = l "hello", "What "} // You can also use table as the key and value: State-> getglobals (). createtable ("nexttable ") . Setstring (table, "hello ") . Setobject ("OBJ", table ); // Equivalent: // Nexttable = {mytable = "hello", OBJ = mytable} // Obtain the table content: Luaobject t2 = state-> getglobals ("mytable "); Int M = t2.getbyname ("M"). getinteger (); Luaobject T3 = state-> getglobals ("nexttable "); STD: String STR = t3.getbyobject (T2). getstring (); 6. traverse the table Luastateowner state; State. dostring ("mytable = {HI = 5, hello = 10, yo = 6 }"); Luaobject OBJ = state. getglobals () ["mytable"]; For (luatableiterator it (OBJ); it. Next ()) { Const char * Key = it. getkey (). getstring (); Int num = it. getvalue (). getinteger (); } End The above is just a simple example to illustrate how to use luaplus and luaplushelper. For more information, see luaplus. This articleArticleSource: Institute of Development http://edu.codepub.com/Source: http://edu.codepub.com/2009/0730/11509.php |