zoukankan      html  css  js  c++  java
  • 使用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<voidconst std::string&> signal_print;
    15    //! 链接函数 
    16    signal_print.connect( sigc::ptr_fun(&Print));
    17    //! 发射信号 
    18    signal_print.emit("hello world ");
    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 ");}    
     9    typedef sigc::signal<voidconst 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比较好,只有一个头文件

  • 相关阅读:
    Pandas基本命令
    python——内建模块instance的学习
    pyhton——logging日志模块的学习
    mongoDB集群的搭建
    goahead(web服务器)分析
    2019-9
    cmake
    mqtt+htttp+websocket
    u-boot中filesize环境变量【转载】
    cppcheck下载及使用
  • 原文地址:https://www.cnblogs.com/jingzhishen/p/3430784.html
Copyright © 2011-2022 走看看