2015-08-26 18:43:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INIT_KEYWORD_MAP_H_
|
|
|
|
#define _INIT_KEYWORD_MAP_H_
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2015-12-05 06:00:26 +00:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-08-26 18:43:36 +00:00
|
|
|
|
2017-08-03 00:01:36 +00:00
|
|
|
#include "result.h"
|
|
|
|
|
2017-06-22 19:53:17 +00:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2015-08-26 18:43:36 +00:00
|
|
|
template <typename Function>
|
|
|
|
class KeywordMap {
|
2017-04-19 23:18:50 +00:00
|
|
|
public:
|
2015-08-26 18:43:36 +00:00
|
|
|
using FunctionInfo = std::tuple<std::size_t, std::size_t, Function>;
|
2017-04-19 23:18:50 +00:00
|
|
|
using Map = std::map<std::string, FunctionInfo>;
|
2015-08-26 18:43:36 +00:00
|
|
|
|
|
|
|
virtual ~KeywordMap() {
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:01:36 +00:00
|
|
|
const Result<Function> FindFunction(const std::vector<std::string>& args) const {
|
2015-08-26 18:43:36 +00:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
2017-08-03 00:01:36 +00:00
|
|
|
if (args.empty()) return Error() << "Keyword needed, but not provided";
|
|
|
|
|
2017-04-24 23:59:05 +00:00
|
|
|
auto& keyword = args[0];
|
|
|
|
auto num_args = args.size() - 1;
|
|
|
|
|
2015-08-26 18:43:36 +00:00
|
|
|
auto function_info_it = map().find(keyword);
|
|
|
|
if (function_info_it == map().end()) {
|
2017-08-03 00:01:36 +00:00
|
|
|
return Error() << StringPrintf("Invalid keyword '%s'", keyword.c_str());
|
2015-08-26 18:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto function_info = function_info_it->second;
|
|
|
|
|
|
|
|
auto min_args = std::get<0>(function_info);
|
|
|
|
auto max_args = std::get<1>(function_info);
|
|
|
|
if (min_args == max_args && num_args != min_args) {
|
2017-08-03 00:01:36 +00:00
|
|
|
return Error() << StringPrintf("%s requires %zu argument%s", keyword.c_str(), min_args,
|
|
|
|
(min_args > 1 || min_args == 0) ? "s" : "");
|
2015-08-26 18:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (num_args < min_args || num_args > max_args) {
|
|
|
|
if (max_args == std::numeric_limits<decltype(max_args)>::max()) {
|
2017-08-03 00:01:36 +00:00
|
|
|
return Error() << StringPrintf("%s requires at least %zu argument%s",
|
|
|
|
keyword.c_str(), min_args, min_args > 1 ? "s" : "");
|
2015-08-26 18:43:36 +00:00
|
|
|
} else {
|
2017-08-03 00:01:36 +00:00
|
|
|
return Error() << StringPrintf("%s requires between %zu and %zu arguments",
|
|
|
|
keyword.c_str(), min_args, max_args);
|
2015-08-26 18:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::get<Function>(function_info);
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:18:50 +00:00
|
|
|
private:
|
|
|
|
// Map of keyword ->
|
|
|
|
// (minimum number of arguments, maximum number of arguments, function pointer)
|
|
|
|
virtual const Map& map() const = 0;
|
2015-08-26 18:43:36 +00:00
|
|
|
};
|
|
|
|
|
2017-06-22 19:53:17 +00:00
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|
|
|
|
|
2015-08-26 18:43:36 +00:00
|
|
|
#endif
|