Search code examples
c++boostboost-signals2

How to use Boost.Signals2 connect_extended?


I have a working Boost.Signals2 signal & slot combination in my C++ project & set up like so;

//DECLARE SIGNAL
signals2::signal<void (const EN_DATA_STREAM, long, double, double, double, double,     double)> signal;

//CONNECT DATAUPDATE() OF CANDIDATE INSTANCE
signal.connect(bind(&Candidate::DataUpdate, candidateInstance, _1, _2, _3, _4, _5, _6, _7));
    
//FIRE SIGNAL
signal(iDataNumber, BarNumber(), DateTime(), Open(), High(), Low(), Close());

I've been trying to take this further and use the boost.signals2 connect_extended functionality as I'd like to pass details of the invoking signal to the slot so that the slot may disconnect itself from the signal at some later time. The syntax for this is escaping me. Please could someone demonstrate how to convert the above code so that it uses connect_extended to pass connection information to the slot.

P.S. I've been looking at this example provided at the boost website but am still none the wiser as to how to tailor it to my requirements where the parameters use bind.

Boost.Signals2 connect_extended example

Thanks


Solution

  • The only advantage of connect_extended is that it allows the slot to receive connection object, which may be crucial in a multi-threaded program, where slot invocation might occur in another thread before connect_extended returns. You do not need connect_extended to pass any other information, as you can bind it directly. Anyway, here is how you can do this with connect_extended:

    #include <boost/signals2.hpp>
    
    using namespace boost::signals2;
    
    void your_slot(connection conn, int, double, int, char)
    {
    }
    
    int main()
    {
      signal<void(int, double, int)> your_signal;
      your_signal.connect_extended(boost::bind(&your_slot, _1, _2, _3, _4, 'a'));
      your_signal(1, 2.0, 3);
    }