ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.06k stars 493 forks source link

🛠 Sometimes the conversion of sol::nested wrappers is not perfect. #1502

Closed wzhengsen closed 1 year ago

wzhengsen commented 1 year ago
struct Vector {
    int x;
    int y;
};

template <class Handler>
bool sol_lua_check(sol::types<Vector>, ::lua_State* L, int index,
     Handler&& handler, sol::stack::record& tracking) {
    // It can pass the type check correctly.
    return sol::stack::check<sol::lua_table>(
         L, index, std::forward<Handler>(handler), tracking);
}

Vector sol_lua_get(sol::types<Vector>, ::lua_State* L, int idx,
     sol::stack::record& tracking) {
    // However, it is not possible to convert the type.
    tracking.use(1);
    sol::table t = sol::table(L, idx);
    return { t["x"].get<int>(), t["y"].get<int>() };
}

int main() {
    sol::state lua;
    lua.open_libraries(sol::lib::base);
    lua.script("vectors = { {x = 3, y = 6}, {x = 6,y=3} }");
    auto v = lua["vectors"].get<std::vector<Vector>>();// error.

    return 0;
}