blob: 481d637020e1bfc338dc4661ebb22402d9edaaeb [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
17#ifndef _INIT_KEYWORD_MAP_H_
18#define _INIT_KEYWORD_MAP_H_
19
20#include <map>
21#include <string>
22
Elliott Hughes4f713192015-12-04 22:00:26 -080023#include <android-base/stringprintf.h>
Tom Cherryb7349902015-08-26 11:43:36 -070024
Tom Cherry81f5d3e2017-06-22 12:53:17 -070025namespace android {
26namespace init {
27
Tom Cherryb7349902015-08-26 11:43:36 -070028template <typename Function>
29class KeywordMap {
Tom Cherryad54d092017-04-19 16:18:50 -070030 public:
Tom Cherryb7349902015-08-26 11:43:36 -070031 using FunctionInfo = std::tuple<std::size_t, std::size_t, Function>;
Tom Cherryad54d092017-04-19 16:18:50 -070032 using Map = std::map<std::string, FunctionInfo>;
Tom Cherryb7349902015-08-26 11:43:36 -070033
34 virtual ~KeywordMap() {
35 }
36
Tom Cherryfe062052017-04-24 16:59:05 -070037 const Function FindFunction(const std::vector<std::string>& args, std::string* err) const {
Tom Cherryb7349902015-08-26 11:43:36 -070038 using android::base::StringPrintf;
39
Tom Cherryfe062052017-04-24 16:59:05 -070040 if (args.empty()) {
41 *err = "keyword needed, but not provided";
42 return nullptr;
43 }
44 auto& keyword = args[0];
45 auto num_args = args.size() - 1;
46
Tom Cherryb7349902015-08-26 11:43:36 -070047 auto function_info_it = map().find(keyword);
48 if (function_info_it == map().end()) {
49 *err = StringPrintf("invalid keyword '%s'", keyword.c_str());
50 return nullptr;
51 }
52
53 auto function_info = function_info_it->second;
54
55 auto min_args = std::get<0>(function_info);
56 auto max_args = std::get<1>(function_info);
57 if (min_args == max_args && num_args != min_args) {
58 *err = StringPrintf("%s requires %zu argument%s",
59 keyword.c_str(), min_args,
60 (min_args > 1 || min_args == 0) ? "s" : "");
61 return nullptr;
62 }
63
64 if (num_args < min_args || num_args > max_args) {
65 if (max_args == std::numeric_limits<decltype(max_args)>::max()) {
66 *err = StringPrintf("%s requires at least %zu argument%s",
67 keyword.c_str(), min_args,
68 min_args > 1 ? "s" : "");
69 } else {
70 *err = StringPrintf("%s requires between %zu and %zu arguments",
71 keyword.c_str(), min_args, max_args);
72 }
73 return nullptr;
74 }
75
76 return std::get<Function>(function_info);
77 }
78
Tom Cherryad54d092017-04-19 16:18:50 -070079 private:
80 // Map of keyword ->
81 // (minimum number of arguments, maximum number of arguments, function pointer)
82 virtual const Map& map() const = 0;
Tom Cherryb7349902015-08-26 11:43:36 -070083};
84
Tom Cherry81f5d3e2017-06-22 12:53:17 -070085} // namespace init
86} // namespace android
87
Tom Cherryb7349902015-08-26 11:43:36 -070088#endif