介绍下Lua做为c++程序的配置文件的用法
大家可能习惯了用xml作为配置文件,其实很多时候我们也可以使用脚本作为程序的配置文件,这样程序既可以添加入脚本
又可以使用配置文件,作为一种脚本语言做配置文件自然可以,用lua的好处有几个方面,
1.是它天生和c++程序结合非常好。
2.这样可以减少整个工程的编译次数(所有配置文件共同的好处)
3.容易进行跨平台的移植
4.lua的解释器非常小,不会使整个工程的体积变得臃肿
最常用的方式就是将要配置的东西放入table中。下面的代码是从table中读取数据的方法!如果有更好的可以提供出来一起分享一下!
最后附上读取配置文件的常用源代码:
[plain] view plain copy <span >#ifndef READLUAVALUE_H #define READLUAVALUE_H #include <string> #include <lua.hpp> using namespace std; class LuaReadtable { public: LuaReadtable(){} ~LuaReadtable(){} voID ReadtableFromKey_Int(lua_State *L, const char* lpsztablename, const char* lpsztableItem,int &int_value); voID ReadtableFromIndex_Int(lua_State *L, int index,int &int_value); voID ReadtableFromKey_String(lua_State *L,string &string_value); voID ReadtableFromIndex_String(lua_State *L,string &string_value); voID ReadtableFromKey_Double(lua_State *L,double &double_value); voID ReadtableFromIndex_Double(lua_State *L,double &double_value); voID ReadtableFromKey_UnsignedLong(lua_State *L,unsigned long &unsigned_long_value); voID ReadtableFromIndex_UnsignedLong(lua_State *L,unsigned long &unsigned_long_value); voID WritetableFromKey_Int(lua_State *L, int int_value); voID WritetableFromIndex_Int(lua_State *L, int int_value); voID WritetableFromKey_String(lua_State *L, string string_value); voID WritetableFromIndex_String(lua_State *L, string string_value); voID WritetableFromKey_Double(lua_State *L, double double_value); voID WritetableFromIndex_Double(lua_State *L, double double_value); voID WritetableFromKey_UnsignedLong(lua_State *L, unsigned long unsigned_long_value); voID WritetableFromIndex_UnsignedLong(lua_State *L, unsigned long unsigned_long_value); voID GettableLenth(lua_State *L,int &length); }; #endif // !READLUAVALUE_H </span>[plain] view plain copy <span >#include "ReadAnDWriteLuatableValue.h" voID LuaReadtable::ReadtableFromKey_Int(lua_State *L,int &int_value) { lua_getglobal(L, lpsztablename); lua_pushstring(L, lpsztableItem); lua_gettable(L, -2); //printf("%s.%s=%d\n", lpsztablename, lpsztableItem, (int)lua_tonumber(L, -1)); int_value = (int)lua_tonumber(L, -1); lua_pop(L, 2); } voID LuaReadtable::ReadtableFromIndex_Int(lua_State *L,int &int_value) { lua_getglobal(L, lpsztablename); lua_rawgeti(L, -1, index); int_value = (int)lua_tonumber(L, -1); lua_pop(L, 2); } voID LuaReadtable::ReadtableFromKey_String(lua_State *L,string &string_value) { lua_getglobal(L, -2); string_value = lua_tostring(L, 2); } voID LuaReadtable::ReadtableFromIndex_String(lua_State *L,string &string_value) { lua_getglobal(L, lpsztablename); lua_getglobal(L, index); string_value = lua_tostring(L, 2); } voID LuaReadtable::ReadtableFromKey_Double(lua_State *L,double &double_value) { lua_getglobal(L, lpsztablename); lua_pushstring(L, lpsztableItem); lua_gettable(L, -2); double_value = (double)lua_tonumber(L, 2); } voID LuaReadtable::ReadtableFromIndex_Double(lua_State *L,double &double_value) { lua_getglobal(L, lpsztablename); lua_rawgeti(L, index); double_value = (double)lua_tonumber(L, 2); } voID LuaReadtable::ReadtableFromKey_UnsignedLong(lua_State *L,unsigned long &unsigned_long_value) { lua_getglobal(L, lpsztablename); lua_pushstring(L, lpsztableItem); lua_gettable(L, -2); unsigned_long_value = (unsigned long)lua_tonumber(L, 2); } voID LuaReadtable::ReadtableFromIndex_UnsignedLong(lua_State *L, index); unsigned_long_value = (unsigned long)lua_tonumber(L, 2); } voID LuaReadtable::WritetableFromKey_Int(lua_State *L, int int_value) { lua_getglobal(L, lpsztablename); lua_pushnumber(L, int_value); lua_setfIEld(L, -2, lpsztableItem); lua_pop(L, 1); } voID LuaReadtable::WritetableFromIndex_Int(lua_State *L, int int_value) { lua_getglobal(L, lpsztablename); lua_pushnumber(L, int_value); lua_rawseti(L, index); lua_pop(L, 1); } voID LuaReadtable::WritetableFromKey_String(lua_State *L, string string_value) { lua_getglobal(L, string_value.c_str()); lua_setfIEld(L, 1); } voID LuaReadtable::WritetableFromIndex_String(lua_State *L, string string_value) { lua_getglobal(L, string_value.c_str()); lua_rawseti(L, 1); } voID LuaReadtable::WritetableFromKey_Double(lua_State *L, double double_value) { lua_getglobal(L, double_value); lua_setfIEld(L, 1); } voID LuaReadtable::WritetableFromIndex_Double(lua_State *L, double double_value) { lua_getglobal(L, double_value); lua_rawseti(L, 1); } voID LuaReadtable::WritetableFromKey_UnsignedLong(lua_State *L, unsigned long unsigned_long_value) { lua_getglobal(L, unsigned_long_value); lua_setfIEld(L, 1); } voID LuaReadtable::WritetableFromIndex_UnsignedLong(lua_State *L, unsigned long unsigned_long_value) { lua_getglobal(L, unsigned_long_value); lua_rawseti(L, 1); } voID LuaReadtable::GettableLenth(lua_State *L,int &length) { lua_getglobal(L, lpsztablename); length=lua_rawlen(L,-1); lua_pop(L, 1); }</span> 总结
以上是内存溢出为你收集整理的Lua做为c++程序的配置文件全部内容,希望文章能够帮你解决Lua做为c++程序的配置文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)