I am using the boost library and my question is about boost::signals.
I have a signal that might call many different slots but only one slot will match the call so I want this particular slot to return true and that the calling will stop.
Is it possible?
Is it efficient?
Can you guys suggest me a better way to do it if it's not efficient?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
As Drew says, this doesn't sound befitting of signals and slots.
And as dribeas says, a workaround is a protocol with a
bool& found
parameter that starts out false, with every slot checking at the start and returns if its true. If any slot sets the value to true, then processing of the other calls will happen very quickly.But just to cover all the bases (even the inadvisable ones), I'll mention that since boost::signals are all run on the same thread as the caller you could throw a custom exception from within the signal, then catch it at the call site. For better or worse, people occasionally resort to this when they feel they have no other option...like during visitor algorithms in the boost graph library:
How do I stop the breadth-first search using Boost Graph Library when using a custom visitor?
And now that I've mentioned it, don't do it that way. :)
UPDATE: Didn't know it but you found that boost has a mechanism for handling this elegantly with combiners that take iterators and not result values:
If you're sure you're sticking with boost then you've answered your own question because that does what you want. Though do note that other signal/slot systems (like Qt's) won't have a parallel to this...
While it may be possible, it's certainly contrary to the "generic publish/subscribe" intent of signals & slots.
I think what you're really looking for is the Chain of Responsibility design pattern.
After some research I've found that in boost documentation they write about Slots that return values.
They suggest to use a different combiner like this:
Now why is that the wrong thing to do?