简体   繁体   English

类成员函数使用boost :: bind和boost :: function作为回调函数

[英]Class member function as callback using boost::bind and boost::function

I'm working through setting up a member function as a callback for a C-library that I'm using. 我正在设置一个成员函数作为我正在使用的C库的回调。 The C-library sets up callbacks like this: C库设置如下的回调:

typedef int (*functionPointer_t)(myType1_t*, myType2_t*, myType3_t*);

setCallback(param1, param2, functionPointer, param4)

I would like to use boost::bind (if possible) to pass in the function pointer. 我想使用boost :: bind(如果可能的话)传入函数指针。 I would prefer that the function being pointed to was a member of the instantiated class, not a static member. 我希望指向的函数是实例化类的成员,而不是静态成员。 Eg 例如

Class A {
 public: 
  A();
 protected:
  int myCallback(myType1_t*, myType2_t*, myType3_t*); //aka functionPointer_t
}

Can this be done using boost::bind and boost::function? 可以使用boost :: bind和boost :: function来完成吗? Per How can I pass a class member function as a callback? Per 如何将类成员函数作为回调传递? (the 3rd answer) it appears that I could declare the following (somewhere, or as a typedef): (第3个答案)似乎我可以声明以下内容(某处或作为typedef):

boost::function<int (A*, myType1_t*, myType2_t*, myType3*> myCallbackFunction

And then somewhere in A (the ctor) call boost::bind on that type, and pass it into the C-library call. 然后在A(ctor)的某处调用boost :: bind在该类型上,并将其传递给C库调用。

Is this possible, or am I off base? 这可能吗,还是我不在基地? Thanks much. 非常感谢。

No. Functor types like boost::function don't convert to function pointers for use with C callback mechanisms. 不会。如boost::function这样的Functor类型不会转换为与C回调机制一起使用的函数指针。

However, most C callback mechanisms have some kind of token mechanism, so your callback function (which is static) has some kind of context information. 但是,大多数C回调机制都有某种令牌机制,因此您的回调函数(它是静态的)具有某种上下文信息。 You can use this to write a wrapper class which maps these tokens to functor objects, and passes execution along to the right one: 您可以使用它来编写一个包装类,将这些标记映射到仿函数对象,并将执行传递到右侧:

class CallbackManager {
public:
    typedef boost::function<int (type1*, type2*, type3*)> callback;

    static void setCallback(CallbackManager::callback cb)
    {
        void *token = ::setCallback(staticCallback);
        callbacks[token] = callback_I;
    }

    static void staticCallback(void* token, type1* a, type2* b, type3* c)
    { return mcallbacks[token](a, b, c); }

private:
    static std::map<void*, callback > callbacks;
};

do not use map, it gives runtime overhead and clutter up code with static map. 不使用map,它会给运行时开销带来静态映射并使代码混乱。

use reinterpret_cast instead. 请改用reinterpret_cast

for instance 例如

// clib.h
typedef void (*CALLBACK_FUNC)(int code,void *param);

void set_callback( CALLBACK_FUNC, void * param ); 

// a.h

class A {
public:
    A()
    {
        ::set_callback( &A::static_callback, this);
    }
private:
    static void static_callback(int code, void * param)
    { 
        A* self = reinterpret_cast<A*>(param);
        self->callback( code );
    }

    inline void callback( int code )
    {
        // write you code here.
    }
};

The problem with member functions is that they automatically receive a pointer to object instance as the first parameter - "this" pointer. 成员函数的问题在于它们自动接收指向对象实例的指针作为第一个参数 - “this”指针。 That's why you can't use member functions a C callback functions. 这就是为什么你不能使用成员函数C回调函数。 You must have the object AND the function pointer together in order to use a member function. 您必须将对象和函数指针放在一起才能使用成员函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM