3

When a C++ application uses a C library that has callbacks in the API, a common pattern is for the app to define static functions that translate some void* user data argument into a class pointer and then call the appropriate member function. The duplication wastes time both writing and reading. It would be nice to have a template function to do this wrapping for me.

I've got part of the way there already...

// C library with some callbacks

typedef void (*CallbackTypeOne)(void* userdata, double arg1);
typedef void (*CallbackTypeTwo)(void* userdata, int arg1);
typedef void (*CallbackTypeThree)(void* userdata, int arg1, float arg2);

typedef void(*GenericCallback)();

void registerAndCallCallback(int typeID, GenericCallback callback, void* userdata)
{
    switch (typeID) {
    case 0: ((CallbackTypeOne)callback)(userdata, 42.0); break;
    case 1: ((CallbackTypeTwo)callback)(userdata, 42); break;
    case 2: ((CallbackTypeThree)callback)(userdata, 42, 42.0f); break;
    };
}

// C++ app using the above library

class MyClass
{
    public:
    MyClass()
    {
        // Ideal short syntax, but doesn't compile
        registerAndCallCallback(0,
            reinterpret_cast<GenericCallback>(
                &staticCallback<MyClass::callbakcOne>),
            this);
        // main.cpp:26:36: error: reinterpret_cast cannot resolve overloaded function 'staticCallback' to type 'GenericCallback' (aka 'void (*)()')

        // A bit more explicit but without providing 'Args' to staticCallback
        registerAndCallCallback(0,
            reinterpret_cast<GenericCallback>(
                &staticCallback<decltype(&MyClass::callbakcOne),
                                &MyClass::callbakcOne>),
            this);
        // main.cpp:52:36: error: too few arguments to function call, expected 1, have 0
        //             (instance->*cb)(args...);
        //              ~~~~~~~~~~~~~         ^
        // main.cpp:37:22: note: in instantiation of function template specialization 'MyClass::staticCallback<void (MyClass::*)(double), &MyClass::callbakcOne>' requested here
        //                     &staticCallback<decltype(&MyClass::callbakcOne),
        //                      ^

        // This works, but I feel there should be a nicer way that avoids having to pass the callback arguments. Avoiding the duplication in decltype would be nice too.
        registerAndCallCallback(0,
            reinterpret_cast<GenericCallback>(
                &staticCallback<decltype(&MyClass::callbakcOne),
                                &MyClass::callbakcOne, double>),
            this);
        registerAndCallCallback(1, reinterpret_cast<GenericCallback>(&staticCallback<decltype(&MyClass::callbakcTwo), &MyClass::callbakcTwo, int>), this);
        registerAndCallCallback(2, reinterpret_cast<GenericCallback>(&staticCallback<decltype(&MyClass::callbakcThree), &MyClass::callbakcThree, int, float>), this);
    }

    void callbakcOne(double arg1) {}
    void callbakcTwo(int arg1) {}
    void callbakcThree(int arg1, float arg2) {}

    template<typename MemberCB, MemberCB cb, typename... Args>
    static void staticCallback(void* userdata, Args... args)
    {
        auto instance = reinterpret_cast<MyClass*>(userdata);
        (instance->*cb)(args...);
    }
};

int main()
{
    MyClass myclass;
    return 0;
}

How can I write the template function staticCallback so that I can just give it a member function to wrap and it can take care of the argument types etc for me?

jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • Maybe you can have the callbacks in a seperate namespace except of a class, that way you can already get rid of all the `MyClass::` stuff, and the many other complexities of class member function pointers – MivVG Jul 21 '18 at 16:42

1 Answers1

3

This question is quite similar and kudos to @fredbaba for the inspiration to solve this.

Anyway, to achieve this, you can use a singleton function factory pattern to get static instances of bindable function objects. The basic approach is to bind your class member function to a static function wrapper that has an invoke method taking the arguments the c library expects, forwarding and casting as necessary.

Tested with gcc 8.3 with -std=c++14

Demonstration:

template <typename TypeID, typename T, typename RetType, typename... Args>
struct FunctionFactory
{
public:
    static void bind(RetType(T::*f)(Args...)) {
        instance().fn_ = [f](T* t, Args... args) {
            return (t->*f)(std::forward<Args>(args)...);
        };
    }

    static RetType invoke(void* userdata, Args... args) {
        T * t = reinterpret_cast<T*>(userdata);
        return instance().fn_(t, std::forward<Args>(args)...);
    }

    typedef decltype(&FunctionFactory::invoke) pointer_type;
    static pointer_type ptr() {
        return &invoke;
    }

private:
    static FunctionFactory & instance() {
        static FunctionFactory inst_;
        return inst_;
    }

    FunctionFactory() = default;

    std::function<RetType(T*, Args...)> fn_;
};

template <typename TypeID, typename T, typename RetType, typename... Args>
typename FunctionFactory<TypeID, T, RetType, Args...>::pointer_type
getFunctionPtr(RetType(T::*f)(Args...))
{
    FunctionFactory<TypeID, T, RetType, Args...>::bind(f);
    return FunctionFactory<TypeID, T, RetType, Args...>::ptr();
}

class MyClass
{
public:
    MyClass()
    {
        registerAndCallCallback(0, reinterpret_cast<GenericCallback>(getFunctionPtr<0>(&MyClass::callbackOne)), this);
        registerAndCallCallback(1, reinterpret_cast<GenericCallback>(getFunctionPtr<1>(&MyClass::callbackTwo)), this);
        registerAndCallCallback(2, reinterpret_cast<GenericCallback>(getFunctionPtr<2>(&MyClass::callbackThree)), this);
    }

    void callbackOne(double arg1) {}
    void callbackTwo(int arg1) {}
    void callbackThree(int arg1, float arg2) {}
};
John Drouhard
  • 1,209
  • 2
  • 12
  • 18