使用接口和派生的模板类。这种技术称为“类型擦除”。
class CallbackBase
{
public:
virtual ~CallbackBase() = 0;
virtual bool exec(const RequestObject& request, ResponseRequest& response) = 0;
};
template <typename F>
class CallbackImpl : public CallbackBase
{
public:
CallbackImpl(F f) : f_(f) {}
virtual bool exec(const RequestObject& request, ResponseRequest& response) {
return f_(request, response);
}
private:
F f_;
};
template <typename F>
void CreateCallback(F f, std::auto_ptr<CallbackBase>& r) {
r.reset(new CallbackImpl<F>(f));
}
// examples
std::auto_ptr<CallbackBase> my_callback;
CreateCallback(GlobalFooBarRequest, my_callback);
CreateCallback(&FooResource::staticFooBarRequest, my_callback);
// for member function, use std::mem_fun and std::bind1st
...
my_callback->exec(request, response);
您可能想要使用 shared_ptr 或类似的而不是 auto_ptr,这一切都取决于您希望如何存储这些对象。
编辑:您可以编写自己的成员函数包装器/闭包/函数对象。代码如下所示(我没有尝试编译它,所以可能会出现一些错误):
template <typename T>
class RequestWrapper
{
typedef bool (T::*F)(const RequestObject&, ResponseRequest&);
T* obj_;
F f_;
public:
RequestWrapper(T* obj, F f)
: obj_(obj)
, f_(f)
{}
bool operator()(const RequestObject& request, ResponseRequest& response) const {
return (obj_->*f_)(request, response);
}
};
template <typename T, typename F>
RequestWrapper<T> BindRequestMfn(T* obj, F mfn)
{
return RequestWrapper<T>(obj, mfn);
}
CreateCallback(BindRequestMfn(foo, &FooResource::fooBarRequest), my_callback);