博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Init函数映射
阅读量:2383 次
发布时间:2019-05-10

本文共 6297 字,大约阅读时间需要 20 分钟。

KeywordMap

class KeywordMap {                                                                public:                                                                           using FunctionInfo = std::tuple
; using Map = std::map
; virtual ~KeywordMap() { } const Result
FindFunction(const std::vector
& args) const { using android::base::StringPrintf; if (args.empty()) return Error() << "Keyword needed, but not provided"; auto& keyword = args[0]; auto num_args = args.size() - 1; auto function_info_it = map().find(keyword); //在map表里,找到相同"do_mount" if (function_info_it == map().end()) { return Error() << StringPrintf("Invalid keyword '%s'", keyword.c_str()); } 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) { return Error() << StringPrintf("%s requires %zu argument%s", keyword.c_str(), min_args, (min_args > 1 || min_args == 0) ? "s" : ""); } if (num_args < min_args || num_args > max_args) { if (max_args == std::numeric_limits
::max()) { return Error() << StringPrintf("%s requires at least %zu argument%s", keyword.c_str(), min_args, min_args > 1 ? "s" : ""); } else { return Error() << StringPrintf("%s requires between %zu and %zu arguments", keyword.c_str(), min_args, max_args); } } return std::get
(function_info); //function_info定义是std::tuple
, //function_info是Fuction实例化,比如 {1, 4, {true, do_mkdir} //则会返回do_mkdir函数的地址 } private: // Map of keyword -> // (minimum number of arguments, maximum number of arguments, function pointer) virtual const Map& map() const = 0; };

Map使用std::map的key值是函数名,比如"do_mount",还有个FunctionInfo类型。

FunctionInfo是std::tuple,第1个和第2个是整型,第三个是Function是函数返回地址。

Function

static const KeywordMap
* function_map_;using BuiltinFunction = std::function
&)>; // Builtin-function-map start const BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const { constexpr std::size_t kMax = std::numeric_limits
::max(); // clang-format off static const Map builtin_functions = { {"bootchart", {1, 1, {false, do_bootchart}}}, {"chmod", {2, 2, {true, do_chmod}}}, {"chown", {2, 3, {true, do_chown}}}, {"class_reset", {1, 1, {false, do_class_reset}}}, {"class_restart", {1, 1, {false, do_class_restart}}}, {"class_start", {1, 1, {false, do_class_start}}}, {"class_stop", {1, 1, {false, do_class_stop}}}, {"copy", {2, 2, {true, do_copy}}}, {"domainname", {1, 1, {true, do_domainname}}}, {"enable", {1, 1, {false, do_enable}}}, {"exec", {1, kMax, {false, do_exec}}}, {"exec_background", {1, kMax, {false, do_exec_background}}}, {"exec_start", {1, 1, {false, do_exec_start}}}, {"export", {2, 2, {false, do_export}}}, {"hostname", {1, 1, {true, do_hostname}}}, {"ifup", {1, 1, {true, do_ifup}}}, {"init_user0", {0, 0, {false, do_init_user0}}}, {"insmod", {1, kMax, {true, do_insmod}}}, {"installkey", {1, 1, {false, do_installkey}}}, {"load_persist_props", {0, 0, {false, do_load_persist_props}}}, {"load_system_props", {0, 0, {false, do_load_system_props}}}, {"loglevel", {1, 1, {false, do_loglevel}}}, {"mkdir", {1, 4, {true, do_mkdir}}},

在使用KeywordMap模板类时候,传入BuiltinFunction参数,BuiltinFunction使用std::function,返回是int,传入的onst std::vectorstd::string&参数。

转载地址:http://hafab.baihongyu.com/

你可能感兴趣的文章
下载Apache官网commons-fileupload.jar和 commons-io.jar包
查看>>
HTTP Status 500 – Internal Server Error Servlet execution threw an exception解决办法
查看>>
解决:laydate时间控件与谷歌浏览器兼容问题
查看>>
将一个长度最多为30位数字的十进制非负整数转换为二进制数输出 c语言 大数处理
查看>>
Intel80x86处理器发展简史
查看>>
个人计算机系统的组成
查看>>
存储器组织
查看>>
城市道路十字路口的通行规则是“红灯停,绿灯行”,请用信号量和PV操作描述交通信号灯和汽车通过十字路口的同步行为
查看>>
顾客银行办理业务时,首先在取号机上取号,然后坐在椅子上等候业务员叫号时前往窗口办理业务,假设银行现在有3个窗口可办理业务,请采用信号量和PV操作描述顾取号等候叫号和银行业务员叫号办理业务的同步操作。
查看>>
cookie技术和session技术
查看>>
汇编语言数据表示
查看>>
汇编语言数据寻址方式
查看>>
汇编语言数据传送指令之通用数据传送类指令
查看>>
汇编语言数据传送指令之堆栈操作指令
查看>>
状态标志
查看>>
汇编语言加法指令
查看>>
汇编语言减法指令
查看>>
汇编语言乘法和除法指令
查看>>
Pair Programming(结对编程)
查看>>
敏捷开发(Agile Development)
查看>>