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

本小妞迷上赌 提交于 2019-12-09 11:31:02

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/11816161/is-there-a-way-to-connect-a-boost-signal-directly-to-another-signal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!