1. /*
  2. Andy is going to hold a concert while the time is not decided.
  3. Eric is a fans of Andy who doesn't want to miss this concert.
  4. Andy doesn't know Eric.
  5. How can Eric gets the news when Andy's concert is going to take?
  6. */
  7. /*
  8. Singer:被观察者
  9. Fans:观察者
  10. */
  11. #include "stdafx.h"
  12. #include <iostream>
  13. #include <boost/signals2.hpp>
  14. #include <boost/bind.hpp>
  15. #include <string>
  16. using namespace std;
  17. struct Singer
  18. {
  19. //定义信号的类型,也就是说 Singer 需要知道 Fans 的响应方式
  20. //也就是说 Singer 需要知道 Fans 会采用什么样的方式来响应 Singer 所发出的信号
  21. //或者说 Singer 需要知道 Fans 用什么样的方式来接受 Singer 发出的信号
  22. //就相当于 Singer 需要知道 Fans的“邮箱”,到时候才可以将信息投递到 Fans 的“邮箱”类型中去
  23. //Fans 的“邮箱”类型—— void (string time)
  24. typedef boost::signals2::signal<void (string time)> signalType;
  25. typedef signalType::slot_type slotType;
  26. signalType m_signal; //定义一个信号
  27. //Singer 发布信号
  28. void PublishTime(string time)
  29. {
  30. m_signal(time); //将包含 time 信息的信号m_signal投递到 Fans 的邮箱中去,注意,投递之前这种类型的邮箱必须要和一个具体的Fans联系起来,即必须知道是谁拥有这种类型的邮箱,这一动作通过后边的Subscribe实现。
  31. }
  32. //Singer 提供一种注册渠道:Fans们可以通过这个渠道来进行注册,告诉Singer,有新信号的话就发送给我一个消息
  33. boost::signals2::connection Subscribe(const slotType& fans)
  34. {//在这里将Fans与Singer建立起一种联系(connection)
  35. //到后面可以发现,Fans需要调用这个函数,即通过这个渠道告诉Singer有消息就要通知给我
  36. return m_signal.connect(fans);
  37. }
  38. };
  39. struct Fans
  40. {
  41. // m_connection:联系的存在是在整个Fans的生命周期内的,一旦Fans消失,这种联系也就不复存在了
  42. boost::signals2::scoped_connection m_connection;
  43. //Fans的响应方式,也就是Fans的邮箱类型,至于里面具体做什么事情,Singer不需要知道。
  44. void Correspond(string time)
  45. {
  46. cout<<"I know the concert time: "<<time<<endl;
  47. }
  48. //Fans需要自己确定他要关注(观察)哪一个Singer 的动向
  49. void Watch(Singer& singer)
  50. {
  51. //通过调用Singer的Subscribe函数(渠道)来将自己的邮箱地址告知Singer
  52. m_connection = singer.Subscribe(boost::bind(&Fans::Correspond, this, _1));
  53. }
  54. };
  55. int main(int argc, char* argv[])
  56. {
  57. Singer  Andy; //刘德华
  58. Fans    Eric; //Eric
  59. Eric.Watch(Andy); //Eric告知刘德华:我要关注你的动向,请把你的最新信息发给我
  60. Andy.PublishTime("2010/10/01");//刘德华发布最新信息,一旦信息发布,Eric的邮箱——void Correspond(string time)就会接受到信息,并进行响应——cout<<….
  61. return 0;
  62. }

Reference:

http://www.cppprog.com/boost_doc/doc/html/signals2/tutorial.html

http://www.cppprog.com/2009/0430/111.html

http://www.cppprog.com/boost_doc/

