Lua做为c++程序的配置文件
大家可能习惯了用xml作为配置文件,其实很多时候我们也可以使用脚本作为程序的配置文件,这样程序既可以添加入脚本
又可以使用配置文件,作为一种脚本语言做配置文件自然可以,用lua的好处有几个方面,
1.是它天生和c++程序结合非常好。
2.这样可以减少整个工程的编译次数(所有配置文件共同的好处)
3.容易进行跨平台的移植
4.lua的解释器非常小,不会使整个工程的体积变得臃肿
最常用的方式就是将要配置的东西放入table中。下面的代码是从table中读取数据的方法!如果有更好的可以提供出来一起分享一下!
- <span style="font-size:14px;">#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>
- <span style="font-size:14px;">#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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。