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> |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 23 | #include <variant> |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 26 | #include "builtins.h" |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 27 | #include "keyword_map.h" |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 28 | #include "result.h" |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 29 | #include "subcontext.h" |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 30 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace init { |
| 33 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 34 | Result<Success> RunBuiltinFunction(const BuiltinFunction& function, |
| 35 | const std::vector<std::string>& args, const std::string& context); |
| 36 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 37 | class Command { |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 38 | public: |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 39 | Command(BuiltinFunction f, bool execute_in_subcontext, const std::vector<std::string>& args, |
| 40 | int line); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 41 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 42 | Result<Success> InvokeFunc(Subcontext* subcontext) const; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 43 | std::string BuildCommandString() const; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 44 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 45 | int line() const { return line_; } |
| 46 | |
| 47 | private: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 48 | BuiltinFunction func_; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 49 | bool execute_in_subcontext_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 50 | std::vector<std::string> args_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 51 | int line_; |
| 52 | }; |
| 53 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 54 | using EventTrigger = std::string; |
| 55 | using PropertyChange = std::pair<std::string, std::string>; |
| 56 | using BuiltinAction = class Action*; |
| 57 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 58 | class Action { |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 59 | public: |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 60 | Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line, |
| 61 | const std::string& event_trigger, |
| 62 | const std::map<std::string, std::string>& property_triggers); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 63 | |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 64 | Result<Success> AddCommand(const std::vector<std::string>& args, int line); |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 65 | void AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 66 | std::size_t NumCommands() const; |
| 67 | void ExecuteOneCommand(std::size_t command) const; |
| 68 | void ExecuteAllCommands() const; |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 69 | bool CheckEvent(const EventTrigger& event_trigger) const; |
| 70 | bool CheckEvent(const PropertyChange& property_change) const; |
| 71 | bool CheckEvent(const BuiltinAction& builtin_action) const; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 72 | std::string BuildTriggersString() const; |
| 73 | void DumpState() const; |
| 74 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 75 | bool oneshot() const { return oneshot_; } |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 76 | const std::string& filename() const { return filename_; } |
| 77 | int line() const { return line_; } |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 78 | static void set_function_map(const KeywordFunctionMap* function_map) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 79 | function_map_ = function_map; |
| 80 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 81 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 82 | private: |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 83 | void ExecuteCommand(const Command& command) const; |
| 84 | bool CheckPropertyTriggers(const std::string& name = "", |
| 85 | const std::string& value = "") const; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 86 | |
| 87 | std::map<std::string, std::string> property_triggers_; |
| 88 | std::string event_trigger_; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 89 | std::vector<Command> commands_; |
| 90 | bool oneshot_; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 91 | Subcontext* subcontext_; |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 92 | std::string filename_; |
| 93 | int line_; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 94 | static const KeywordFunctionMap* function_map_; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 97 | } // namespace init |
| 98 | } // namespace android |
| 99 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 100 | #endif |