Skip to content

Commit

Permalink
runtime: Add CallbackResult return value to all action operator()
Browse files Browse the repository at this point in the history
BREAKING CHANGE: If you have implemented any Actions yourself,
you will need to make sure the `operator()(const Sync& ...)`
returns `cloe::CallbackResult`. You can use `CallbackResult::Ok`
to achieve the same behavior.
  • Loading branch information
cassava committed Apr 22, 2024
1 parent 1393f53 commit 329933c
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 117 deletions.
5 changes: 3 additions & 2 deletions engine/src/coordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,13 @@ sol::table Coordinator::register_lua_table(const std::string& field) {
return tbl;
}

void Coordinator::execute_trigger(TriggerPtr&& t, const Sync& sync) {
cloe::CallbackResult Coordinator::execute_trigger(TriggerPtr&& t, const Sync& sync) {
logger()->debug("Execute trigger {}", inline_json(*t));
(t->action())(sync, *executer_registrar_);
auto result = (t->action())(sync, *executer_registrar_);
if (!t->is_conceal()) {
history_.emplace_back(HistoryTrigger{sync.time(), std::move(t)});
}
return result;
}

Duration Coordinator::process(const Sync& sync) {
Expand Down
2 changes: 1 addition & 1 deletion engine/src/coordinator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Coordinator {
cloe::TriggerPtr make_trigger(cloe::Source s, const cloe::Conf& c) const;
void queue_trigger(cloe::Source s, const cloe::Conf& c) { queue_trigger(make_trigger(s, c)); }
void queue_trigger(cloe::TriggerPtr&& t);
void execute_trigger(cloe::TriggerPtr&& t, const cloe::Sync& s);
cloe::CallbackResult execute_trigger(cloe::TriggerPtr&& t, const cloe::Sync& s);

// for access to protected methods
friend TriggerRegistrar;
Expand Down
37 changes: 26 additions & 11 deletions engine/src/lua_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

#include "lua_action.hpp"

#include <string>
#include <optional>
#include <string>
#include <utility>

#include <sol/optional.hpp>
Expand Down Expand Up @@ -60,30 +60,45 @@ cloe::TriggerPtr make_trigger_from_lua(cloe::TriggerRegistrar& r, const sol::tab

namespace actions {

void Lua::operator()(const cloe::Sync&, cloe::TriggerRegistrar&) {
cloe::CallbackResult LuaFunction::operator()(const cloe::Sync& sync, cloe::TriggerRegistrar&) {
logger()->trace("Running lua function.");
auto result = func_(std::ref(sync));
if (!result.valid()) {
throw cloe::Error("error executing Lua function: {}", sol::error{result}.what());
}
// Return false from a pinned action to remove it.
if (result.return_count() > 0 && !result.get<bool>()) {
return cloe::CallbackResult::Unpin;
}
return cloe::CallbackResult::Ok;
}

cloe::CallbackResult Lua::operator()(const cloe::Sync&, cloe::TriggerRegistrar&) {
logger()->trace("Running lua script.");
auto result = lua_.script(script_);
if (!result.valid()) {
sol::error err = result;
throw err;
throw cloe::Error("error executing Lua function: {}", sol::error{result}.what());
}
// Return false from a pinned action to remove it.
if (result.return_count() > 0 && !result.get<bool>()) {
return cloe::CallbackResult::Unpin;
}
return cloe::CallbackResult::Ok;
}

void Lua::to_json(cloe::Json& j) const {
j = cloe::Json{
{"script", script_},
{"script", script_},
};
}

cloe::TriggerSchema LuaFactory::schema() const {
static const char* desc = "lua script to execute";
return cloe::TriggerSchema{
this->name(),
this->description(),
cloe::InlineSchema(desc, cloe::JsonType::string, true),
this->name(), this->description(), cloe::InlineSchema(desc, cloe::JsonType::string, true),
cloe::Schema{
{"script", cloe::make_prototype<std::string>("lua script to execute")},
}
};
{"script", cloe::make_prototype<std::string>("lua script to execute")},
}};
}

cloe::ActionPtr LuaFactory::make(const cloe::Conf& c) const {
Expand Down
10 changes: 2 additions & 8 deletions engine/src/lua_action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,7 @@ class LuaFunction : public cloe::Action {
return std::make_unique<LuaFunction>(this->name(), func_);
}

void operator()(const cloe::Sync& sync, cloe::TriggerRegistrar&) override {
logger()->trace("Running lua function.");
auto result = func_(std::ref(sync));
if(!result.valid()) {
throw cloe::Error("error executing Lua function: {}", sol::error{result}.what());
}
}
cloe::CallbackResult operator()(const cloe::Sync& sync, cloe::TriggerRegistrar&) override;

void to_json(cloe::Json& j) const override { j = cloe::Json{}; }

Expand All @@ -65,7 +59,7 @@ class Lua : public cloe::Action {

cloe::ActionPtr clone() const override { return std::make_unique<Lua>(name(), script_, lua_); }

void operator()(const cloe::Sync&, cloe::TriggerRegistrar&) override;
cloe::CallbackResult operator()(const cloe::Sync&, cloe::TriggerRegistrar&) override;

protected:
void to_json(cloe::Json& j) const override;
Expand Down
2 changes: 1 addition & 1 deletion engine/src/utility/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void CommandExecuter::wait_all() {

namespace actions {

void Command::operator()(const cloe::Sync&, cloe::TriggerRegistrar&) { executer_->run(command_); }
cloe::CallbackResult Command::operator()(const cloe::Sync&, cloe::TriggerRegistrar&) { executer_->run(command_); return cloe::CallbackResult::Ok; }

void Command::to_json(cloe::Json& j) const { command_.to_json(j); }

Expand Down
2 changes: 1 addition & 1 deletion engine/src/utility/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Command : public cloe::Action {
return std::make_unique<Command>(name(), command_, executer_);
}

void operator()(const cloe::Sync&, cloe::TriggerRegistrar&) override;
cloe::CallbackResult operator()(const cloe::Sync&, cloe::TriggerRegistrar&) override;

protected:
void to_json(cloe::Json& j) const override;
Expand Down
3 changes: 2 additions & 1 deletion optional/vtd/src/scp_action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ class ScpAction : public cloe::Action {
ScpAction(const std::string& name, std::shared_ptr<ScpTransceiver> scp_client, const std::string& msg)
: cloe::Action(name), client_(scp_client), xml_(msg) {}
cloe::ActionPtr clone() const override { return std::make_unique<ScpAction>(name(), client_, xml_); }
void operator()(const cloe::Sync&, cloe::TriggerRegistrar&) override {
cloe::CallbackResult operator()(const cloe::Sync&, cloe::TriggerRegistrar&) override {
logger()->info("Sending SCP message: {}", xml_);
client_->send(xml_);
return cloe::CallbackResult::Ok;
}
bool is_significant() const override { return false; }

Expand Down
2 changes: 1 addition & 1 deletion plugins/basic/src/hmi_contact.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class UseContact : public Action {
UseContact(const std::string& name, ContactMap<D>* m, const Conf& data)
: Action(name), hmi_(m), data_(data) {}
ActionPtr clone() const override { return std::make_unique<UseContact<D>>(name(), hmi_, data_); }
void operator()(const Sync&, TriggerRegistrar&) override { from_json(*data_, *hmi_); }
CallbackResult operator()(const Sync&, TriggerRegistrar&) override { from_json(*data_, *hmi_); return CallbackResult::Ok; }

protected:
void to_json(Json& j) const override { j = *data_; }
Expand Down
5 changes: 4 additions & 1 deletion runtime/include/cloe/conf/action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class Configure : public Action {
conf_.erase("name");
}
ActionPtr clone() const override { return std::make_unique<Configure>(name(), ptr_, conf_); }
void operator()(const Sync&, TriggerRegistrar&) override { ptr_->from_conf(conf_); }
CallbackResult operator()(const Sync&, TriggerRegistrar&) override {
ptr_->from_conf(conf_);
return CallbackResult::Ok;
}

protected:
void to_json(Json& j) const override { j = *conf_; }
Expand Down
5 changes: 4 additions & 1 deletion runtime/include/cloe/registrar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class DirectCallback : public Callback {
auto& condition = dynamic_cast<E&>((*it)->event());
if (condition(sync, args...)) {
if ((*it)->is_sticky()) {
this->execute((*it)->clone(), sync);
auto result = this->execute((*it)->clone(), sync);
if (result == CallbackResult::Unpin) {
it = triggers_.erase(it);
}
} else {
// Remove from trigger list and advance.
this->execute(std::move(*it), sync);
Expand Down
14 changes: 11 additions & 3 deletions runtime/include/cloe/trigger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,18 @@ class Event : public Entity {
using EventFactory = TriggerFactory<Event>;
using EventFactoryPtr = std::unique_ptr<EventFactory>;

enum class CallbackResult {
/// Default, use standard behavior.
Ok,

/// Remove from callback if it was sticky.
Unpin,
};

/**
* Interface the trigger manager must provide for executing triggers.
*/
using CallbackExecuter = std::function<void(TriggerPtr&&, const Sync&)>;
using CallbackExecuter = std::function<CallbackResult(TriggerPtr&&, const Sync&)>;

/**
* Callback provides the interface with which the global trigger manager,
Expand Down Expand Up @@ -559,7 +567,7 @@ class Callback {
* Execute a trigger in the given sync context by passing it to the
* executer.
*/
void execute(TriggerPtr&& t, const Sync& s);
CallbackResult execute(TriggerPtr&& t, const Sync& s);

private:
CallbackExecuter executer_;
Expand Down Expand Up @@ -623,7 +631,7 @@ class Action : public Entity {
/**
* Execute the action.
*/
virtual void operator()(const Sync&, TriggerRegistrar&) = 0;
virtual CallbackResult operator()(const Sync&, TriggerRegistrar&) = 0;

/**
* Return whether this action is a significant action.
Expand Down
11 changes: 7 additions & 4 deletions runtime/include/cloe/trigger/example_actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class Log : public Action {
Log(const std::string& name, LogLevel level, const std::string& msg)
: Action(name), level_(level), msg_(msg) {}
ActionPtr clone() const override { return std::make_unique<Log>(name(), level_, msg_); }
void operator()(const Sync&, TriggerRegistrar&) override { logger()->log(level_, msg_.c_str()); }
CallbackResult operator()(const Sync&, TriggerRegistrar&) override {
logger()->log(level_, msg_.c_str());
return CallbackResult::Ok;
}
bool is_significant() const override { return false; }

protected:
Expand Down Expand Up @@ -71,7 +74,7 @@ class Bundle : public Action {
public:
Bundle(const std::string& name, std::vector<ActionPtr>&& actions);
ActionPtr clone() const override;
void operator()(const Sync& s, TriggerRegistrar& r) override;
CallbackResult operator()(const Sync& s, TriggerRegistrar& r) override;
bool is_significant() const override;

protected:
Expand Down Expand Up @@ -103,7 +106,7 @@ class Insert : public Action {
public:
Insert(const std::string& name, const Conf& triggers) : Action(name), triggers_(triggers) {}
ActionPtr clone() const override { return std::make_unique<Insert>(name(), triggers_); }
void operator()(const Sync& s, TriggerRegistrar& r) override;
CallbackResult operator()(const Sync& s, TriggerRegistrar& r) override;

protected:
void to_json(Json& j) const override;
Expand Down Expand Up @@ -138,7 +141,7 @@ class PushRelease : public Action {
return std::make_unique<PushRelease>(name(), duration_, push_->clone(), release_->clone(),
repr_);
}
void operator()(const Sync&, TriggerRegistrar&) override;
CallbackResult operator()(const Sync&, TriggerRegistrar&) override;

protected:
void to_json(Json& j) const override { j = repr_; }
Expand Down
Loading

0 comments on commit 329933c

Please sign in to comment.