使用sigc++插槽系统

http://www.cppblog.com/gaimor/archive/2010/02/22/108236.html?opt=admin

 

我所知的c++插槽系统由3个boost的,sigslot的,sigc++的
这里介绍sigc++的使用

最基本的使用方法:

1.回调函数为一般函数:
代码如下:

 1  #include  < iostream >
 2  #include  < string >
 3  #include  < sigc ++/ sigc ++ .h >
 4 
 5  // ! 普通函数 
 6  void  Print( const  std:: string &  str)
 7  {
 8     std::cout  <<  str;
 9  }
10 
11  int  main()
12  {
13      // ! 返回值void,参数const std::string&  
14     sigc::signal < void const  std:: string &>  signal_print;
15      // ! 链接函数 
16     signal_print.connect( sigc::ptr_fun( & Print));
17      // ! 发射信号 
18     signal_print.emit( " hello world\n " );
19    
20     system( " pause " );
21      return   0 ;
22  }

2.回调函数为成员函数

 1  #include  < iostream >
 2  #include  < string >
 3  #include  < sigc ++/ sigc ++ .h >
 4   
 5  class  Printer : public  sigc::trackable
 6  {
 7  public :
 8      void  Work(){slot.emit( " work\n " );}    
 9     typedef sigc::signal < void const  std:: string &>  Slot;
10     Slot slot;             
11      void  Print( const  std:: string &  str){std::cout << str;}   
12  }; 
13   
14  int  main()
15  {
16     Printer printer; 
17     Printer::Slot::iterator iter  =  printer.slot.connect(sigc::mem_fun( & printer, & Printer::Print));
18     printer.Work();
19     iter -> disconnect();
20     printer.Work();
21     
22     system( " pause " );
23      return   0 ;
24  }

在sigc++中sigc::ptr_fun负责绑定一般函数
而sigc::men_fun负责绑定成员函数.

可以看到一般的信号插槽系统都具备以下几个函数
a.插槽连接
b.插槽断开
c.信号发射
当然有的插槽信号库还提供其它一些函数
比如对信号设定优先级等等

这是简单实用sigc++的例子
不过若论简单性的话还是sigslot比较好,只有一个头文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值