牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

sigslot简介

转载自:http://www.thecodeway.com/blog/?p=85


    在开发一个复杂工程的时候,经常会遇到这样一个问题:整个系统被分成数个模块,每个模块提供有限的功能,由上层调用组成整个系统,为了保证每个模块的独立性,我们经常会尽量限制模块与模块之间的直接联系,比如每个模块只提供有限的API或者COM接口,而内部实现则完全封闭起来。
    但有的时候会出一些设计要求,必须能够使模块之间能够直接通讯,而这两个模块往往处于不同的逻辑层次,之间相差甚远,如何设计它们之间的调用模式使整个工程维持整洁变得非常困难,比如模块直接直接包含对方的头文件会引起编译变得复杂,提供api或者接口会引起版本危机等问题。
    sigslot的出现为我们提供了一种解决问题的思想,它用“信号”的概念实现不同模块之间的传输问题,sigslot本身类似于一条通讯电缆,两端提供发送器和接收器,只要把两个模块用这条电缆连接起来就可以实现接口调用,而sigslot本身只是一个轻量级的作品,整个库只有一个.h文件,所以无论处于何种层次的库,都可以非常方便的包含它。

    举个例子,我们设计一个发送消息的类,这个类负责在某种时刻向外界发出求救信号

// Class that sends the notification.
class Sender  
{
public:
    
// The signal declaration. 
    
// The ‘2′ in the name indicates the number of parameters. Parameter types 
    
// are declared in the template parameter list.
    sigslot::signal2< std::string , int > SignalDanger;
    
    
// When anyone calls Panic(), we will send the SignalDanger signal.
    void Panic()
    
{
        SignalDanger(
"Help!"0); 
    }

}
;

另外一个类则负责接收求助信号
 
// Listening class. It must inherit sigslot.
class Receiver : public sigslot::has_slots<>
{
public:
    
// When anyone calls Panic(), Receiver::OnDanger gets the message.
    
// Notice that the number and type of parameters match
    
// those in Sender::SignalDanger, and that it doesn’t return a value.
    void OnDanger(std::string message, int time)
    
{
        printf(
"I heard something like \"%s\" at %d!\n", message.c_str(), time);
    }

}
;

现在让我们在主逻辑中把这两个类连接起来

Sender sender;
Receiver receiver;
 
// Receiver registers to get SignalDanger signals.
// When SignalDanger is sent, it is caught by OnDanger().
// Second parameter gives address of the listener function class definition.
// First parameter points to instance of this class to receive notifications.
sender.SignalDanger.connect(&receiver, Receiver::OnDanger);

只要在任何时候调用 sender.Panic()函数,就会把求救信号发送给接收者,而且这两个发送和接收端的模块都可以独立编译,不会出现版本问题。

posted on 2010-02-24 20:21 杨粼波 阅读(1203) 评论(1)  编辑 收藏 引用

评论

# re: sigslot简介 2010-02-25 17:24 ccsdu2009

呵呵 你也关注过这个库啊
其实不考虑线程安全的话
gui库glooey总改造了的sigslot是很简单的  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理