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 | |
| 25 | class Action { |
| 26 | public: |
| 27 | Action(); |
| 28 | |
| 29 | void AddCommand(int (*f)(int nargs, char** args), |
| 30 | const std::vector<std::string>& args, |
| 31 | const std::string& filename = "", int line = 0); |
| 32 | bool InitTriggers(const std::vector<std::string>& args, std::string* err); |
| 33 | bool InitSingleTrigger(const std::string& trigger); |
| 34 | std::size_t NumCommands() const; |
| 35 | void ExecuteOneCommand(std::size_t command) const; |
| 36 | void ExecuteAllCommands() const; |
| 37 | bool CheckEventTrigger(const std::string& trigger) const; |
| 38 | bool CheckPropertyTrigger(const std::string& name, |
| 39 | const std::string& value) const; |
| 40 | bool TriggersEqual(const class Action& other) const; |
| 41 | std::string BuildTriggersString() const; |
| 42 | void DumpState() const; |
| 43 | |
| 44 | private: |
| 45 | class Command; |
| 46 | |
| 47 | void ExecuteCommand(const Command& command) const; |
| 48 | bool CheckPropertyTriggers(const std::string& name = "", |
| 49 | const std::string& value = "") const; |
| 50 | bool ParsePropertyTrigger(const std::string& trigger, std::string* err); |
| 51 | |
| 52 | std::map<std::string, std::string> property_triggers_; |
| 53 | std::string event_trigger_; |
| 54 | std::vector<Command*> commands_; |
| 55 | }; |
| 56 | |
| 57 | class ActionManager { |
| 58 | public: |
| 59 | static ActionManager& GetInstance(); |
| 60 | void QueueEventTrigger(const std::string& trigger); |
| 61 | void QueuePropertyTrigger(const std::string& name, const std::string& value); |
| 62 | void QueueAllPropertyTriggers(); |
| 63 | void QueueBuiltinAction(int (*func)(int nargs, char** args), |
| 64 | const std::string& name); |
| 65 | void ExecuteOneCommand(); |
| 66 | bool HasMoreCommands() const; |
| 67 | Action* AddNewAction(const std::vector<std::string>& triggers, |
| 68 | std::string* err); |
| 69 | void DumpState() const; |
| 70 | |
| 71 | private: |
| 72 | ActionManager(); |
| 73 | |
| 74 | ActionManager(ActionManager const&) = delete; |
| 75 | void operator=(ActionManager const&) = delete; |
| 76 | |
| 77 | std::vector<Action*> action_list_; |
| 78 | std::queue<Action*> action_queue_; |
| 79 | std::size_t cur_command_; |
| 80 | }; |
| 81 | |
| 82 | #endif |