BOOST::Signals2的更多相关文章

  1. Boost信号/槽signals2

    信号槽是Qt框架中一个重要的部分,主要用来解耦一组互相协作的类,使用起来非常方便.项目中有同事引入了第三方的信号槽机制,其实Boost本身就有信号/槽,而且Boost的模块相对来说更稳定. signa ...

  2. boost之signal

    boost里的signal是一个模板类,不区分信号种类,产生信号统一用()调用操作符. 1.回调普通函数代码示例: #include <iostream> #include <str ...

  3. Using Boost Libraries in Windows Store and Phone Applications

    Using Boost Libraries in Windows Store and Phone Applications RATE THIS Steven Gates 18 Jul 2014 5:3 ...

  4. 观察者模式与Boost.Signals

      1)  观察者模式定义 略,各种设计模式的书上都有定义. 2)  观察者模式一般实现 观察者模式一般实现,都是“被观察者”保存一个“观察者”的列表,循环这个列表来通知“观察者”.代码,其中使用了b ...

  5. boost signal2 slot_base

    先看成员_tracked_objects,从字面上讲是被跟踪的对象,再看,相关函数 bool expired() const,这个函数是检查_tracked_objects是否已经expired.只不 ...

  6. boost事件处理

    尽管这个库的名字乍一看好象有点误导,但实际上并不是如此. Boost.Signals 所实现的模式被命名为 '信号至插槽' (signal to slot).它基于下面概念:当相应的信号被发出时.相关 ...

  7. boost::signals::signal的使用方法

    吃力的讲完boost::signals的ppt.然后接着就是做练习题. 通过讲ppt,发现有一句话说的真好:你自己知道是一回事.你能给别人讲明确又是另外一回事.真的有些东西你自己理解,可是用语言去非常 ...

  8. boost------signals2的使用1(Boost程序库完全开发指南)读书笔记

    signals2基于Boost的另一个库signals,实现了线程安全的观察者模式.在signals2库中,观察者模式被称为信号/插槽(signals and slots),他是一种函数回调机制,一个 ...

  9. boost------signals2的使用2(Boost程序库完全开发指南)读书笔记

    1.应用于观察者模式 本小节将使用signals2开发一个完整的观察者模式示例程序,用来演示信号/插槽的用法.这个程序将模拟一个日常生活场景:客人按门铃,门铃响,护士开门,婴儿哭闹. Ring.h: ...

随机推荐

  1. traceroute原理

    traceroute原理 ICMP ICMP全称为Internet Control Message Protocol,即,网络控制报文协议. 当一个IP数据报发送失败时,最后一个路由器会向发送发传递一 ...

  2. 将16进制(HTML)颜色值转换成 Color类型

    private void btnChangeColor_Click(object sender, EventArgs e) { txtColor.BackColor = ColorTranslator ...

  3. http status 源码

    private static readonly String[][] s_HTTPStatusDescriptions = new String[][] { null, new String[] { ...

  4. 附加到IIS调试出现不会命中断点

    当项目附加到IIS进行调试时,如果在IIS中没有配置该项目则在设置断点是会出现:当前不会命中断点 还没有为该文档加载任何符号

  5. (转)[老老实实学WCF] 第一篇 Hello WCF

    http://blog.csdn.net/songyefei/article/details/7363296#comments 老老实实学WCF  第一篇 Hello WCF WCF(Windows ...

  6. React-nwb的使用

    一.查看nwb的版本 nwb -v 二.创建一个react项目 nwb new react-app react-demo 三.启动项目 nwb serve

  7. tomcat免安装版注册为系统服务

    环境: OS:windows7_64bit JDK:jdk1.6_64bit tomcat:apache-tomcat-7.0.61-windows-x64 1.修改tomcat/bin/servic ...

  8. 原创:2016.4.25-2016.5.1 C# informal essay and tittle_tattle

    1.Some  tips of the Time in C sharp (1) How to define the category of the "Datetime"? date ...

  9. android之ListView,详细介绍实现步骤,举例,自定义listview适配器

    android之ListView,详细介绍实现步骤,举例,自定义listview适配器 本文来源于www.ifyao.com禁止转载!www.ifyao.com android中如何使用listVie ...

  10. Aphache VFS

    http://blog.csdn.net/hemingwang0902/article/details/4733911 http://jackyrong.iteye.com/blog/1330946 ...