C++ member function pointer, boost::signals

2019-04-14 16:17发布

问题:

I have the following situation, (better in code)

    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
     }  

So the question is, how i pass a 'member-function' pointer to the Register method. Also, is this ok? What I want/need, is some sort of delegate registration system, so if anynone could point my in the right direction I'll appreciate it. Thanx in advance.

回答1:

You would typically use boost bind:

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

What's going on here? :-)

The connect method of the signal takes a functor. That is an object that implements the () operator. bind takes function pointers (either to free functions or member functions) and returns a functor with the right signature.

Also see here:

Complete example using Boost::Signals for C++ Eventing

and here:

how boost::function and boost::bind work

To disconnect a signal store the return value from connect into a:

boost::signals::connection

And then call the disconnect method on that.



回答2:

Typically you'll either do:

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

Or, as an inline function:

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

The latter may be slightly more efficient by removing the layer of indirection boost::function has - but only assuming boost::signal doesn't use boost::function internally (which it is likely to). So use whichever one you prefer, really.



回答3:

I got it working after trying a lot, here's the code:

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

well this code works as it should be, with one exception, if I unccomment the deviceLost.disconnect(handler) statement, I receive compilation errors like: error C266 "boost::operator ==": 4 overloads have similar conversions.

So, why is this happening? Do you know any other way to accomplish what I'm trying?



回答4:

In case anyone wants a full example:

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