Merge "init: rename 'Trigger' to 'Event' and convert to std::variant"
This commit is contained in:
commit
d9ebf06117
3 changed files with 34 additions and 68 deletions
|
@ -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<std::string> 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> action) {
|
|||
}
|
||||
|
||||
void ActionManager::QueueEventTrigger(const std::string& trigger) {
|
||||
trigger_queue_.push(std::make_unique<EventTrigger>(trigger));
|
||||
event_queue_.emplace(trigger);
|
||||
}
|
||||
|
||||
void ActionManager::QueuePropertyTrigger(const std::string& name,
|
||||
const std::string& value) {
|
||||
trigger_queue_.push(std::make_unique<PropertyTrigger>(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<BuiltinTrigger>(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 {
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include <map>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "builtins.h"
|
||||
|
@ -41,6 +42,10 @@ class Command {
|
|||
int line_;
|
||||
};
|
||||
|
||||
using EventTrigger = std::string;
|
||||
using PropertyChange = std::pair<std::string, std::string>;
|
||||
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<BuiltinFunction>* 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> 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<std::unique_ptr<Action>> actions_;
|
||||
std::queue<std::unique_ptr<Trigger>> trigger_queue_;
|
||||
std::queue<std::variant<EventTrigger, PropertyChange, BuiltinAction>> event_queue_;
|
||||
std::queue<const Action*> current_executing_actions_;
|
||||
std::size_t current_command_;
|
||||
};
|
||||
|
|
|
@ -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<std::string>& args)
|
|||
static int queue_property_triggers_action(const std::vector<std::string>& args)
|
||||
{
|
||||
ActionManager::GetInstance().QueueBuiltinAction(property_enable_triggers_action, "enable_property_trigger");
|
||||
ActionManager::GetInstance().QueueAllPropertyTriggers();
|
||||
ActionManager::GetInstance().QueueAllPropertyActions();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue