diff --git a/init/action.cpp b/init/action.cpp index dd03017ab..21abe0273 100644 --- a/init/action.cpp +++ b/init/action.cpp @@ -199,17 +199,19 @@ bool Action::CheckPropertyTriggers(const std::string& name, return found; } -bool Action::CheckEventTrigger(const std::string& trigger) const { - return !event_trigger_.empty() && - trigger == event_trigger_ && - CheckPropertyTriggers(); +bool Action::CheckEvent(const EventTrigger& event_trigger) const { + return event_trigger == event_trigger_ && CheckPropertyTriggers(); } -bool Action::CheckPropertyTrigger(const std::string& name, - const std::string& value) const { +bool Action::CheckEvent(const PropertyChange& property_change) const { + const auto& [name, value] = property_change; return event_trigger_.empty() && CheckPropertyTriggers(name, value); } +bool Action::CheckEvent(const BuiltinAction& builtin_action) const { + return this == builtin_action; +} + std::string Action::BuildTriggersString() const { std::vector triggers; @@ -233,41 +235,6 @@ void Action::DumpState() const { } } -class EventTrigger : public Trigger { -public: - explicit EventTrigger(const std::string& trigger) : trigger_(trigger) { - } - bool CheckTriggers(const Action& action) const override { - return action.CheckEventTrigger(trigger_); - } -private: - const std::string trigger_; -}; - -class PropertyTrigger : public Trigger { -public: - PropertyTrigger(const std::string& name, const std::string& value) - : name_(name), value_(value) { - } - bool CheckTriggers(const Action& action) const override { - return action.CheckPropertyTrigger(name_, value_); - } -private: - const std::string name_; - const std::string value_; -}; - -class BuiltinTrigger : public Trigger { -public: - explicit BuiltinTrigger(Action* action) : action_(action) { - } - bool CheckTriggers(const Action& action) const override { - return action_ == &action; - } -private: - const Action* action_; -}; - ActionManager::ActionManager() : current_command_(0) { } @@ -281,16 +248,15 @@ void ActionManager::AddAction(std::unique_ptr action) { } void ActionManager::QueueEventTrigger(const std::string& trigger) { - trigger_queue_.push(std::make_unique(trigger)); + event_queue_.emplace(trigger); } -void ActionManager::QueuePropertyTrigger(const std::string& name, - const std::string& value) { - trigger_queue_.push(std::make_unique(name, value)); +void ActionManager::QueuePropertyChange(const std::string& name, const std::string& value) { + event_queue_.emplace(std::make_pair(name, value)); } -void ActionManager::QueueAllPropertyTriggers() { - QueuePropertyTrigger("", ""); +void ActionManager::QueueAllPropertyActions() { + QueuePropertyChange("", ""); } void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) { @@ -303,19 +269,20 @@ void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& action->AddCommand(func, name_vector, 0); - trigger_queue_.push(std::make_unique(action.get())); + event_queue_.emplace(action.get()); actions_.emplace_back(std::move(action)); } void ActionManager::ExecuteOneCommand() { - // Loop through the trigger queue until we have an action to execute - while (current_executing_actions_.empty() && !trigger_queue_.empty()) { + // Loop through the event queue until we have an action to execute + while (current_executing_actions_.empty() && !event_queue_.empty()) { for (const auto& action : actions_) { - if (trigger_queue_.front()->CheckTriggers(*action)) { + if (std::visit([&action](const auto& event) { return action->CheckEvent(event); }, + event_queue_.front())) { current_executing_actions_.emplace(action.get()); } } - trigger_queue_.pop(); + event_queue_.pop(); } if (current_executing_actions_.empty()) { @@ -349,7 +316,7 @@ void ActionManager::ExecuteOneCommand() { } bool ActionManager::HasMoreCommands() const { - return !current_executing_actions_.empty() || !trigger_queue_.empty(); + return !current_executing_actions_.empty() || !event_queue_.empty(); } void ActionManager::DumpState() const { diff --git a/init/action.h b/init/action.h index c528a7cc2..d006c50c6 100644 --- a/init/action.h +++ b/init/action.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "builtins.h" @@ -41,6 +42,10 @@ class Command { int line_; }; +using EventTrigger = std::string; +using PropertyChange = std::pair; +using BuiltinAction = class Action*; + class Action { public: explicit Action(bool oneshot, const std::string& filename, int line); @@ -52,9 +57,9 @@ class Action { std::size_t NumCommands() const; void ExecuteOneCommand(std::size_t command) const; void ExecuteAllCommands() const; - bool CheckEventTrigger(const std::string& trigger) const; - bool CheckPropertyTrigger(const std::string& name, - const std::string& value) const; + bool CheckEvent(const EventTrigger& event_trigger) const; + bool CheckEvent(const PropertyChange& property_change) const; + bool CheckEvent(const BuiltinAction& builtin_action) const; std::string BuildTriggersString() const; void DumpState() const; @@ -81,12 +86,6 @@ private: static const KeywordMap* function_map_; }; -class Trigger { -public: - virtual ~Trigger() { } - virtual bool CheckTriggers(const Action& action) const = 0; -}; - class ActionManager { public: static ActionManager& GetInstance(); @@ -96,8 +95,8 @@ class ActionManager { void AddAction(std::unique_ptr action); void QueueEventTrigger(const std::string& trigger); - void QueuePropertyTrigger(const std::string& name, const std::string& value); - void QueueAllPropertyTriggers(); + void QueuePropertyChange(const std::string& name, const std::string& value); + void QueueAllPropertyActions(); void QueueBuiltinAction(BuiltinFunction func, const std::string& name); void ExecuteOneCommand(); bool HasMoreCommands() const; @@ -108,7 +107,7 @@ class ActionManager { void operator=(ActionManager const&) = delete; std::vector> actions_; - std::queue> trigger_queue_; + std::queue> event_queue_; std::queue current_executing_actions_; std::size_t current_command_; }; diff --git a/init/init.cpp b/init/init.cpp index e91faea52..99ce5e692 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -164,8 +164,8 @@ void property_changed(const std::string& name, const std::string& value) { // waiting on a property. if (name == "sys.powerctl") HandlePowerctlMessage(value); - if (property_triggers_enabled) - ActionManager::GetInstance().QueuePropertyTrigger(name, value); + if (property_triggers_enabled) ActionManager::GetInstance().QueuePropertyChange(name, value); + if (waiting_for_prop) { if (wait_prop_name == name && wait_prop_value == value) { wait_prop_name.clear(); @@ -535,7 +535,7 @@ static int property_enable_triggers_action(const std::vector& args) static int queue_property_triggers_action(const std::vector& args) { ActionManager::GetInstance().QueueBuiltinAction(property_enable_triggers_action, "enable_property_trigger"); - ActionManager::GetInstance().QueueAllPropertyTriggers(); + ActionManager::GetInstance().QueueAllPropertyActions(); return 0; }