Lua (2), idlcpplua
In the last tutorial on idlcpp in C ++ mixed programming (I), I introduced the use of idlcpp. Now we will explain the sample tutorial in idlcpp. Here is an example of the Lua language. First, look at the first example program LuaTutorial0. Like the first example in many languages, it is a program that prints Hello world. Use Visual Studio 2015 to open the solution file tutorials \ LuaTutorials. sln, which contains multiple engineering files.
In the LuaTutorial0 project, three files have been added: LuaTutorial0.cpp, Tutorial0. I, and tutorial0.lua. The content of Tutorial0. I is as follows:
//tutorial$$#include <stdio.h>namespace tutorial{ struct Test { static void Run(); }; $* inline void Test::Run() { printf("Hello World!"); } *$}
It looks like C ++ code. Compile Tutorial0. I and generate four files: Tutorial0.h, Tutorial0.mh, Tutorial0.ic, and Tutorial0.mc. The content of Tutorial0.h is as follows:
//DO NOT EDIT THIS FILE, it is generated by idlcpp//http://www.idlcpp.org#pragma once#include <stdio.h>#line 4 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i"namespace tutorial#line 5 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i"{#line 6 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i" struct Test#line 7 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i" { public:#line 8 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i" static void Run();#line 9 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i" }; inline void Test::Run() { printf("Hello World!"); } #line 16 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i"}
Because in the compilation. the-ld option is specified when the I file is created. there are many # line commands in the hfile, which can be located during the next C ++ compilation. i, instead of locating. h. Modify the compilation option, remove the-ld option, and re-compile. The result is as follows:
//DO NOT EDIT THIS FILE, it is generated by idlcpp//http://www.idlcpp.org#pragma once#include <stdio.h>namespace tutorial{ struct Test { public: static void Run(); }; inline void Test::Run() { printf("Hello World!"); } }
This looks refreshing. Please compare it with the above Tutorial0. I content. Basically, the content is similar. The first line is as follows:
// Tutorial
This is a comment. Like C ++, idlcpp uses // to represent a single line comment and/**/to represent a comment.
$ # Include <stdio. h>
Idlcpp only analyzes the declaration of the interface, and other content will usually appear in the C ++ header file. Here, idlcpp provides a method to copy Part Of The. I file directly to. h. There are three methods
This line indicates copying # include <stdio. h> to. h. The following printf uses this header file.
Namespace tutorial and corresponding {}.
The concepts in namespace and C ++ are the same and will be output to. h as is.
Struct Test and corresponding {};.
This is similar to the concept in C ++ and will be output to. h as is.
The following. h file contains one more line
Public:
The data members and member functions declared in idlcpp are regarded as public, so this line is left empty.
Static void Run (); both sides of the line are the same. Declare a static member function.
$ *
Inline void Test: Run ()
{
Printf ("Hello World! ");
}
* $
As described above, idlcpp copies content between $ * and * $ to. h. Because idlcpp only processes the function declaration and cannot process its implementation code, it is impossible to put the modern code in the declaration of the class like C ++ and can only be written outside. To write a. cpp file less, you can write it in the header file using inline functions.
The Tutorial0.ic file does not contain any substantive content.
The Tutorial0.mh and Tutorial0.mc files are used to construct the corresponding metadata information. The specific content involves too much and is not explained for the time being.
Let's take a look at the content of LuaTutorial0.cpp.
#include <tchar.h>#include <string>#include <windows.h>#include "lua.hpp"#include "../../../paf/src/paflua/LuaWrapper.h"#include "../../Common/Tutorial0.h"#include "../../Common/Tutorial0.mh"#include "../../Common/Tutorial0.ic"#include "../../Common/Tutorial0.mc"#if defined(_DEBUG)#pragma comment(lib,"pafcore_d.lib")#pragma comment(lib,"paflua_d.lib")#pragma comment(lib,"lua53_d.lib")#else#pragma comment(lib,"pafcore.lib")#pragma comment(lib,"paflua.lib")#pragma comment(lib,"lua53.lib")#endifvoid GetExePath(std::string& path){ char fileName[MAX_PATH]; GetModuleFileName(0, fileName, sizeof(fileName)); const char* end = _tcsrchr(fileName, '\\'); path.assign(fileName, end + 1);}int _tmain(int argc, _TCHAR* argv[]){ int error; lua_State *L = luaL_newstate(); luaL_openlibs(L); luaopen_paflua(L); std::string path; GetExePath(path); path += "tutorial0.lua"; error = luaL_loadfile(L, path.c_str()) || lua_pcall(L, 0, 0, 0); if (error) { fprintf(stderr, "%s\n", lua_tostring(L, -1)); lua_pop(L, 1); } lua_close(L); return 0;}
# Include "lua. hpp"
This line introduces the lua header file
# Include ".../paf/src/paflua/LuaWrapper. h"
This line introduces the lua Plug-In header file
# Include ".../../Common/Tutorial0.h"
# Include ".../Common/Tutorial0.mh"
# Include ".../../Common/Tutorial0.ic"
# Include ".../../Common/Tutorial0.mc"
These lines are included by the Tutorial0. I compilation results. After compilation, the corresponding metadata information is registered to the system, so that the footstep language can be accessed.
The main () function is a basic process for running a lua script. Where
Luaopen_paflua (L );
This line loads the plug-in the lua virtual machine.
Finally, let's take a look at the content of tutorial0.lua.
paf.tutorial.Test.Run();
This Code indicates that: tutorial: Test: Run ();
All data types generated by idlcpp are under the name of paf. It can be understood that the name of paf in lua is equivalent to the global namespace in C ++. In C ++, the full name of the Run function can be considered to be: tutorial: Test: Run. In lua, this function is called paf. tutorial. Test. Run.
After the link is compiled, the execution result is as follows:
We can see that Lua correctly calls the functions in C ++.