Init函数映射

KeywordMap

class KeywordMap {                                                              
  public:                                                                       
    using FunctionInfo = std::tuple<std::size_t, std::size_t, Function>;        
    using Map = std::map<std::string, FunctionInfo>;                            
                                                                                                                                                                                                            
    virtual ~KeywordMap() {                                                     
    }                                                                           
                                                                                
    const Result<Function> FindFunction(const std::vector<std::string>& 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<decltype(max_args)>::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>(function_info);                
        //function_info定义是std::tuple<std::size_t, std::size_t, Function>,
        //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<BuiltinFunction>* function_map_;
using BuiltinFunction = std::function<int(const std::vector<std::string>&)>; 


// Builtin-function-map start                                                   
const BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {                
    constexpr std::size_t kMax = std::numeric_limits<std::size_t>::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&参数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值