c++ - 如何将 C++ 类函数作为回调传递

标签 c++

我在理解如何将 this 传递给传递给库的回调函数时遇到一些问题。该函数需要具有特定的签名。

有问题的库是 OCILIB(https://vrogier.github.io/ocilib/doc/html/classocilib_1_1_subscription.html)并试图将类函数作为 Register() 的第四个参数传递。

我可以毫无问题地通过它

&database::callback // a static function of database

[](ocilib::Event &event) // as lambda function
{
}

但它没有访问实例变量的权限。我试着像这样使用它

[&](ocilib::Event &event) // as lambda function
{
}

但签名不匹配,我得到以下错误

database.cpp: In member function ‘bool dcn::database::watch(std::__cxx11::string)’:
database.cpp:104:44: error: no matching function for call to ‘ocilib::Subscription::Register(ocilib::Connection&, std::__cxx11::string&, ocilib::Subscription::ChangeTypesValues, dcn::database::watch(std::__cxx11::string)::<lambda(ocilib::Event&)>, unsigned int, unsigned int)’
    }, (unsigned int) 7778, (unsigned int) 0);
                                            ^
In file included from /usr/include/ocilib.hpp:9194:0,
                 from /home/ai/dcn/include/main.h:17,
                 from database.cpp:1:
/usr/include/ocilib_impl.hpp:6650:13: note: candidate: void ocilib::Subscription::Register(const ocilib::Connection&, const ostring&, ocilib::Subscription::ChangeTypes, ocilib::Subscription::NotifyHandlerProc, unsigned int, unsigned int)
 inline void Subscription::Register(const Connection &connection, const ostring& name, ChangeTypes changeTypes, NotifyHandlerProc handler, unsigned int port, unsigned int timeout)
             ^~~~~~~~~~~~
/usr/include/ocilib_impl.hpp:6650:13: note:   no known conversion for argument 4 from ‘dcn::database::watch(std::__cxx11::string)::<lambda(ocilib::Event&)>’ to ‘ocilib::Subscription::NotifyHandlerProc {aka void (*)(ocilib::Event&)}’
make[1]: *** [database.o] Error 1

函数定义为

static void callback(ocilib::Event &);

需要您的帮助来解决这个问题。提前致谢。

最佳答案

那是一个可怕的 API。任何拥有回调函数但不要求 void* 的人都在编写我们知道在 70 年代是个坏主意的代码。

您别无选择,只能使用全局状态分派(dispatch)回您的类(class)。

template<std::size_t N, class R, class...Args>
struct crappy_api_fix{
  static std::array< std::function<R(Args&&...)>, N >& table(){
    static std::array< std::function<R(Args&&...)>, N > arr;
    return arr;
  }
  template<std::size_t I>
  static R call( Args...args ){ return table()[I]( std::forward<Args>(args)... ); }
  using sig=R(Args...);
  template<std::size_t I=N-1>
  static sig* make(std::function<R(Args&&...)> f){
    if(!table()[I]){
      table()[I]=f;
      return &call<I>;
    }
    if(I==0) return nullptr;
    return make< (I-1)%N >(f);
  }
  template<std::size_t I=N-1>
  static void recycle( sig* f ){
    if (f==call<I>){
      table()[I]={};
      return;
    }
    if (I==0) return;
    recycle< (I-1)%N >( f);
  }
};

类似的东西。它维护一个包含 N 个标准函数的全局表,并返回一个知道要调用哪个标准函数的函数指针。

// up to 50 callbacks alive at once:
using cb_helper = crappy_api_fix<50, void, ocilib::Event &>;

// "stateless" function pointer wrapping a lambda:
void(*f)(ocilib::Event&) = cb_helper::make([&](ocilib::Event &event) {});

// blah is the API class with the Register method.  We pass the f from above:
blah->Register(arg1, arg2, f, arg4);

// This might be far away fron the above code:
// we should keep copy of the f; when we have unregistered, we shoukd recyle it:
cb_helper::recycle(f); // when `f` is no longer needed

抱歉打字错误,这是在我手机上输入的。

关于c++ - 如何将 C++ 类函数作为回调传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56352417/

相关文章:

c++ - Protobuf 默认值

c++ - 通过近似选择颜色区域

c++ - 确定字符串是否为 double

c++ - 这是C++中最好的分配方式

c++ - 无法成功比较字符串

c++ - 关于具有虚拟继承的程序输出的混淆

c++ - 如何将 C++ 对象保存到 xml 文件中并恢复?

c++ - boost::变体。 boost::重载函数的访问者

c++ - 可移植可执行文件的保存位置

c++ - std::cout 函数作为参数