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

发布于 2024-08-16 23:07:24 字数 818 浏览 7 评论 0原文

我正在设置一个成员函数作为我正在使用的 C 库的回调。 C 库设置回调如下:

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

setCallback(param1, param2, functionPointer, param4)

我想使用 boost::bind (如果可能)来传递函数指针。我希望所指向的函数是实例化类的成员,而不是静态成员。例如,

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

这可以使用 boost::bind 和 boost::function 来完成吗?根据 如何将类成员函数传递为回调?(第三个答案)看来我可以声明以下内容(在某处,或作为 typedef):

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

然后在 A (构造函数)中的某处对该类型调用 boost::bind ,并传递它进入 C 库调用。

这可能吗,还是我偏离了基地?非常感谢。

I'm working through setting up a member function as a callback for a C-library that I'm using. The C-library sets up callbacks like this:

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. I would prefer that the function being pointed to was a member of the instantiated class, not a static member. E.g.

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? Per How can I pass a class member function as a callback? (the 3rd answer) it appears that I could declare the following (somewhere, or as a 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.

Is this possible, or am I off base? Thanks much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无所谓啦 2024-08-23 23:07:24

不会。像 boost::function 这样的函子类型不会转换为函数指针以与 C 回调机制一起使用。

但是,大多数 C 回调机制都有某种令牌机制,因此您的回调函数(静态)具有某种上下文信息。您可以使用它来编写一个包装类,将这些标记映射到函子对象,并将执行传递给正确的对象:

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;
};

No. Functor types like boost::function don't convert to function pointers for use with C callback mechanisms.

However, most C callback mechanisms have some kind of token mechanism, so your callback function (which is static) has some kind of context information. 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;
};
静若繁花 2024-08-23 23:07:24

不要使用映射,它会增加运行时开销,并且静态映射会使代码变得混乱。

请改用reinterpret_cast

例如

// 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.
    }
};

do not use map, it gives runtime overhead and clutter up code with static map.

use reinterpret_cast instead.

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.
    }
};
猫九 2024-08-23 23:07:24

成员函数的问题在于它们自动接收一个指向对象实例的指针作为第一个参数 - “this”指针。这就是为什么不能使用 C 回调函数中的成员函数。您必须将对象和函数指针放在一起才能使用成员函数。

The problem with member functions is that they automatically receive a pointer to object instance as the first parameter - "this" pointer. That's why you can't use member functions a C callback functions. You must have the object AND the function pointer together in order to use a member function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文