blob: c95fc7318884f82ff15a89c784abaa07819ebb74 [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 Cherryb592dd82017-08-02 17:01:36 -070025#include "result.h"
26
Tom Cherry81f5d3e2017-06-22 12:53:17 -070027namespace android {
28namespace init {
29
Tom Cherryb7349902015-08-26 11:43:36 -070030template <typename Function>
31class KeywordMap {
Tom Cherryad54d092017-04-19 16:18:50 -070032 public:
Tom Cherryb7349902015-08-26 11:43:36 -070033 using FunctionInfo = std::tuple<std::size_t, std::size_t, Function>;
Tom Cherryad54d092017-04-19 16:18:50 -070034 using Map = std::map<std::string, FunctionInfo>;
Tom Cherryb7349902015-08-26 11:43:36 -070035
36 virtual ~KeywordMap() {
37 }
38
Tom Cherryb592dd82017-08-02 17:01:36 -070039 const Result<Function> FindFunction(const std::vector<std::string>& args) const {
Tom Cherryb7349902015-08-26 11:43:36 -070040 using android::base::StringPrintf;
41
Tom Cherryb592dd82017-08-02 17:01:36 -070042 if (args.empty()) return Error() << "Keyword needed, but not provided";
43
Tom Cherryfe062052017-04-24 16:59:05 -070044 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()) {
Tom Cherryb592dd82017-08-02 17:01:36 -070049 return Error() << StringPrintf("Invalid keyword '%s'", keyword.c_str());
Tom Cherryb7349902015-08-26 11:43:36 -070050 }
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 Cherryb592dd82017-08-02 17:01:36 -070057 return Error() << StringPrintf("%s requires %zu argument%s", keyword.c_str(), min_args,
58 (min_args > 1 || min_args == 0) ? "s" : "");
Tom Cherryb7349902015-08-26 11:43:36 -070059 }
60
61 if (num_args < min_args || num_args > max_args) {
62 if (max_args == std::numeric_limits<decltype(max_args)>::max()) {
Tom Cherryb592dd82017-08-02 17:01:36 -070063 return Error() << StringPrintf("%s requires at least %zu argument%s",
64 keyword.c_str(), min_args, min_args > 1 ? "s" : "");
Tom Cherryb7349902015-08-26 11:43:36 -070065 } else {
Tom Cherryb592dd82017-08-02 17:01:36 -070066 return Error() << StringPrintf("%s requires between %zu and %zu arguments",
67 keyword.c_str(), min_args, max_args);
Tom Cherryb7349902015-08-26 11:43:36 -070068 }
Tom Cherryb7349902015-08-26 11:43:36 -070069 }
70
71 return std::get<Function>(function_info);
72 }
73
Tom Cherryad54d092017-04-19 16:18:50 -070074 private:
75 // Map of keyword ->
76 // (minimum number of arguments, maximum number of arguments, function pointer)
77 virtual const Map& map() const = 0;
Tom Cherryb7349902015-08-26 11:43:36 -070078};
79
Tom Cherry81f5d3e2017-06-22 12:53:17 -070080} // namespace init
81} // namespace android
82
Tom Cherryb7349902015-08-26 11:43:36 -070083#endif