boost signals allows temporarily blocking a connection via a connection member function. However, I have a single signal with many connections. The connections are stored and maintained by their respective listeners. Now the broadcaster decides that it wants to stop sending signals for a while. There does not seem to be a way to iterate all connections of a signal or disable the whole signal temporarily. This seems strange to me as surely such a mechanism must exist internally in order for the signal to reach all of its subscribers when signalled...
Am I missing something? How can I temporarily disable a signal?
views:
37answers:
1
A:
I don't know of any way to do that directly. If you are willing to permanently disconnect all slots, you can use the disconnect_all_slots()
method. For example:
boost::signal<int ()> foo;
...
foo.disconnect_all_slots();
If you need to temporarily block them, the best workaround I can come up with is to use a custom combiner that emulates that behaviour.
#include <boost/signals.hpp>
#include <iostream>
//Define a reusable combiner that allows all slots to be blocked
template <typename Combiner>
struct blockable {
typedef typename Combiner::result_type result_type;
blockable() : blocked(false), combiner() {}
//Block or unblock all slots
void block() {blocked = true;}
void unblock() {blocked = false;}
template <typename InputIterator>
result_type operator()(InputIterator first, InputIterator last) {
//Either call into inner combiner, or throw if all slots are blocked
if (!blocked) return combiner(first, last);
throw std::runtime_error("All slots are blocked");
}
private:
bool blocked;
Combiner combiner;
};
//Quick and dirty sample using the blockable combiner
int bar() {
return 1;
}
int main() {
boost::signal<int (), blockable<boost::last_value<int> > > foo;
foo.connect(&bar);
try {
//show that it works
int x = foo();
std::cout << x << std::endl;
//Now block all slots
foo.combiner().block();
int y = foo();
//This won't run since the last call to foo() should throw
std::cout << y << std::endl;
} catch (std::exception& e) {
//Should get here via 2nd call to foo()
std::cout << e.what() << std::endl;
}
return 0;
}
JRM
2010-08-06 15:01:27