Tom Cherry | b734990 | 2015-08-26 11:43:36 -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_KEYWORD_MAP_H_ |
| 18 | #define _INIT_KEYWORD_MAP_H_ |
| 19 | |
| 20 | #include <map> |
| 21 | #include <string> |
| 22 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 24 | |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame^] | 25 | #include "result.h" |
| 26 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace init { |
| 29 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 30 | template <typename Function> |
| 31 | class KeywordMap { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 32 | public: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 33 | using FunctionInfo = std::tuple<std::size_t, std::size_t, Function>; |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 34 | using Map = std::map<std::string, FunctionInfo>; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 35 | |
| 36 | virtual ~KeywordMap() { |
| 37 | } |
| 38 | |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame^] | 39 | const Result<Function> FindFunction(const std::vector<std::string>& args) const { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 40 | using android::base::StringPrintf; |
| 41 | |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame^] | 42 | if (args.empty()) return Error() << "Keyword needed, but not provided"; |
| 43 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 44 | auto& keyword = args[0]; |
| 45 | auto num_args = args.size() - 1; |
| 46 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 47 | auto function_info_it = map().find(keyword); |
| 48 | if (function_info_it == map().end()) { |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame^] | 49 | return Error() << StringPrintf("Invalid keyword '%s'", keyword.c_str()); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | auto function_info = function_info_it->second; |
| 53 | |
| 54 | auto min_args = std::get<0>(function_info); |
| 55 | auto max_args = std::get<1>(function_info); |
| 56 | if (min_args == max_args && num_args != min_args) { |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame^] | 57 | return Error() << StringPrintf("%s requires %zu argument%s", keyword.c_str(), min_args, |
| 58 | (min_args > 1 || min_args == 0) ? "s" : ""); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (num_args < min_args || num_args > max_args) { |
| 62 | if (max_args == std::numeric_limits<decltype(max_args)>::max()) { |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame^] | 63 | return Error() << StringPrintf("%s requires at least %zu argument%s", |
| 64 | keyword.c_str(), min_args, min_args > 1 ? "s" : ""); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 65 | } else { |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame^] | 66 | return Error() << StringPrintf("%s requires between %zu and %zu arguments", |
| 67 | keyword.c_str(), min_args, max_args); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 68 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return std::get<Function>(function_info); |
| 72 | } |
| 73 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 74 | private: |
| 75 | // Map of keyword -> |
| 76 | // (minimum number of arguments, maximum number of arguments, function pointer) |
| 77 | virtual const Map& map() const = 0; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 80 | } // namespace init |
| 81 | } // namespace android |
| 82 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 83 | #endif |