disconnect_all_slots()
方法。例如:
boost::signal<int ()> foo;
...
foo.disconnect_all_slots();
如果您需要临时阻止它们,我能想到的最佳解决方法是使用一个模拟该行为的自定义组合器。
#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;
}