Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie
Discuss newsgroups and documents
Preface
Take the jsoncpp library as an example in data/configuration storage method JSON
"Data/configuration storage method JSON" take cocos2d for iPhone + touchjson as an Example
", I summarized how to use JSON as the configuration, but in fact, although the JSON syntax format is simple, and the use of the parsing library is also very simple, but because of this simplicity, some features are missing. For example, the reference of another configuration line in the configuration file, such as the inheritance of the configuration segment, can be solved using XML. In addition, sometimes you need to perform simple operations in the configuration file, for example, I often want to scale an image to a fraction of the current resolution, it is too tempting to use a complete language that can be used for computation to configure ......
Lua is born for this ...... Although now Lua has far more functions than this, the purpose of the initial design of Lua is a simple configuration language. See Lua's history
. Although it is actually feasible to use python as the configuration, Lua has the advantage of high speed, and Lua is much smaller than python, so it is suitable for embedding it into programs.
When Lua is used as the configuration, it is actually equivalent to embedding Lua in C ++, but it does not need to use some complex features of the scripting language, just as a configuration file.
Here we take the original data/configuration storage method JSON as an example to use the jsoncpp Library
The SDL project started in this article is used as an example.
First, build a C ++ compiling environment that can be embedded with Lua and download a luabinaries
Now I use lua5_00004_win32_dll8_lib.zip.
. Here I don't even need a Lua interactive environment that can run .....
Simple Example
First, create a simple Lua file named picture. Lua.
Name = "dragon.png"
Rotation= 180
Although there are only two simple lines, it is also a legal Lua program.
And then read it out in the C ++ program. This process actually uses the Lua c api. The specific Lua c api is not described in detail here, which is complicated, you can refer to programming in Lua. The Chinese version is also available. Here is just an example for reference.
Now you can start to complete the C ++ program:
First, include the necessary header files:
Extern "C "{
# Include "Lua/Lua. H"
# Include "Lua/lauxlib. H"
# Include "Lua/lualib. H"
}
Because the Lua header file only takes into account the C language, you must add extern "C" when using it in C ++ ". This is a typical attitude toward C ++, because most of the libraries written in pure C language will naturally use the extern "C" statement automatically through macro judgment.
Then:
Using
Namespace
STD;
Struct
Pictureinfo {
String name;
Float
Rotation;
} Gpictureinfo;
Void
Pictureinit (){
Lua_state * l = lual_newstate ();
If
(Lual_dofile (L, "picture. Lua"
)! = 0
){
Printf ("error happen ."
);
// Handle the error.
Exit (1
);
}
Lua_getglobal (L, "name"
);
Assert (lua_isstring (L,-1
) = 1
);
Gpictureinfo. Name = lua_tostring (L,-1
);
Lua_getglobal (L, "rotation"
);
Gpictureinfo. Rotation = (float
) Lua_tonumber (L,-1
);
Lua_close (L );
}
All the variables we use here are global variables, so it is very convenient to call them. The main content above is the use of Lua c api. It is not clear here because of its complexity.
The basic process is to create a new Lua state and then use lual_dofile
Execute the required configuration file and call lua_getglobal
Add appropriate conversions to obtain the required configuration values. The example here is a string and a floating point number.
For more information about the displayed results and other code, see data/configuration storage method JSON. Use the jsoncpp library as an example.
.
Array
The array in Lua is also expressed in the form of table. Here I will only introduce how to obtain the configuration from the Lua file, and the specific display and other things will not be detailed.
Here, we create a Lua file containing an array as the Configuration:
Data = {
{
Name = "dragon.png"
, Rotation = 180
}
,
{
Name = "dragon.png"
, Rotation = 0
}
}
The following describes how to read the Configuration:
Using
Namespace
STD;
Struct
Pictureinfo {
String name;
Float
Rotation;
};
Vector gpictureinfovec;
Void
Pictureinit (){
Lua_state * l = lual_newstate ();
If
(Lual_dofile (L, "picture. Lua"
)! = 0
){
Printf ("error happen ."
);
// Handle the error.
Exit (1
);
}
// Get the table
Lua_getglobal (L, "data"
);
Assert (lua_istable (L,-1
) = 1
);
/*
Table is in the stack at index 'T'
*/
Lua_pushnil (l );/*
First key
*/
While
(Lua_next (L,-2
)! = 0
){
Pictureinfo Info;
/*
'Key' (at index-2) and 'value' (at index-1)
*/
// Push the key to stack for getting the value
Lua_pushstring (L, "name"
);
// Now the table is in the-2 and key in the top (-1)
Lua_gettable (L,-2
);
Assert (lua_isstring (L,-1
));
Info. Name = lua_tostring (L,-1
);
Lua_pop (L, 1
);
// Push the key to stack for getting the value
Lua_pushstring (L, "rotation"
);
// Now the table is in the-2 and key in the top (-1)
Lua_gettable (L,-2
);
Assert (lua_isnumber (L,-1
));
Info. Rotation = lua_tonumber (L,-1
);
Gpictureinfovec. push_back (Info );
/*
Removes the key we pushed and the 'value' of the global table; keeps 'key' for next iteration
*/
Lua_pop (L, 2
);
}
Lua_close (L );
}
The comments in the Code are enough to be explained in detail, but because the Lua API is really not easy to understand, it cannot be explained clearly here, so it is better to first understand the Lua API, for more information about the API, see the reference manual.
.
The above only uses the Lua API to traverse the array and obtain elements from the table.
If we only have the above, we cannot see the advantages of using Lua as the configuration, the advantage of configuring with Lua is that you can use the features of Lua to implement inheritance between configuration segments and complete operation functions.
For example, you do not need to modify the code for reading the configuration above. We only need to modify the configuration to see how to use the above functions in Lua:
Data1 = {
Name = "dragon.png"
, Rotation = 180
}
Data2 = {
Name = data1.name, rotation = data1.rotation/2
}
Data = {
Data1, data2
}
In this case, the data of data2 is completely dependent on the data of data1. When there is anything that needs to be modified, you only need to modify one place. For the configuration, don't repeat yourself is also very meaningful. The most important thing is that it is very powerful to perform computation in the configuration. Even if you do not need Lua's function to interact with the code, you only need to perform Lua's computation, you can also complete the layout of all Sprite ......
Summary
Compared with XML and JSON, Lua is definitely the most powerful configuration ...... As a complete language, it has all the functions you want to implement in the configuration. But the disadvantages are also obvious:
First of all, in terms of speed, Lua needs to explain the operation, which may obviously not keep up with the XML or JSON parsing speed. However, the parsing and reading of configurations can be put in the initialization phase, so it is not too difficult to accept when there are not many configurations. In addition, you can also use the Lua configuration as a mechanism in the development phase, and completely convert it into binary data after release.
Second, you need to manually call the Lua API to read the Lua configuration. Compared with XML, the JSON type has a very convenient library, which is still troublesome to use, in particular, the use of Lua APIS is not so simple and intuitive. However, this is not insurmountable. You can write a small library to encapsulate the Lua API to form a library similar to jsoncpp and use map to represent everything. This may require limiting some Lua syntax or making a trade-off during parsing. For example, the function may need to be filtered out. Otherwise, it is not like using Lua as a configuration.
In addition, the Lua configuration is not generated in XML, and JSON is convenient with the full library support. This cannot be overcome at the moment, writing a database that automatically generates the Lua configuration file is not too easy. In addition, the advantage of Lua in automatic generation is not available, so it is better to use JSON.
In general, if there is no tool and a large number of hand-written configurations are required, it would be nice to encapsulate the Lua API and then use Lua for configuration, when writing a script, you will feel the benefits of a complete language as a configuration.
The author of the original article retains the copyright reprinted. Please indicate the original author and give a link
Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie