如果其中一个返回 true,有没有办法阻止 boost::signal 调用其插槽?
我正在使用 boost 库,我的问题是关于 boost::signals 的。
我有一个信号可能会调用许多不同的插槽,但只有一个插槽与调用匹配,因此我希望该特定插槽返回 true 并且调用将停止。
可能吗?
效率高吗?
如果效率不高,你们能建议我一个更好的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过一些研究,我发现在 boost 文档中他们写了 返回值的槽。
他们建议使用不同的组合器,如下所示:
现在为什么这样做是错误的?
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?
虽然这可能是可能的,但这肯定与信号和信号的“通用发布/订阅”意图相反。插槽。
我认为您真正寻找的是 责任链 设计模式。
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.
正如德鲁所说,这听起来并不适合信号和槽。
正如dribeas所说,解决方法是使用带有bool&的协议。 find 参数从 false 开始,每个槽在开始时进行检查,如果为 true,则返回。如果任何插槽将该值设置为 true,则其他调用的处理将会非常快地发生。
但为了涵盖所有基础(甚至是不建议的基础),我会提到,因为 boost::signals 是 全部与调用者在同一线程上运行,您可以从信号中抛出自定义异常,然后在以下位置捕获它呼叫站点。无论好坏,当人们觉得自己别无选择时,他们偶尔会诉诸于此……就像在 boost 图库中的访问者算法中一样:
使用自定义访问者时,如何停止使用 Boost Graph Library 进行广度优先搜索?
既然我已经提到了,请不要这样做。 :)
更新: 不知道,但你发现 boost 有一种机制可以通过采用迭代器而不是结果值的组合器来优雅地处理这个问题:
如果您确定自己坚持使用 boost,那么您就已经回答了自己的问题,因为这就是您想要的。不过请注意,其他信号/槽系统(例如 Qt 的)不会与此类似......
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...