Python 的 C++ 的 PubSub/观察者模式?
我正在寻找 Python PubSub 库的 C++ 替代品,其中我不必将信号与插槽等连接,而是可以注册特殊类型的消息,而不知道可以发送它的对象。
i'm looking for a C++ replacement of the Python PubSub Library in which i don't have to connect a signal with a slot or so, but instead can register for a special Kind of messages, without knowing the object which can send it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您误解了信号和槽是什么。 使用信号和槽,您不必知道谁发送信号。 您的“客户端”类仅声明插槽,外部管理器可以将信号连接到它们。
我建议你看看Qt。 它是一个令人惊叹的跨平台库,不仅仅提供 GUI 支持。 它具有方便高效的信号和槽实现可供您使用。
如今,它还获得了 LGPL 许可(除了 GPL 和商业许可),因此您几乎可以将它用于任何目的。
关于您的澄清评论,为什么不针对错误提出例外? 父母可以通知GUI,或者GUI可以注册父母发出的信号。 这样家长也不必了解 GUI。
Perhaps you misunderstand what signals and slots are. With signals and slots you don't have to know who sends signals. Your "client" class just declares slots, and an outside manager can connect signals to them.
I recommend you to check out Qt. It's an amazing cross-platform library with much more than just GUI support. It has a convenient and efficient implementation of signals and slots which you can use.
These days it's also licensed with LGPL (in addition to GPL and commercial), so you can use it for practically any purpose.
Re your clarification comment, why not raise an exception for the error? The parent can notify the GUI, or alternatively the GUI can register for a signal the parent emits. This way the parent also doesn't have to know about the GUI.
你可以使用 boost 库吗? 如果是这样,那么结合函数和绑定库可以让您执行以下操作。 如果您的编译器支持,您可以使用 tr1 功能执行相同的操作。
因此,当您运行此命令时,您会得到:
那么..这是您正在寻找的东西吗?
请参阅此处和此处了解大部分代码的源代码。
编辑: boost::signal 库也可能做你想做的事。
Can you use the boost libraries? If so then combining the function and bind libraries allows you to do the following. You may be able to do the same using the tr1 functionality if your compiler supports it.
So when you run this you get:
So.. is this the kind of thing you are looking for?
See here and here for the source of most of this code.
EDIT: The boost::signal library might also do what you want.
为什么不直接实施一个呢? 这不是一个复杂的模式(好吧,取决于你真正想要的)。 不管怎样,我前段时间已经实现了一个快速而肮脏的方法。 它没有经过优化、同步且单线程。 我希望你能用它来制作你自己的。
Why don't you just implement one? It's not a complicated pattern (well, depending what you really want). Anyway, I already implemented a quick and dirty one some time ago. It is not optimized, synchronous and single threaded. I hope you can use it to make your own.