Maël Nison
Maël Nison

Reputation: 7353

C++11 variadic template function call forwarding

I'm trying to figure out how to create a C++11 template function which would convert function calls between two conventions : the first one is using Variant (note : a variant is a polymorphic type which is the base for the subclasses IntVariable, DoubleVariant, etc), the second one is the C function call.

We know every piece of information at the compile time : the argument count is the number of parameters, and the arguments/return type depends of the 'cfunc' variable type.

// 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;
}

Upvotes: 1

Views: 1950

Answers (2)

Xeo
Xeo

Reputation: 131789

Using the indices trick, this is rather easy:

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)>());
}

Note some changes in the code. First, sizeof...(Args) yields the number of arguments in the pack. Second, I fixed the signature of the function to pass cfunc as an actual parameter.

Upvotes: 5

servn
servn

Reputation: 3069

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);
}

Most of the difficulty here is the recursion to generate the indexes into the array. There might be simpler ways to do it, but this works.

Upvotes: 2

Related Questions