当C ++应用程序使用API中具有回调的C库时,该应用程序的常见模式是定义静态函数,这些函数将某些void*
用户数据参数转换为类指针,然后调用适当的成员功能。复制浪费了写作和阅读的时间。拥有模板功能为我完成此包装将非常好。
我已经有办法了...
// 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;
}
如何编写模板函数staticCallback
,以便为它提供一个要包装的成员函数,并为我处理参数类型等?
答案 0 :(得分:2)
This question与@fredbaba非常相似,并为解决此问题提供了灵感。
无论如何,要实现此目的,可以使用单例函数工厂模式来获取可绑定函数对象的静态实例。基本方法是将您的类成员函数绑定到一个静态函数包装器,该包装器具有一个调用方法,该方法带有c库期望的参数,并根据需要转发和强制转换。
Tested with gcc 6.3 with -std=c++14
演示:
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) {
(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) {}
};