libjingle中的重要概念

sigslot。。有较多的中文文章介绍这个东西。大家可以google下。很惊讶,sigslot只有一个.h文件。。

这两个文章可以看下

http://www.diybl.com/course/3_program/c++/cppxl/2007124/89795.htmlGTalk源码剖析之:sigslot介绍
sigslot库学习http://www.cppblog.com/jake1036/archive/2011/08/05/152529.html

下面是个人认识,误导大家莫见怪,说错的地方,希望大家指出。

认识c++10年了,不过还是新手。几个地方说明下。个人觉得sigslot首先是一种设计模式。

第2个例子的源码更加清晰。加了点说明上去

#include "sigslot.h"
#include
<iostream>
#include
<string>
class Sender{
public:
  sigslot::signal2
<std::string ,int> SignalDanger;//sigslot::signal2<>这个是一个工厂模式?这里定义了一个类实例
   
void Panic()
 
{
    SignalDanger(
"Help!" ,0);
  }

}
;
class Receiver :public sigslot::has_slots<>{//sigslot::has_slots<>同上面。。也是工厂模式,具体c++机制还需要再看下。
public :
 
void OnDanger(std::string mes,int no)
 

    std::cout
<<mes<<no;
  }

}
;
int main()
{
  Sender send ;
  Receiver recv ;
  send.SignalDanger.connect(
&recv ,&Receiver::OnDanger) ;//细节先不管,意思就是send的singalDanger方法和recv的onDanger事件联系在一起 了
  send.Panic() ;//调用panic引发了调用signalDanger。于是就触发recv的onDanger事件。或者说上一语句把signalDanger指针指向了onDanger,于是。。。
  getchar() ;
}

这个机制和window的消息机制相比如何?个人感觉消息机制更好。

最后,libjingle中对sigslot的解释很好。http://code.google.com/intl/zh-CN/apis/talk/libjingle/important_concepts.html

和在一起看,基本就把sigslot搞明白了,防止被墙,复制一段在下面。

Signals

libjingle uses the sigslot library to            facilitate communication between objects. sigslot is a generic framework            that enables you to connect a calling member to a receiving function            in any class (including the same class) very simply. The way it works            is this:

  1. The sending class declares a member variable, called a signal,                using a special template-like syntax. This signal defines the parameters                of the listening function.
  2. The listening class implements a function with the same number, type,                and sequence of parameters as the signal. This is sometimes called                the receiver or theslot. (Note: this can even be the same                class as the one that declared the signal.) This function cannot                return a value (e.g., returns void). The receiver must inheritsigslot::has_slots<>.
  3. The listener connects to the signal by calling the signal's connect method,                passing in a pointer to the instance of the listening object, and                the address of the implementing class function.
  4. The sender calls its signal member as if it were a function, passing                in the appropriate parameter types as declared. It can pass parameters                by either value or reference.

You can connect as many signals as you like to a common slot. libjingle            sometimes assigns multiple signals to a single slot in order to consolidate            its message handling. Conversely, several objects declare a signal object            in order to broadcast commonly needed messages from a single point (for            example, alerts sent when a connection state changes).sigslot takes            care of disconnecting callbacks and dereferencing when objects are destroyed.

定义一个signal变量,定义一个slot方法,connect在一起。一个slot可以和多个signal连接。另外也有disconnect方法。

 

下面来看thread

 

你可能感兴趣的:(设计模式,function,object,Class,Parameters,Signal)