I am searching for a way to implement something like this, using boost
class GenBoost{
boost::signal<void(void)> m_signal;
std::function<void (bool)> m_function
public:
void setSignal(boost::signal<void(void)> sigArg)
{
m_signal = sigArg;
}
void setFunction(std::function<void (bool)> &functionArg)
{
m_function = functionArg;
m_signal.connect(boost::bind(&GebBoost::onSignal,this,_1));
}
void onSignal(){
//do something
}
};
How can this be achieved. signal copying is not possible!?
I am not 100% sure of your intent, but assuming that
onSignal()will not need to interact withm_signal(and that you only need one connection tom_signal), it appears that you can decouplem_signalfrom your class entirely. For example, if you really do not want the body of 'onSignal()' to be called until a 'm_function' has been set, you could do something like:Note that I stripped the last
_1fromm_signal.connect(boost::bind(&GebBoost::onSignal,this,_1));because the signal is declared as typevoid(void)so should be connected to avoidcallback. I also added the scoped_connection so that if the object of typeGenBoostis destroyed before the signal it is connected to, it will automatically disconnect rather than allow the signal to retain an invalid pointer.Alternatively, if you needed to retain a reference to the original signal, you could add back your original signal member, but as a pointer
boost::signal<void(void)>* m_signal;. ThensetSignalbecomes:I still recommend using the scoped connection and connecting in
setSignalso that you ensure you only have one connection to one signal (IfsetSignalis called twice, the connection to the first signal is automatically disconnected). If you go the pointer route, you must externally ensure that the passed-in signal has a lifetime longer than theGenBoostobject.