暴露递归结构体作为Lua表

出于历史原因,我正在构建一个在C++中模拟Lua表的结构体。

typedef union Value Value;
typedef struct LuaTable LuaTable;
typedef struct TableValue TableValue;

enum ValueType {
    INT = 0,
    BOOL = 1,
    FLOAT = 2,
    STRING = 3,
    TABLE = 4,
};

struct LuaTable {
    std::vector<TableValue> array;
    std::map<std::string, TableValue> hashmap;
};

union Value {
    int c_int;
    bool c_bool;
    double c_float;
    std::string c_str;
    LuaTable table;
};

struct TableValue {
  ValueType type;
  Value val;
};

我可能会简化它,让一个表只是一个list或者一个map

我想通过LuaJIT FFI以一些高级语法访问这个结构体:

x = c_tbl.a.b.c

就像这样代理

LuaTable l_tbl {...};
...
l_tbl['a']['b']['c']

在我开始考虑懒惰求值(在某个元表中建立前缀),我一般想知道我是否考虑了正确的方法。

最简单的方法是通过解析LuaTable结构来初始化c_tbl。这就是我卡住的地方。是否有一种简单的方法来解析这样的递归结构体?为了减轻解析/遍历困难,我愿意重新设计它。

点赞