boost------signals2的使用

来源:互联网 发布:2017dnf商人起步必知 编辑:程序博客网 时间:2024/05/29 04:59
小节将使用signals2开发一个完整的观察者模式示例程序,用来演示信号/插槽的用法。这个程序将模拟一个日常生活场景:客人按门铃,门铃响,护士开门,婴儿哭闹。


Ring.h

[cpp] view plain copy
 print?
  1. #ifndef __RING_H__  
  2. #define __RING_H__  
  3.   
  4. #include "iostream"  
  5. using namespace std;  
  6. #include "boost/signals2.hpp"  
  7.   
  8.   
  9. class Ring  
  10. {  
  11. public:  
  12.     typedef boost::signals2::signal<void()> signal_t;   //创建一个信号 
  13.     typedef signal_t::slot_type slot_t;         //定义信号类型
  14.   
  15.     boost::signals2::connection connect(const slot_t& s)  
  16.     {  
  17.         return alarm.connect(s);           //链接alarm响了与链接函数的关联
  18.     }  
  19.   
  20.     void Press()  
  21.     {  
  22.         cout << "Ring alarm..." << endl;  
  23.         alarm();  
  24.     }  
  25.   
  26. private:  
  27.     signal_t alarm;  
  28. };  
  29.   
  30.   
  31. #endif // !__RING_H__  



Nurse.h

[cpp] view plain copy
 print?
  1. #ifndef __NURSE_H__  
  2. #define __NURSE_H__  
  3.   
  4.   
  5. #include "boost/random.hpp"  
  6.   
  7.   
  8. extern char const nurse1[] = "Mary";  
  9. extern char const nurse2[] = "Kate";  
  10.   
  11. typedef boost::variate_generator<boost::rand48, boost::uniform_smallint<> > bool_rand;  
  12. bool_rand g_rand(boost::rand48(time(0)), boost::uniform_smallint<>(0, 100));  
  13.   
  14. template<char const* name>  
  15. class Nurse  
  16. {  
  17. public:  
  18.     Nurse() : rand_(g_rand) { }  
  19.   
  20.     void Action()  
  21.     {  
  22.         cout << name;  
  23.         if (rand_() > 30)  
  24.         {  
  25.             cout << " wake up and open door." << endl;  
  26.         }  
  27.         else  
  28.         {  
  29.             cout << " is sleeping..." << endl;  
  30.         }  
  31.     }  
  32.   
  33. private:  
  34.     bool_rand& rand_;  
  35. };  
  36.   
  37.   
  38. #endif // !__NURSE_H__  



Baby.h

[cpp] view plain copy
 print?
  1. #ifndef __BABY_H__  
  2. #define __BABY_H__  
  3.   
  4.   
  5. extern char const baby1[] = "Tom";  
  6. extern char const baby2[] = "Jerry";  
  7.   
  8. template<char const* name>  
  9. class Baby  
  10. {  
  11. public:  
  12.     Baby() : rand(g_rand) { }  
  13.     void Action()  
  14.     {  
  15.         cout << "Baby " << name;  
  16.         if (rand() > 50)  
  17.         {  
  18.             cout << " wake up and crying loudly..." << endl;  
  19.         }  
  20.         else  
  21.         {  
  22.             cout << " is sleeping sweetly..." << endl;  
  23.         }  
  24.     }  
  25.   
  26. private:  
  27.     bool_rand& rand;  
  28. };  
  29.   
  30.   
  31. #endif // !__BABY_H__  


Guest.h

[cpp] view plain copy
 print?
  1. #ifndef __GUEST_H__  
  2. #define __GUEST_H__  
  3.   
  4. #include "Ring.h"  
  5.   
  6. class Guest  
  7. {  
  8. public:  
  9.     void Press(Ring& r)  
  10.     {  
  11.         cout << "A guest press the ring." << endl;  
  12.         r.Press();  
  13.     }  
  14. };  
  15.   
  16.   
  17. #endif // !__GUEST_H__  


main

[cpp] view plain copy
 print?
  1. #include "stdafx.h"  
  2. #include "boost/utility/result_of.hpp"  
  3. #include "boost/typeof/typeof.hpp"  
  4. #include "boost/assign.hpp"  
  5. #include "boost/ref.hpp"  
  6. #include "boost/bind.hpp"  
  7. #include "boost/function.hpp"  
  8. #include "boost/signals2.hpp"  
  9. #include "numeric"  
  10. #include "iostream"  
  11. using namespace std;  
  12. #include "Ring.h"  
  13. #include "nurse.h"  
  14. #include "Baby.h"  
  15. #include "Guest.h"  
  16.   
  17.   
  18. int _tmain(int argc, _TCHAR* argv[])  
  19. {  
  20.     // 声明门铃、护士、婴儿、客人等类的实例  
  21.     Ring r;  
  22.     Nurse<nurse1> n1;  
  23.     Nurse<nurse2> n2;  
  24.     Baby<baby1> b1;  
  25.     Baby<baby2> b2;  
  26.     Guest g;  
  27.   
  28.     // 把护士、婴儿、门铃连接起来  
  29.     r.connect(boost::bind(&Nurse<nurse1>::Action, n1));    //利用boost的bind函数进行回掉
  30.     r.connect(boost::bind(&Nurse<nurse2>::Action, n2));  
  31.     r.connect(boost::bind(&Baby<baby1>::Action, b1));  
  32.     r.connect(boost::bind(&Baby<baby2>::Action, b2));  
  33.   
  34.     // 客人按动门铃,触发一系列的事件  
  35.     g.Press(r);  
  36.   
  37.     return 0;  
  38. }  

原创粉丝点击