zoukankan      html  css  js  c++  java
  • 信号和槽有一个非常精炼的C++实现,作者是Sarah Thompson,该实现只有一个头文件sigslot.h,跨平台且线程安全

    关于信号和槽有一个非常精炼的C++实现,作者是Sarah Thompson,该实现只有一个头文件sigslot.h,跨平台且线程安全。

    源码在:http://sigslot.cvs.sourceforge.net/viewvc/sigslot/sigslot/sigslot.h?revision=1.1.1.1&content-type=text%2Fplain

    在WebRTC中,sigslot .h是其基础的事件处理框架, 在多个模块的消息通知,响应处理中被使用。

    sigslot.h的使用方法如下所示:

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. #include "sigslot.h"    
    2. #include <string>    
    3. #include <stdio.h>    
    4. #include <iostream>    
    5. #include <windows.h>    
    6.     
    7. using namespace sigslot;      
    8.     
    9. using namespace std;    
    10.     
    11. class CSender    
    12. {    
    13. public:    
    14.     sigslot::signal2<string, int> m_pfnsigDanger;    
    15.     
    16.     void Panic()    
    17.     {    
    18.         static int nVal = 0;    
    19.         char szVal[20] = { 0 };    
    20.         sprintf_s(szVal,20, "help--%d", nVal);    
    21.         m_pfnsigDanger(szVal, nVal++);    
    22.     }    
    23. };    
    24.     
    25. class CReceiver :public sigslot::has_slots<>    
    26. {    
    27. public:    
    28.     void OnDanger(string strMsg, int nVal)    
    29.     {    
    30.         //printf("%s ==> %d", strMsg.c_str(), nVal);    
    31.         cout << strMsg.c_str() << " ==> " << nVal << endl;    
    32.     }    
    33. };    
    34.         
    35. int main()    
    36. {    
    37.     CSender sender;    
    38.     CReceiver recever;    
    39.     
    40.     cout << "create object ok..." << endl;    
    41.     
    42.     sender.m_pfnsigDanger.connect(&recever, &CReceiver::OnDanger);    
    43.     
    44.     cout << "connect succ!" << endl;    
    45.     
    46.     while (1)    
    47.     {    
    48.         cout << "in while..." << endl;    
    49.     
    50.         sender.Panic();    
    51.         Sleep(2000);    
    52.     
    53.         cout << "end of sleep" << endl;    
    54.     }    
    55.     
    56.     
    57.     return 0;    
    58. }    

    需要注意的是,如果在Qt工程中使用sigslot.h,sigslot.h中的emit函数名会和Qt中的emit宏冲突,修改方法有两个,一是将sigslot.h的emit改成其他名字,二是在.pro文件中添加DEFINES+=QT_NO_EMIT,禁用Qt的emit宏。

    参考链接:http://blog.csdn.net/u014338577/article/details/47127405

    http://blog.csdn.net/caoshangpa/article/details/54088313

  • 相关阅读:
    二维空间轨迹聚类
    Java 格式化输出
    Linux 小笔记
    Nginx ServerName 配置说明(转)
    LVS
    Nginx + tomcat 实现简单集群(基于反向代理方式)
    mysql 外键
    tomcat 日志输出
    tomcat https
    SpringMVC表单标签简介
  • 原文地址:https://www.cnblogs.com/findumars/p/5625061.html
Copyright © 2011-2022 走看看