Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _INIT_ACTION_H |
| 18 | #define _INIT_ACTION_H |
| 19 | |
| 20 | #include <map> |
| 21 | #include <queue> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 25 | #include "builtins.h" |
| 26 | #include "init_parser.h" |
| 27 | #include "keyword_map.h" |
| 28 | |
| 29 | class Command { |
| 30 | public: |
| 31 | Command(BuiltinFunction f, const std::vector<std::string>& args, |
| 32 | const std::string& filename, int line); |
| 33 | |
| 34 | int InvokeFunc() const; |
| 35 | std::string BuildCommandString() const; |
| 36 | std::string BuildSourceString() const; |
| 37 | |
| 38 | private: |
| 39 | BuiltinFunction func_; |
| 40 | std::vector<std::string> args_; |
| 41 | std::string filename_; |
| 42 | int line_; |
| 43 | }; |
| 44 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 45 | class Action { |
| 46 | public: |
Chih-Hung Hsieh | 034c475 | 2016-07-12 13:50:44 -0700 | [diff] [blame] | 47 | explicit Action(bool oneshot = false); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 48 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 49 | bool AddCommand(const std::vector<std::string>& args, |
| 50 | const std::string& filename, int line, std::string* err); |
| 51 | void AddCommand(BuiltinFunction f, |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 52 | const std::vector<std::string>& args, |
| 53 | const std::string& filename = "", int line = 0); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 54 | void CombineAction(const Action& action); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 55 | bool InitTriggers(const std::vector<std::string>& args, std::string* err); |
| 56 | bool InitSingleTrigger(const std::string& trigger); |
| 57 | std::size_t NumCommands() const; |
| 58 | void ExecuteOneCommand(std::size_t command) const; |
| 59 | void ExecuteAllCommands() const; |
| 60 | bool CheckEventTrigger(const std::string& trigger) const; |
| 61 | bool CheckPropertyTrigger(const std::string& name, |
| 62 | const std::string& value) const; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 63 | bool TriggersEqual(const Action& other) const; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 64 | std::string BuildTriggersString() const; |
| 65 | void DumpState() const; |
| 66 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 67 | bool oneshot() const { return oneshot_; } |
| 68 | static void set_function_map(const KeywordMap<BuiltinFunction>* function_map) { |
| 69 | function_map_ = function_map; |
| 70 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 71 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 72 | |
| 73 | private: |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 74 | void ExecuteCommand(const Command& command) const; |
| 75 | bool CheckPropertyTriggers(const std::string& name = "", |
| 76 | const std::string& value = "") const; |
| 77 | bool ParsePropertyTrigger(const std::string& trigger, std::string* err); |
| 78 | |
| 79 | std::map<std::string, std::string> property_triggers_; |
| 80 | std::string event_trigger_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 81 | std::vector<Command> commands_; |
| 82 | bool oneshot_; |
| 83 | static const KeywordMap<BuiltinFunction>* function_map_; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 86 | class Trigger { |
| 87 | public: |
| 88 | virtual ~Trigger() { } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 89 | virtual bool CheckTriggers(const Action& action) const = 0; |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 92 | class ActionManager { |
| 93 | public: |
| 94 | static ActionManager& GetInstance(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 95 | |
| 96 | void AddAction(std::unique_ptr<Action> action); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 97 | void QueueEventTrigger(const std::string& trigger); |
| 98 | void QueuePropertyTrigger(const std::string& name, const std::string& value); |
| 99 | void QueueAllPropertyTriggers(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 100 | void QueueBuiltinAction(BuiltinFunction func, const std::string& name); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 101 | void ExecuteOneCommand(); |
| 102 | bool HasMoreCommands() const; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 103 | void DumpState() const; |
| 104 | |
| 105 | private: |
| 106 | ActionManager(); |
| 107 | |
| 108 | ActionManager(ActionManager const&) = delete; |
| 109 | void operator=(ActionManager const&) = delete; |
| 110 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 111 | std::vector<std::unique_ptr<Action>> actions_; |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 112 | std::queue<std::unique_ptr<Trigger>> trigger_queue_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 113 | std::queue<const Action*> current_executing_actions_; |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 114 | std::size_t current_command_; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 117 | class ActionParser : public SectionParser { |
| 118 | public: |
| 119 | ActionParser() : action_(nullptr) { |
| 120 | } |
| 121 | bool ParseSection(const std::vector<std::string>& args, |
| 122 | std::string* err) override; |
| 123 | bool ParseLineSection(const std::vector<std::string>& args, |
| 124 | const std::string& filename, int line, |
| 125 | std::string* err) const override; |
| 126 | void EndSection() override; |
| 127 | void EndFile(const std::string&) override { |
| 128 | } |
| 129 | private: |
| 130 | std::unique_ptr<Action> action_; |
| 131 | }; |
| 132 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 133 | #endif |