我正在试图弄清楚如何创建一个C ++ 11模板函数,它可以在两个约定之间转换函数调用:第一个是使用Variant(注意:一个变体是一个多态类型,它是子类IntVariable,DoubleVariant等),第二个是C函数调用。
我们在编译时知道每一条信息:参数count是参数的数量,参数/返回类型取决于'cfunc'变量类型。
// We will assume that the two following functions are defined with their correct
// specializations.
template < typename T >
Variant * convertToVariant( T t );
template < typename T >
T convertFromVariant( Variant * variant );
// The following function is incomplete, the question is how to convert the
// variant parameters into a C function call ?
template < typename Return, typename... Arguments >
Variant * wrapCFunction< Return cfunc( Args... ) >(int argc, Variant ** argv) {
// Here comes the magic call of cfunc, something like :
if ( argc != mpl::count< Args... >::value )
throw std::runtime_error( "bad argument count" );
return cfunc( convertFromVariant< Args... >( argv[ X ] )... );
}
// Example use case :
int foo( int a, int b );
int main(void) {
int argc = 2;
Variant * argv[2] = { new IntVariant( 5 ), new IntVariant( 6 ) };
Variant * res = wrapCFunction< foo >( argc, argv );
IntVariant * intRes = dynamic_cast< IntVariant >( res );
return intRes ? intRes->value : -1;
}
答案 0 :(得分:5)
使用indices trick,这很容易:
template<unsigned...> struct indices{};
template<unsigned N, unsigned... Is>
struct indices_gen : indices_gen<N-1, N-1, Is...>{};
template<unsigned... Is>
struct indices_gen<0, Is...> : indices<Is...>{};
// assuming the parameters were actually like this
template<typename Return, typename... Args, unsigned... Is>
Variant* wrapCFunction(Return (*cfunc)(Args...), int argc, Variant** argv, indices<Is...>) {
return cfunc(convertFromVariant<Args>(argv[Is])...);
}
template<typename Return, typename... Args>
Variant* wrapCFunction(Return (*cfunc)(Args...), int argc, Variant** argv) {
if (argc != sizeof...(Args))
throw std::runtime_error("bad argument count");
return wrapCFunction(cfunc, argc, argv, indices_gen<sizeof...(Args)>());
}
请注意代码中的一些更改。首先,sizeof...(Args)
产生包中的参数数量。其次,我修改了函数的签名,将cfunc
作为实际参数传递。
答案 1 :(得分:2)
class Variant;
template < typename T > Variant * convertToVariant( T t );
template < typename T > T convertFromVariant( Variant * variant );
template <typename Return, typename... Arguments>
struct WrapCFunctionImpl {
template<int argsToAdd, int... argIndexes>
struct Impl {
typedef typename Impl<argsToAdd - 1, argIndexes..., sizeof...(argIndexes)>::Type Type;
};
};
template <typename Return, typename... Arguments>
template <int... argIndexes>
struct WrapCFunctionImpl<Return, Arguments...>::Impl<0, argIndexes...> {
typedef Impl Type;
static Variant* run(Return cfunc( Arguments... ), Variant ** argv) {
return convertToVariant( cfunc( convertFromVariant<Arguments>( argv[argIndexes] )... ) );
}
};
template < typename Return, typename... Arguments >
Variant * wrapCFunction(Return cfunc( Arguments... ), Variant ** argv) {
return WrapCFunctionImpl<Return,Arguments...>::template Impl<sizeof...(Arguments)>::Type::run(cfunc, argv);
}
int foo(int, int);
Variant *f(Variant** x) {
return wrapCFunction(foo, x);
}
这里的大部分困难是将索引生成到数组中的递归。可能有更简单的方法,但这可行。