From b7f37f6426c78132d338b032962209bd93771039 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 20 Nov 2023 13:02:29 +1100 Subject: Add a generic lua function binding, alongside properties --- src/lua/bridge.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/lua/bridge.cpp') diff --git a/src/lua/bridge.cpp b/src/lua/bridge.cpp index ba6f50b4..a45f2b27 100644 --- a/src/lua/bridge.cpp +++ b/src/lua/bridge.cpp @@ -110,16 +110,32 @@ static auto new_property_module(lua_State* state) -> int { return 1; } +template +inline constexpr bool always_false_v = false; + auto Bridge::AddPropertyModule( const std::string& name, - std::vector>> props) - -> void { + std::vector>>> + props) -> void { // Create the module (or retrieve it if one with this name already exists) luaL_requiref(&state_, name.c_str(), new_property_module, true); for (const auto& prop : props) { lua_pushstring(&state_, prop.first.c_str()); - bindings_.Register(&state_, prop.second.get()); + std::visit( + [&](auto&& arg) { + using T = std::decay_t; + if constexpr (std::is_same_v) { + bindings_.Register(&state_, arg); + } else if constexpr (std::is_same_v>) { + bindings_.Register(&state_, arg.get()); + } else { + static_assert(always_false_v, "missing case"); + } + }, + prop.second); + lua_settable(&state_, -3); // metatable.propname = property } -- cgit v1.2.3