我正在尝试编写一个用C ++管理代理的类。我已经为我实现了委托类。我希望这个委托管理器类有两个功能:
可以使用指定输入参数/返回类型的指定某个类型委托的实例,然后对其进行缓存。
另一个函数将采用正确类型的成员函数,以便将缓存的委托实例绑定到它。
目前,我有:
template<typename... Args>
struct FunctionParamsPack { };
这是此函数所用参数类型的容器。即foo(int i, double d)
和int
double
。我遵循here的建议。
然后我有DelegateInfoPack
类:
template<typename FuncRetType,typename... FuncParams>
struct DelegateInfoPack{
//for look-up by components in the program
typedef typename DelegateClass<FuncRetType, FuncParams...> _Delegate;
//for the delegate manager
typedef typename FuncRetType _FuncRetType;
typedef typename FunctionParamsPack<FuncParams...> _FuncParams;
};
该结构包含在程序中的组件中,并且它键入了三个类型名,其中两个将在DelegateManger类中使用:
template<typename DelegateInfoPack>
class DelegateManager
{
typedef typename DelegateInfoPack::_Delegate _Delegate;
typedef typename DelegateInfoPack::_FuncRetType _FuncRetType;
typedef typename DelegateInfoPack::_FuncParams _FuncParams;
void CacheDelegate(_Delegate* del,...) {}
template<typename UserClass>
void BindDelegate(..., _FuncRetType(UserClass::*fp)( _FuncParams())) {} //Doesn't work!
}
我的问题在于BindDelegate()
功能。我无法为具有给定返回类型和输入参数类型的类型的成员函数创建正确的签名。
基本上,我需要知道如何使用给定的返回类型和参数类型来获得正确的函数指针类型,以便我的BindDelegate将其作为参数。
答案 0 :(得分:2)
一种方法是使用部分专业化:
template<typename> class DelegateManager;
template<typename FuncRetType,typename... FuncParams>
class DelegateManager<DelegateInfoPack<FuncRetType,FuncParams...>>
{
template<typename UserClass>
void BindDelegate(_FuncRetType(UserClass::*fp)(FuncParams...))
{
}
};
另一种方法是创建一个生成适当函数类型的类
template <typename FuncRetType,typename FuncParams>
struct FunctionPointer;
template <typename FuncRetType,typename...ARGS>
struct FunctionPointer<FuncRetType,FunctionParamsPack<ARGS...>> {
typedef FuncRetType (Type)(ARGS...);
};
然后在你的BindDelegate成员函数中使用它:
template<typename UserClass>
void
BindDelegate(
typename FunctionPointer<_FuncRetType,_FuncParams>::Type UserClass::*fp
)
{ ... }
或者甚至可以将它放入DelegateInfoPack类:
template<typename FuncRetType,typename... FuncParams>
struct DelegateInfoPack {
.
.
.
typedef FuncRetType (_FuncType)(FuncParams...);
};
并在DelegateManager中使用它
template<typename DelegateInfoPack>
struct DelegateManager
{
.
.
.
typedef typename DelegateInfoPack::_FuncType _FuncType;
template<typename UserClass>
void BindDelegate(_FuncType UserClass::*fp)
{
}
};
答案 1 :(得分:1)
作为解决任务的另一种方法 - C ++ 11引入了新的语言功能,可以使用标准元素使代码更加灵活
#include <iostream>
#include <functional>
#include <tuple>
#include <iostream>
using std::cout;
using std::endl;
using namespace std::placeholders;
// helpers for tuple unrolling
template<int ...> struct seq {};
template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {};
template<int ...S> struct gens<0, S...>{ typedef seq<S...> type; };
// simple function
double foo_fn(int x, float y, double z)
{
return x + y + z;
}
// structure with memner function to call
struct foo_struct
{
// member function to be used as a delegate
double foo_fn(int x, float y, double z)
{
return x + y + z;
}
// this member function has different signature - but it can be used too
// please note that argument order is changed too
double foo_fn_4(int x, double z, float y, long xx)
{
return x + y + z + xx;
}
};
// delegate class that holds as delegate as its params for future call
template <typename Ret, typename ...Args>
struct delayed_call
{
// tuple can be used as FunctionParamsPack type
typedef std::tuple<Args...> params_type;
// std::function as delegate type
typedef std::function<Ret(Args...)> function_type;
// stored parameters
params_type params;
// stored delegate
function_type func;
// invocation
Ret operator()()
{
return callFunc(typename gens<sizeof...(Args)>::type());
}
// direct invocation
Ret operator()(Args... args)
{
return func(args...);
}
// internal invocation with tuple unrolling
template<int ...S>
double callFunc(seq<S...>)
{
return func(std::get<S>(params) ...);
}
};
int main(void)
{
// arguments
std::tuple<int, float, double> t = std::make_tuple(1, 5, 10);
// var #1 - you can use simple function as delegate
delayed_call<double, int,float, double> saved_foo_fn{t, foo_fn};
foo_struct fs;
// var #2 - you can use member function as delegate
delayed_call<double, int,float, double> saved_foo_fn_struct{t, std::bind(&foo_struct::foo_fn, fs, _1, _2, _3)};
// var #3 - you can use member function with different signature as delegate.
// bind 0 to xx and change argument order
delayed_call<double, int,float, double> saved_foo_fn_struct_4{t, std::bind(&foo_struct::foo_fn_4, fs, _1, _3, _2, 0l)};
// var #4 - you can use lambda function as delegate
delayed_call<double, int,float, double> saved_lambda{t, [](int x, float y, double z)
{
return x + y + z;
}
};
cout << "saved_foo_fn: " << saved_foo_fn() << endl;
cout << "saved_foo_fn_struct: " << saved_foo_fn_struct() << endl;
cout << "saved_foo_fn_struct_4: " << saved_foo_fn_struct_4() << endl;
cout << "saved_lambda: " << saved_lambda() << endl;
cout << "direct call with (1,2,3) to a member: " << saved_foo_fn_struct(1, 2, 3) << endl;
}
输出:
saved_foo_fn: 16
saved_foo_fn_struct: 16
saved_foo_fn_struct_4: 16
saved_lambda: 16
direct call with (1,2,3) to a member: 6
因此,您不仅限于成员函数,还可以使用任何可调用类型甚至不同的签名
如果占位符:: _ 1 ......看起来很难看 - there is a solution