6

I've been working with libffi lately, and since it uses a C API, any abstraction is done by using void pointers (good ol' C). I'm creating a class (with variadic templates) which utilizes this API. The class declaration is the following: (where Ret = return value and Args = function arguments)

template <typename Ret, typename... Args>
class Function

Within this class I have two different functions declared as well (simplified):

Ret Call(Args... args); // Calls the wrapped function
void CallbackBind(Ret * ret, void * args[]); // The libffi callback function (it's actually static...)

I want to be able to use Call from CallbackBind; and that is my problem. I have no idea how I am supposed to convert the void* array to the templated argument list. This is what I want more or less:

CallbackBind(Ret * ret, void * args[])
{
 // I want to somehow expand the array of void pointers and convert each
 // one of them to the corresponding template type/argument. The length
 // of the 'void*' vector equals sizeof...(Args) (variadic template argument count)

 // Cast each of one of the pointers to their original type
 *ret = Call(*((typeof(Args[0])*) args[0]), *((typeof(Args[1])*) args[1]), ... /* and so on */);
}

If this is not achievable, are there any workarounds or different solutions available?

5
  • 1
    Is there a reason you can't just call directly into the library's API from the callback? You already have a void*, and it already expects that.
    – Mark B
    Jun 25, 2012 at 18:43
  • Mmm why would you want to call a variadic template function with a fixed amount of arguments? Based on the fact that "the length of args equals sizeof...(Args)", then you know the number of arguments required, why use variadic templates?.
    – mfontanini
    Jun 25, 2012 at 18:49
  • @MarkB Since CallbackBind is called from the library's API I'm trying to call my own function which does not use void pointers. @mfontanini I want a class which mimics std::function, and therefore I require variadic templates (such as operator()(Args...)) Jun 25, 2012 at 18:59
  • 2
    I'm tempted to write something like CallAux<Tuple, Indices...>::call(void * args[]) { Call(*static_cast<(*std::tuple_element<Tuple, Indices>*>(args[Indices]))...); }.
    – Kerrek SB
    Jun 25, 2012 at 19:01
  • 1
    You should have a look at this: stackoverflow.com/questions/7858817/…. You could adapt it to fit your needs :D
    – mfontanini
    Jun 25, 2012 at 19:19

1 Answer 1

5

You don't want to iterate over the types, you want to create a parameter pack and expand it in a variadic template. You have an array, so the pack you want is a pack of integers 0,1,2... to serve as array indices.

#include <redi/index_tuple.h>

template<typename Ret, typename... Args>
struct Function
{
  Ret (*wrapped_function)(Args...);

  template<unsigned... I>
  Ret dispatch(void* args[], redi::index_tuple<I...>)
  {
    return wrapped_function(*static_cast<Args*>(args[I])...);
  }

  void CallbackBind(Ret * ret, void * args[])
  {
    *ret = dispatch(args, to_index_tuple<Args...>());
  }
};

Something like that, using index_tuple.h

The trick is that CallbackBind creates an index_tuple of integers representing the arg positions, and dispatches to another function which deduces the integers and expands the pack into a list of cast expressions to use as arguments to the wrapped function.

1
  • What an elegant solution! Worked flawlessly for me. Jun 26, 2012 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.