Interactive detailed _lua of Lua and C + + languages

Source: Internet
Author: User
Tags lua

Objective

Anyone who has written a Windows program knows that for applications, if you need to save some local configuration information, we often write these configuration information in the registry or local configuration file, many applications are to write some configuration information in the configuration file, such as the end of the INI file, a lot of configuration files, is used very widely, and then the application will parse the configuration file and read some configuration information when it is started.

One of the most important uses of LUA is as a configuration language. This article will combine LUA to extend the application, which provides greater flexibility and convenience.

This blog post is mainly about using C + + and LUA to interact with the values of common variables in Lua, the values of table in Lua, and calls to functions in Lua. Let's start here.

Starting with one of the simplest examples

A GUI program that reads the size of the window from the configuration file, thereby enabling the size of the window to be set. Below I write an MFC based form program to complete this function. Click here to download complete code engineering. I post the key code:

Copy Code code as follows:

BOOL Cluaconfig::loadconfig ()
{
L = Lual_newstate ();
if (! L
{
return false;
}

Load configuration file
int bRet = Lual_loadfile (L, pconfigfile);
if (BRet)
{
return false;
}
Run configuration file
BRet = Lua_pcall (L, 0, 0, 0);
if (BRet)
{
return false;
}

Read High
Lua_getglobal (L, "width");
Lua_getglobal (L, "height");

Width
if (!lua_isnumber (L,-2))
{
return false;
}

Height
if (!lua_isnumber (L,-1))
{
return false;
}
Iwindowheight = Lua_tointeger (L,-1);
Iwindowwidth = Lua_tointeger (L,-2);
return true;
}

Lual_newstate will not say, with rotten; Lual_loadfile is used to load a LUA file and then invoke Lua_pcall to run the compiled program block, Lua_pcall to run the LUA code in protected mode, that is, error, Lua_ Pcall will return an error code and will not crash directly. When the program block is run, two times Lua_getglobal function is called, which presses the global variable value into the stack, so the value of width is at index-2, and the value of height is at index-1; Next, it's not much to say. That's it. Download the program, run it, OK, modify the Code folder under the Config.lua file to see the results of the operation. Source code here to download.

Table action

In Lua, if the C API can't manipulate the table, then we can still play happily. Let's take a look at how the C API operates the table. Now you have the following LUA statement:

Copy Code code as follows:

Background = {r = 0.3, G = 1, b = 0.5}

So how does the C API read this code and parse out each of these fields? I'll post the code first and then analyze it in one sentence:

Copy Code code as follows:

Read the global data into the stack
Lua_getglobal (L, "background");
if (!lua_istable (L,-1))
{
If it is not a table, an error message is displayed
cout << "It ' s not a table." << Endl;
return 0;
}

Reads the value of a field in the table and presses the value into the stack
Lua_getfield (L,-1, "R");

Reading values from the stack
if (!lua_isnumber (L,-1))
{
If it is not real, an error message is displayed
cout << "It ' s not a number." << Endl;
return 0;
}

Double fvalue = Lua_tonumber (L,-1);
cout << "R =>" << fvalue << Endl;

Forgive me for omitting the code like lual_newstate. Well, reading a table is a good way to read a global variable. is divided into the following steps:

1. Read the variable using Lua_getglobal and read the table to the stack;
2. Use Lua_getfield to read the value of the field in table and read the value of the field to the stack;
3. Use the Lua_to* series function to read the value of the field from the stack.

This is the operation of reading table, what about setting table? We can write our own values to the stack, what should we do? Look at the code:

Copy Code code as follows:

Set the value you want to set to the stack
Lua_pushnumber (L, 0.55);

Set this value to the table
Lua_setfield (L,-2, "R");

Is the above two lines of code, of course, you also need to first use Lua_getglobal read table variables, read the table to the stack, and then follow the two lines of code to set the OK. What is the specific meaning of the above two lines of code?

The 1.lua_push* series function is to place a new value that needs to be set on the stack;

2.lua_setfield function with Lua_getfield is a function of a property, except here is the set semantics, will be lua_push* to the value in the stack, set to the table corresponding key.

Now read the table and set the table, so how do you create a new table in the tables altogether? We have this demand. What to do?

Copy Code code as follows:

Create a new table and press it into the stack
Lua_newtable (L);

Set values into table
Lua_pushstring (L, "http://www.jb51.net"); First press the value into the stack
Lua_setfield (L,-2, "website"); Set the value to the table

Then set a value
Lua_pushstring (L, "Jelly wants | An original article sharing website ");
Lua_setfield (L,-2, "description");

I posted a few important lines of code, and most importantly, a lua_newtable function that creates a new table and places the table in the stack, and then the value of the table set above is the same. Download the source code one, download two.

Calling the LUA function

Yes, you're right, you can define a function in a LUA file, and then call this function in C + +, which looks like "tall". Now I'm going to talk about this "tall" function; the Habitual code:

Copy Code code as follows:

And then take a look at the arguments and return a worthwhile function call
Now we've defined an add function in Test.lua that calculates the value of two values, and these two values are passed in by argument.
Get and later, will return this and, now we are in C + + This call this add function
Lua_getglobal (L, "add"); Get the function and push it into the stack
Lua_pushnumber (L, 10); Press into the first parameter
Lua_pushnumber (L, 20); Press into the second parameter

Complete the call
IRet = Lua_pcall (L, 2, 1, 0);
if (IRet)
{
const char *perrormsg = lua_tostring (L,-1);
cout << perrormsg << Endl;
Lua_close (L);
return 0;
}

Get calculated results
IRet = Lua_isnumber (L,-1);
if (!iret)
{
cout << "Error occured." << Endl;
Lua_close (L);
return 0;
}

Double fvalue = Lua_tonumber (L,-1);
cout << "result is" << fvalue << Endl;

The above code calls the following LUA function:

Copy Code code as follows:

--There are parameters, there are return values
function Add (IA, IB)
return IA + IB
End

This simple LUA function doesn't have anything to say about that long section of C + + code. In Lua, functions are the same as normal values, so calling a function in Lua in C + + is divided into the following steps:

Use Lua_getglobal to get the function and then push it into the stack;

If the function has parameters, we need to press the function parameters into the stack in turn;

When these preparations are ready, call Lua_pcall to start calling the function, and after the call completes, the return value is pushed into the stack;

The final fetch return is worth the process needless to say, the call is complete.

Source code here to download.

Summarize

To this article summed up, the total cost of 4 days of spare time, the time spent mainly in the preparation of demo, well, this article presented, I hope to help. If you feel well, you can share this article with more friends. Of course, you can also scan the right-hand side of the page to help me write a better article, that would be excellent.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.