c++ - boost signal-slot generalisation -
i searching way implement 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 } };
how can achieved. signal copying not possible!?
i not 100% sure of intent, assuming onsignal()
not need interact m_signal
(and need 1 connection m_signal
), appears can decouple m_signal
class entirely. example, if not want body of 'onsignal()' called until 'm_function' has been set, like:
class genboost{ std::function<void (bool)> m_function; boost::signals::scoped_conection m_connection; public: void setsignal(boost::signal<void(void)>& sigarg) { m_connection = sigarg.connect(boost::bind(&gebboost::onsignal,this)); } void setfunction(std::function<void (bool)> &functionarg) { m_function = functionarg; } void onsignal() { if ( m_function ) do_work(); } void do_work() { //do } };
note stripped last _1
m_signal.connect(boost::bind(&gebboost::onsignal,this,_1));
because signal declared type void(void)
should connected void
callback. added scoped_connection if object of type genboost
destroyed before signal connected to, automatically disconnect rather allow signal retain invalid pointer.
alternatively, if needed retain reference original signal, add original signal member, pointer boost::signal<void(void)>* m_signal;
. setsignal
becomes:
void setsignal(boost::signal<void(void)>& sigarg) { m_signal = &sigarg; m_connection = m_signal->connect(boost::bind(&gebboost::onsignal,this)); }
i still recommend using scoped connection , connecting in setsignal
ensure have 1 connection 1 signal (if setsignal
called twice, connection first signal automatically disconnected). if go pointer route, must externally ensure passed-in signal has lifetime longer genboost
object.
Comments
Post a Comment