简体   繁体   中英

Boost::signals2 - Passing a signal slot as argument

I'm using boost::signals2 to create a class that uses a signal to run an event under a specific condition. This class has a method that is called: RegisterCallback .

This function should take a parameter of the slot type that the signal uses (which is of void return type with a double as the only arg).

However, I'm not quite sure how I should define that argument in the method signature, as the source file for the signal1 class is quite unreadable for me.

So I basically want to do this:

myTimer(interval);
myTimer.RegisterCallback(&aCallbackFunction);

Here is the member signal:

boost::signals2::signal<void (double)> m_signal;

... and here is the register method that I need (uncomplete)

   /**
    * Register a callback function that will be called when the timer interval elapses.
    */
    void RegisterCallback(const boost::signals2::signal1<???????> &slot)
    {
        m_signal.connect(slot);
    }

You can also use the boost::function and boost::bind library I think.

typedef booost::signals2::signal(void (double)>::slot_type CallbackSlot
void RegisterCallback(CallbackSlot slot)
{
    m_signal.connect(slot);
}

// ...

class MyClass {void handler(double);}

// ...

RegisterCallback(boost::bind(MyClass::handler, this, _1));

I propose that you also read the boost::function and boost::bind documentation to use signals2 in a good way. This example code is not tested but contains all the stuff needed to solve your problem.

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