C++模板类型匹配在RPC分发中的应用研究

最近研究了一下MammothServer,发现里面有一个叫Dispatcher的实现,很有意思。正好自己最近在学习boost::mpl等东东,因此花了几天学习,并把自己的学习心得总结了一下。相信对大多数C++程序员会有帮助。

前言

在编写通讯框架时,经常要处理众多的协议。而处理完协议后,再调用相应的处理函数时,
在C++中,我们一般要使用统一接口。比如Windows消息中的MSG结构等等。
这种统一的处理结构最大的缺点是缺乏有效地类型检测,容易出错。因为,编译器无法对每一个特定消息进行数据合法性检查。

不过利用模板技术,完全可以实现RPC参数的解析以及与C++函数的自动匹配,支持不定长参数。

伪代码
class Dispatcher {
map<string, func> invokers_;
void register_function( const std::string& name, func);
void invoke( void* packet ) {
sequence params = deserialize( packet );
invokers_[ params["name"] ]( params );
}
}

void hello(const std::string& msg ) {
}
void hello2(const std::string& msg , const std::string& from) {
}

Dispatcher.register_function("hello", hello );
Dispatcher.register_function("hello2", hello2 );


准备知识

C++ template
boost::bind
boost::function 仿函数
boost::fusion 处理不同类型参数的sequence
boost::mpl 处理函数参数表的推导


part1 - basic framework

由于用户提供的函数带有不确定个数的参数,因此,在dispatcher内部,需要首先将用户提供的Function转换成为统一参数的仿函数,定义如下:
[quote]出于讲解的需要,暂时不支持返回值,且与应用相关的参数被简化为一个void* [/quote]

typedef boost::function<void(void*)> invoker_function_type;

相应的容器为
typedef std::map<std::string, invoker_function_type> dictionary_type;


注册函数考虑面向对象,因此包含函数Function和类实例Base.

class Dispatcher {
// 实际的invoke函数形式
typedef boost::function<void(void*)> invoker_function_type;
// name -> invoker 表
typedef std::map<std::string, invoker_function_type> dictionary_type;

dictionary_type m_invokers;

public:
template<typename Function, typename Base>
void register_function(std::string const & name, Function f, Base& base) {
m_invokers[name] = ...; //TODO
}
};



part2 - invoker

invoker是整个实现的核心。其中最重要的是对用户调用函数参数的自适应。

// predefinition
template<typename Function
,typename From = typename mpl::advance_c<typename mpl::begin< ft::parameter_types<Function> >::type, 1>::type
,typename To = typename mpl::end< ft::parameter_types<Function> >::type
>
struct Invoker;

// invoker,提取参数,放入sequence,继续调用
template<typename Function
,typename From
,typename To
>
struct Invoker
{
static void apply(Function func) {
typedef typename mpl::deref<From>::type arg_type; // 当前参数类型
typedef typename mpl::next<From>::type next_iter_type; // 下一个参数

typedef Invoker<Function, next_iter_type, To> NextInvoker;
NextInvoker::apply(func);
}
};

// 特化的invoker,结尾
template<typename Function
,typename To
>
struct Invoker<Function,To,To>
{
static void apply(Function func) {
std::cout << "finish";
}
};

template<typename Function>
void trigger(Function func) {

Invoker<Function>::apply( func );
}

不准确,但是直观的的描述可以理解为:
如果 函数为 func( int, char,void*),则实际生成的模板类分别是

Invoker< Function, int , void >;
Invoker< Function, char, void >;
Invoker< Function, void*, void >;
Invoker< Function, void, void >; //* 这个会匹配到特化的Invoker,从而结束递归


加入收集参数的模板参数
// invoker,提取参数,放入sequence,继续调用
template<typename Function
,typename From
,typename To
>
struct Invoker
{
template< typename Args >
static void apply(Function func, Args args) {
typedef typename mpl::deref<From>::type arg_type; // 当前参数类型
typedef typename mpl::next<From>::type next_iter_type; // 下一个参数

typedef Invoker<Function, next_iter_type, To> NextInvoker;
NextInvoker::apply(func, fusion::push_back(args,std::string("hello")));
}
};

// 特化的invoker,结尾
template<typename Function
,typename To
>
struct Invoker<Function,To,To>
{
template< typename Args >
static void apply(Function func,Args args) {
std::cout << args;
fusion::invoke(func, fusion::push_front(args, input)); // 完成对用户函数的调用
}
};

template<typename Function>
void trigger(Function func) {
Invoker<Function>::apply( func , fusion::nil() );
}


进一步实现对参数的提取,就可以实现一个完整的RPC的机制。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值