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