blob: 7837bb3634748845ccd4c4311c34734d75dd278e [file] [log] [blame]
Tom Cherryb7349902015-08-26 11:43:36 -07001/*
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 Cherryf4db2aa2019-06-14 14:54:02 -070017#pragma once
Tom Cherryb7349902015-08-26 11:43:36 -070018
19#include <map>
20#include <string>
21
Tom Cherryb592dd82017-08-02 17:01:36 -070022#include "result.h"
23
Tom Cherry81f5d3e2017-06-22 12:53:17 -070024namespace android {
25namespace init {
26
Tom Cherryb7349902015-08-26 11:43:36 -070027template <typename Function>
28class KeywordMap {
Tom Cherryad54d092017-04-19 16:18:50 -070029 public:
Tom Cherryb7349902015-08-26 11:43:36 -070030 using FunctionInfo = std::tuple<std::size_t, std::size_t, Function>;
Tom Cherryad54d092017-04-19 16:18:50 -070031 using Map = std::map<std::string, FunctionInfo>;
Tom Cherryb7349902015-08-26 11:43:36 -070032
33 virtual ~KeywordMap() {
34 }
35
Tom Cherryb592dd82017-08-02 17:01:36 -070036 const Result<Function> FindFunction(const std::vector<std::string>& args) const {
Tom Cherryb592dd82017-08-02 17:01:36 -070037 if (args.empty()) return Error() << "Keyword needed, but not provided";
38
Tom Cherryfe062052017-04-24 16:59:05 -070039 auto& keyword = args[0];
40 auto num_args = args.size() - 1;
41
Tom Cherryb7349902015-08-26 11:43:36 -070042 auto function_info_it = map().find(keyword);
43 if (function_info_it == map().end()) {
Tom Cherryf4db2aa2019-06-14 14:54:02 -070044 return Errorf("Invalid keyword '{}'", keyword);
Tom Cherryb7349902015-08-26 11:43:36 -070045 }
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 Cherryf4db2aa2019-06-14 14:54:02 -070052 return Errorf("{} requires {} argument{}", keyword, min_args,
53 (min_args > 1 || min_args == 0) ? "s" : "");
Tom Cherryb7349902015-08-26 11:43:36 -070054 }
55
56 if (num_args < min_args || num_args > max_args) {
57 if (max_args == std::numeric_limits<decltype(max_args)>::max()) {
Tom Cherryf4db2aa2019-06-14 14:54:02 -070058 return Errorf("{} requires at least {} argument{}", keyword, min_args,
59 min_args > 1 ? "s" : "");
Tom Cherryb7349902015-08-26 11:43:36 -070060 } else {
Tom Cherryf4db2aa2019-06-14 14:54:02 -070061 return Errorf("{} requires between {} and {} arguments", keyword, min_args,
62 max_args);
Tom Cherryb7349902015-08-26 11:43:36 -070063 }
Tom Cherryb7349902015-08-26 11:43:36 -070064 }
65
66 return std::get<Function>(function_info);
67 }
68
Tom Cherryad54d092017-04-19 16:18:50 -070069 private:
70 // Map of keyword ->
71 // (minimum number of arguments, maximum number of arguments, function pointer)
72 virtual const Map& map() const = 0;
Tom Cherryb7349902015-08-26 11:43:36 -070073};
74
Tom Cherry81f5d3e2017-06-22 12:53:17 -070075} // namespace init
76} // namespace android