From cc70ee54472750936e8b58697e96f183908575c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Julian-Steffen=20M=C3=BCller?= Date: Tue, 24 Nov 2020 23:23:05 +0100 Subject: [PATCH] Unified lua function calls by template --- src/thrawn_api.cpp | 34 +++++++++++----------------------- src/thrawn_api.h | 13 +++++++++++++ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/thrawn_api.cpp b/src/thrawn_api.cpp index 820ed47..371c0b1 100644 --- a/src/thrawn_api.cpp +++ b/src/thrawn_api.cpp @@ -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; + }); } \ No newline at end of file diff --git a/src/thrawn_api.h b/src/thrawn_api.h index acd5b67..0857f0f 100644 --- a/src/thrawn_api.h +++ b/src/thrawn_api.h @@ -1,5 +1,6 @@ #if !defined(THRAWN_API_H) #define THRAWN_API_H +#include extern "C" { @@ -47,4 +48,16 @@ int thrawn_log_light_dimmable(lua_State* lua); int thrawn_log_light_tw(lua_State* lua); +template +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 \ No newline at end of file