Is it possible to add some condition to `boost::signal`

99 Views Asked by At

Is it possible to add some condition to boost::signal. It may get some Boolean function and when I emit signal it should check if function returns true then emit.

I don't want to check the condition during emitting because it is emitted it many places. I also don't want to check the condition in the slot because it should not know about that condition.

2

There are 2 best solutions below

0
On BEST ANSWER

If you need to emit signal from many places this way, I would add a method for it:

void emitSignal()
{
   if( /* condition */ ) {
       _signal();
   }
}

then you call emitSignal() instead of emitting signal directly.

0
On

I'd design this as an adaptor. This adaptor has a slot for the original signal, checks the condition, and emits a second signal if the test passes.