加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

将lua文件中的简单值转换为c的通用解决方案

发布时间:2020-12-14 21:44:50 所属栏目:大数据 来源:网络整理
导读:我正在尝试使用Lua文件作为配置或ini.我成功了,但我的解决方案让我感到烦恼.具体来说,get_double,get_int和get_string函数需要以可重用的方式完成. 我遇到了创建没有参数的函数模板的问题. 另外,我不确定如何概括lua_is …和lua_to ….我的想法是给我们if(is
我正在尝试使用Lua文件作为配置或ini.我成功了,但我的解决方案让我感到烦恼.具体来说,get_double,get_int和get_string函数需要以可重用的方式完成.

我遇到了创建没有参数的函数模板的问题.
另外,我不确定如何概括lua_is …和lua_to ….我的想法是给我们if(is_same< T,double> :: value)return(double)lua_isnumber(L,-1);但它不起作用.

这是工作代码:

main.cc:

#include <iostream>
#include <lua.hpp>

using namespace std;

class Lua_vm 
{
private:
  lua_State *L;
public:
  double get_double(const char *var_name) {
    lua_getglobal(L,var_name);
    if (!lua_isnumber(L,-1)) {
      cout << "error: " << var_name << " is of a wrong typen";
    }
    return (double)lua_tonumber(L,-1);
    lua_pop(L,1);
  }
  int get_int(const char *var_name) {
    lua_getglobal(L,-1)) {
      cout << "error: " << var_name << " is of a wrong typen";
    }
    return (int)lua_tonumber(L,1);
  }
  string get_string(const char *var_name) {
    lua_getglobal(L,var_name);
    if (!lua_isstring(L,-1)) {
      cout << "error: " << var_name << " is of a wrong typen";
    }
    return string(lua_tostring(L,-1));
    lua_pop(L,1);
  }
  Lua_vm(const char *lua_config_filename) {
    L = lua_open();
    if (luaL_loadfile(L,lua_config_filename) || lua_pcall(L,0)) {
      cout << "error: " << lua_tostring(L,-1) << "n";
    }
    lua_pushnil(L);
  }
  ~Lua_vm() {
    lua_close(L);
  }
};
int main(int argc,char** argv)
{
  Lua_vm lvm("config.lua");

  cout << "vol is " << lvm.get_double("vol") << "n";
  cout << "rho is " << lvm.get_int("rho") << "n";
  cout << "method is " << lvm.get_string("method") << "n";

  return 0;
}

config.lua:

method = "cube"
len = 3.21
rho = 13
vol = len*len*len
mass = vol*rho

它用g main编译.C -I /usr/include / lua5.1 / -llua5.1; ./a.out

解决方法

这是我用于此目的的代码(更多: http://tom.rethaller.net/wiki/projects/prologue/lua)

class LuaState
{
private:
    lua_State *L;
    // [...]    

public:
    // get function
    template<typename T>
    T get(const char * varname) {
      char temp[64];
      memset(temp,sizeof(temp));
      int i=0;
      int j=0;
      int n=0;
      while (varname[i] != '') {
        char c = varname[i];
        if (c == '.') {
          if (n == 0)
            lua_getglobal(L,temp);
          else
            lua_getfield(L,-1,temp);
          ++n;
          memset(temp,sizeof(temp));
          j = 0;

          if (lua_isnil(L,-1))
            return 0;
        }
        else {
          temp[j] = c;
          ++j;
        }
        ++i;
      }
      if (n == 0)
        lua_getglobal(L,temp);
      else
        lua_getfield(L,temp);
      return lua_get<T>();
    }

    // Generic get
    template<typename T>
    T lua_get() {
      return 0;
    }

    // Specializations
    template <> float lua_get<float>() {
      return lua_tonumber(L,-1);
    }
    template <> double lua_get<double>() {
      return lua_tonumber(L,-1);
    }
    template <> bool lua_get<bool>() {
      return lua_toboolean(L,-1);
    }
    template <> int lua_get<int>() {
      return lua_tointeger(L,-1);
    }
    template <> unsigned int lua_get<unsigned int>() {
      return (unsigned int)lua_tonumber(L,-1);
    }
    template <> const char * lua_get<const char *>() {
      return lua_tostring(L,-1);
    }
};

– 编辑 – 为了说明如何使用它,你只需要打电话:

bool use_blur = lua_state.get<bool>("config.render.effects.blur");

得到的价值:

config = {
    render = {
        effects = {
            blur = false,}
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读