C++ 通知中心
在使用 iOS 和 Mac Objective C 框架进行了一段时间的编程之后,我开始喜欢上 NSNotificationCenter 和 NSNotification 类实现的通用通知模式。回到 C++,它一直是我在大多数事情上选择的语言,我发现自己正在尝试复制这种模式,并相信实际上应该已经有一个类似的 C++ 类的通用实现来提供对其的支持。
由于后者更具动态性,该模式在 C++ 中的实现似乎比 Objective C 更困难,但这似乎远非不可能。我浏览了 boost 库,因为它们通常都很棒,但很遗憾没有在那里找到我的运气。尽管 boost::bind、boost::lamda、boost::function 似乎完成了大部分工作。我错过了一些明显的事情吗?是否已经存在任何东西可以让我轻松复制 NSNotification/NSNotificationCenter 行为?
After programming for sometime with the iOS and Mac objective C frameworks, I have come to love the generic notification pattern implemented by the NSNotificationCenter and NSNotification classes. Coming back to C++, which has always been my language of choice for most things, I find myself trying to replicate this pattern and believe there should really already be a generic implementation of similar C++ classes offering support for it out there.
It does seem like the pattern is somewhat more difficult to implement in C++ than Objective C because of the more dynamic nature of the later, but it seems far from impossible. I've looked through the boost libraries as they are generally awesome and was sad not to find my luck there. Although the boost::bind, boost::lamda, boost::function seem like they do most of the work. Have I missed something obvious? Is there anything already existing out there that would allow me to easily replicate NSNotification/NSNotificationCenter behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
理论上,您可以创建一个类,该类具有在调用某个通知时要调用的函数指针向量 - 一个具有字典的类,其中对象是推送通知时要调用的函数向量
In theory you could create a class that has a vector of function pointers to call when a certain notification is called - A class that has a dictionary where the objects are the vectors of functions to call when a notification is pushed
除了其他答案中提到的
boost
包之外,另一个选择是poco::NotificationCenter
。此实现更接近 Cocoa 通知框架,正如 Poco 的文档中具体讨论的那样:
In addition to the
boost
packages mentioned in other answers, another option ispoco::NotificationCenter
.This implementation is closer to the Cocoa notification framework, as specifically discussed on Poco's documentation:
按照 @anno 建议查看 boot::signal 的建议,经过检查,它似乎是一个可能的选择,尽管正如预期的那样,它不像目标 C 解决方案那么简单。浏览 boost::signal 教程,我想我会讨论与当前问题最相关的方面。
创建通知发送者:
deliverNews
的目标是通知观察者NewsItem
已生成。可以按如下方式添加观察者(使用 boost::bind 库):
为了解决从列表中删除已释放的观察者的问题,boost::signal 提供了以下解决方案
Following @anno's recommendation to look at boot::signal, it does after examination seem like a possible option although it is, as expected, not as straight-forward as the objective C solutions. Looking through the boost::signal tutorial, I thought I would go through the most relevant aspects for the problem at hand.
To create notification senders:
The objective of
deliverNews
is to inform observers that aNewsItem
has been generated.Observers can be added as follows (using the boost::bind library):
To address the problem of removing observers which have been deallocated from the list, boost::signal offers the following solution