2

我有以下情况,(代码更好)

    class Foo
    {
private:
       typedef boost::signal<void ()> Signal;
       Signal signal;

public:
       void Register_SignalFunction(const Signal::slot_type& slot);
       void Unregister_SignalFunction(const Signal::slog_type& slot);
    }

    class OtherFoo
    {
       Foo foo;
public:
       OtherFoo()
       {
            foo.Register_SignalFunction(&OnSignal)    //I know I can't do this that is precisely my question.
       }

       void OnSignal();  //what to do when signal fires
     }  

所以问题是,我如何将“成员函数”指针传递给 Register 方法。还有,这样可以吗?我想要/需要的是某种代表注册系统,所以如果有人能指出我正确的方向,我将不胜感激。提前谢谢。

4

4 回答 4

4

您通常会使用 boost 绑定:

foo.Register_SignalFunction(boost::bind(&OtherFoo::OnSignal, this));

这里发生了什么?:-)

信号的连接方法采用函子。那是一个实现 () 运算符的对象。bind 接受函数指针(指向自由函数或成员函数)并返回具有正确签名的函子。

另见此处:

使用 Boost::Signals 进行 C++ 事件的完整示例

和这里:

boost::function 和 boost::bind 是如何工作的

要断开信号,请将连接的返回值存储到:

boost::signals::connection

然后在上面调用disconnect方法。

于 2011-08-23T05:05:54.660 回答
1

通常你会这样做:

void Register_SignalFunction(const boost::function<void()> &slot) {
    signal += slot;
}

或者,作为内联函数:

template<typename T>
void Register_SignalFunction(T &slot) {
    signal += slot;
}

通过删除间接层,后者可能会稍微更有效boost::function- 但只是假设boost::signal不在boost::function内部使用(它可能会)。所以使用你喜欢的任何一个,真的。

于 2011-08-23T04:17:07.103 回答
0

经过多次尝试,我得到了它的工作,这是代码:

GraphicsDeviceManager
{
  private:
      typedef boost::signal<void ()> DeviceLost;
      DeviceLost deviceLost;

  public:
      Register_DeviceLostHandler(const boost::function<void ()> &handler)
      {
          deviceLost.connect(slot);
      }
      Unregister_DeviceLostHandler(const boost::function<void ()> &handler)
      {
          //deviceLost.disconnect(slot); 
      }
}

class GameBase
{
  private:
    GraphicsDeviceManager* graphics;

   public:
       GameBase()
       {
          graphics = new GraphicsDeviceManager();
          graphics->Register_DeviceLostHandler(boost::bind(&GameBase::OnDeviceLost, this));
       }
       void OnDeviceLost()
       {
           //do some stuff
       }
}

这段代码可以正常工作,但有一个例外,如果我取消对 deviceLost.disconnect(handler) 语句的注释,我会收到如下编译错误:error C266 "boost::operator ==": 4 重载具有类似的转换。

那么,为什么会这样呢?你知道任何其他方式来完成我正在尝试的事情吗?

于 2011-08-23T17:38:42.610 回答
0

如果有人想要一个完整的例子:

#include <iostream>
#include <boost/signals2/signal.hpp>
#include <boost/bind.hpp>
#include <boost/optional/optional_io.hpp>

#define registerEvent_(A)   registerEvent(boost::bind(A, this, _1, _2))

struct A
{
    typedef boost::signals2::signal<int (int &, int &)> EventSignal;
    typedef EventSignal::slot_type SlotType;

    void registerEvent(const SlotType & slot);
    void triggerAll(int& a1, int& a2);

    EventSignal signal_;
};

void A::registerEvent(const SlotType & slot) { signal_.connect(slot); }
void A::triggerAll(int& a1, int& a2) {std::cout << signal_(a1, a2) << "\n";}

struct B : public A
{
    B();
    int myFunc(int& a1, int& a2);
};

B::B() {
#ifdef WITHMACRO
    registerEvent_(&B::myFunc);
#else
    registerEvent(boost::bind(&B::myFunc, this, _1, _2));
#endif
}
int B::myFunc(int& a1, int& a2) { return a1 + a2 + 1; }

int main()
{
    int a1 = 2;
    int a2 = 3;
    B b;
    b.triggerAll(a1, a2);
}
于 2017-09-15T14:54:02.567 回答