Chinaunix首页 | 论坛 | 博客
  • 博客访问: 854888
  • 博文数量: 254
  • 博客积分: 5350
  • 博客等级: 大校
  • 技术积分: 2045
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 13:27
文章分类

全部博文(254)

文章存档

2015年(1)

2014年(9)

2013年(17)

2012年(30)

2011年(150)

2010年(17)

2009年(28)

2008年(2)

分类: LINUX

2013-07-24 11:39:32


点击(此处)折叠或打开

  1. /*
  2.  * function2.cpp
  3.  *
  4.  * Created on: 2013-7-24
  5.  * Author: root
  6.  */

  7. #include <iostream>
  8. #include "boost/function.hpp"
  9. #include "boost/bind.hpp"
  10. /*
  11. //带状态的类对象
  12. class keeping_state {
  13.     int total_;
  14. public:
  15.     keeping_state():total_(0){}

  16.     int operator()(int i) { //括号重载 void operator()()
  17.         total_+= i;
  18.         return total_;
  19.     }

  20.     int total() const {
  21.         return total_;
  22.     }
  23. };

  24. int main() {
  25.     keeping_state ks;
  26. //    boost::function<int(int)> f1;
  27. ////    f1 = ks; 使用拷贝对象时并不能同时改变total_
  28. //    f1 = boost::ref(ks);//使用引用, 常量引用boost::cref
  29. //
  30. //    boost::function<int(int)> f2;
  31. //    f2 = boost::ref(ks);

  32.     //第二种示例
  33.     boost::function1<int,int> f1;
  34. //    f1 = ks;使用拷贝对象时并不能同时改变total_
  35.     f1 = boost::ref(ks);

  36.     boost::function1<int,int> f2(f1);
  37.     boost::function1<short,short> f3(f2);


  38.     std::cout << "The current f1 total is " << f1(10) << '\n';
  39.     std::cout << "The current f2 total is " << f2(10) << '\n';
  40.     std::cout << "The current f2 total is " << f3(10) << '\n';
  41.     std::cout << "After adding 10 two times,the total is " << ks.total() << '\n';
  42. }
  43. */


  44. //boost.function 与 boost.bind 合用,让调用代码对被调用代码无所知,实现解耦
  45. class tape_recorder {
  46. public:
  47.     void play() {
  48.         std::cout << "Since my baby left me ...\n";
  49.     }

  50.         void stop() {
  51.         std::cout << "OK, taking a break \n";
  52.     }

  53.         void forward() {
  54.         std::cout << "whizzz\n";
  55.     }

  56.         void rewind() {
  57.         std::cout << "zzzihw\n";
  58.     }

  59.         void record(const std::string& sound) {
  60.         std::cout << "Recorder: "<< sound << " \n";
  61.     }
  62. };

  63. class command {
  64.     boost::function<void()> f_;
  65. public:
  66.     command() {}
  67.     command(boost::function<void()> f):f_(f) { }

  68.     void execute() {
  69.         if(f_) {
  70.             f_();
  71.         }
  72.     }

  73.     template<typename Func> void set_function(Func f) {
  74.         f_ = f;
  75.     }

  76.     bool enabled() const {
  77.         return f_;
  78.     }
  79. };

  80. int main() {
  81.     tape_recorder tr;

  82.     command play(boost::bind(&tape_recorder::play,&tr));
  83.     command stop(boost::bind(&tape_recorder::stop,&tr));
  84.     command forward(boost::bind(&tape_recorder::forward,&tr));
  85.     command rewind(boost::bind(&tape_recorder::rewind,&tr));
  86.     command record;

  87.     //boost::function<void()> f(boost::bind(&tape_recorder::play,&tr));
  88.     //f = boost::ref(play);
  89.     if(play.enabled()){
  90.         play.execute();
  91.     }

  92.     //pass some lyrics
  93.     std::string s="What a beautiful morning...";
  94.     record.set_function(
  95.         boost::bind(&tape_recorder::record,&tr,s));
  96.     record.execute();

  97. }

阅读(1013) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~