Unified lua function calls by template

This commit is contained in:
2020-11-24 23:23:05 +01:00
parent 01488e29d4
commit cc70ee5447
2 changed files with 24 additions and 23 deletions

View File

@@ -254,35 +254,23 @@ int thrawn_publish(lua_State* lua) {
}
void call_thrawn_on_start(LuaService& service) {
lua_getglobal(service.lua, "thrawn_on_start");
if (lua_pcall(service.lua,0, 0, 0) != 0) {
spdlog::error("[{}] Unable to call 'thrawn_on_start'. [Lua: '{}']", service.name, lua_tostring(service.lua,-1));
return;
}
thrawn_call(service, "thrawn_on_start", [&]() {
return 0;
});
}
void call_thrawn_on_update(LuaService& service, TopicState state) {
lua_getglobal(service.lua, "thrawn_on_update");
thrawn_call(service, "thrawn_on_update", [&]() {
push_topic_state_table(service.lua, state);
push_topic_state_table(service.lua, state);
if (lua_pcall(service.lua, 1, 0, 0) != 0) {
spdlog::error("[{}] Unable to call 'thrawn_on_update'. [Lua: '{}']", service.name, lua_tostring(service.lua,-1));
return;
}
return 1;
});
}
void call_thrawn_on_schedule(LuaService& service, std::time_t now, const std::string& callback) {
lua_getglobal(service.lua, callback.c_str());
thrawn_call(service, callback.c_str(), [&]() {
lua_pushinteger(service.lua, now);
lua_pushinteger(service.lua, now);
if (lua_pcall(service.lua, 1, 0, 0) != 0) {
spdlog::error("[{}] Unable to call '{}'. [Lua: '{}']", callback, service.name);
return;
}
return 1;
});
}

View File

@@ -1,5 +1,6 @@
#if !defined(THRAWN_API_H)
#define THRAWN_API_H
#include <spdlog/spdlog.h>
extern "C"
{
@@ -47,4 +48,16 @@ int thrawn_log_light_dimmable(lua_State* lua);
int thrawn_log_light_tw(lua_State* lua);
template<typename F>
void thrawn_call(LuaService& service, const std::string& lua_func, F push_params) {
lua_getglobal(service.lua, lua_func.c_str());
int num_arguments = push_params();
if (lua_pcall(service.lua, num_arguments, 0, 0) != 0) {
spdlog::error("[{}] Unable to call '{}'. [Lua: '{}']", lua_func, service.name);
return;
}
}
#endif // THRAWN_API_H