gpt4 book ai didi

c++ - 创建一个模板来包装 C++ 成员函数并公开为 C 回调

转载 作者:太空狗 更新时间:2023-10-29 21:33:22 24 4
gpt4 key购买 nike

当 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 以便我可以只给它一个成员函数来包装并且它可以为我处理参数类型等?

最佳答案

This question非常相似,感谢@fredbaba 的启发来解决这个问题。

无论如何,要实现这一点,您可以使用单例函数工厂模式来获取可绑定(bind)函数对象的静态实例。基本方法是将您的类成员函数绑定(bind)到一个静态函数包装器,该包装器具有一个调用方法,该方法采用 c 库期望的参数,并根据需要进行转发和转换。

Tested with gcc 8.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) {
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) {}
};

关于c++ - 创建一个模板来包装 C++ 成员函数并公开为 C 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51451843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com