Is there a way to connect a boost signal directly to another signal?
Asked Answered
A

1

7

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?

Arsenate answered 5/8, 2012 at 11:51 Comment(0)
A
9

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

Amharic answered 5/8, 2012 at 12:20 Comment(4)
Spot on! Exactly what I was looking for.Arsenate
The link seems to be dead. A related discussion can be found here: groups.google.com/forum/#!topic/boost-list/So4i8JXneJ0Bot
Q2: Will I get a crash if pB is subsequently released? A2: Yes PS I check this out - it's not crash !!!Jackleg
@Jackleg well, a more exact answer would be: it's an Undefined Behavior. So you may or may not get a "crash".Amharic

© 2022 - 2024 — McMap. All rights reserved.