summaryrefslogtreecommitdiff
path: root/src/lua/bridge.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-11-20 13:02:29 +1100
committerjacqueline <me@jacqueline.id.au>2023-11-20 13:02:29 +1100
commitb7f37f6426c78132d338b032962209bd93771039 (patch)
treefd9e097ed55167616e630c257a28724b0f1ddc63 /src/lua/bridge.cpp
parentb3b512f10e0570f7dc8a04e1613f1234e5532728 (diff)
downloadtangara-fw-b7f37f6426c78132d338b032962209bd93771039.tar.gz
Add a generic lua function binding, alongside properties
Diffstat (limited to 'src/lua/bridge.cpp')
-rw-r--r--src/lua/bridge.cpp22
1 files changed, 19 insertions, 3 deletions
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 <class... Ts>
+inline constexpr bool always_false_v = false;
+
auto Bridge::AddPropertyModule(
const std::string& name,
- std::vector<std::pair<std::string, std::shared_ptr<Property>>> props)
- -> void {
+ std::vector<std::pair<std::string,
+ std::variant<LuaFunction, std::shared_ptr<Property>>>>
+ 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<decltype(arg)>;
+ if constexpr (std::is_same_v<T, LuaFunction>) {
+ bindings_.Register(&state_, arg);
+ } else if constexpr (std::is_same_v<T, std::shared_ptr<Property>>) {
+ bindings_.Register(&state_, arg.get());
+ } else {
+ static_assert(always_false_v<T>, "missing case");
+ }
+ },
+ prop.second);
+
lua_settable(&state_, -3); // metatable.propname = property
}