Unified lua function calls by template
This commit is contained in:
@@ -254,35 +254,23 @@ int thrawn_publish(lua_State* lua) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void call_thrawn_on_start(LuaService& service) {
|
void call_thrawn_on_start(LuaService& service) {
|
||||||
lua_getglobal(service.lua, "thrawn_on_start");
|
thrawn_call(service, "thrawn_on_start", [&]() {
|
||||||
|
return 0;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void call_thrawn_on_update(LuaService& service, TopicState state) {
|
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);
|
return 1;
|
||||||
|
});
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void call_thrawn_on_schedule(LuaService& service, std::time_t now, const std::string& callback) {
|
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);
|
return 1;
|
||||||
|
});
|
||||||
if (lua_pcall(service.lua, 1, 0, 0) != 0) {
|
|
||||||
spdlog::error("[{}] Unable to call '{}'. [Lua: '{}']", callback, service.name);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
#if !defined(THRAWN_API_H)
|
#if !defined(THRAWN_API_H)
|
||||||
#define THRAWN_API_H
|
#define THRAWN_API_H
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
@@ -47,4 +48,16 @@ int thrawn_log_light_dimmable(lua_State* lua);
|
|||||||
|
|
||||||
int thrawn_log_light_tw(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
|
#endif // THRAWN_API_H
|
||||||
Reference in New Issue
Block a user