简体   繁体   中英

Is there a way to connect a boost signal directly to another signal?

I was wondering if there is a nicer way to connect a Boost signal of one class directly to a signal of another class?

For example imagine a facade class with a bunch of members which provide their own signals. Now assume that the facade wants to expose these signals. I usually end up writing boilerplate methods which I then connect as signal handlers.

using namespace boost::signal;

class A
{
public:
  A(){};
  virtual ~A(){};

  signal<void()> signalA;
};

class B
{
public:
  B(){};
  virtual ~B(){};

  signal<void()> signalB;
};

class Facade
{
private:
  A& a;
  B& b;

public:
  Facade(A& refA, B& refB) 
  : a(refA), b(refB) 
  {
    // connect A's signal to facadeSignalA
    a.signalA.connect(boost::bind(&Facade::forwardedSignalA, this));
    // connect B's signal to facadeSignalB
    b.signalB.connect(boost::bind(&Facade::forwardedSignalB, this));
  }
  virtual ~Facade() {};

  // user visible signals
  signal<void()> facadeSignalA;
  signal<void()> facadeSignalB;

private:
  // ugly boilerplate code used to forward signals
  void forwardedSignalA()
  {
    facadeSignalA();
  }
  void forwardedSignalB()
  {
    facadeSignalB();
  }
};

Now this is not very elegant and becomes very tedious after while. Is there a way to do this without having to write these kinds of forwarding methods?

Yes, it turns out that you can "chain" signals directly. Please see this thread . It's undocumented, but it seems a very useful feature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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