POCO C++库学习和分析 -- 通知和事件 (四)
下面另一张图是Poco中Event流动的过程:
从图上看实现事件的类被分成了几类:
1) Delegate:AbstractDelegate,Delegate,Expire,FunctionDelegate,AbstractPriorityDelegate,PriorityDelegate,FunctionPriorityDelegate:
2) Strategy:
NotificationStrategy,PriorityStrategy,DefaultStrategy,FIFOStrategy
3) Event:
AbstractEvent,PriorityEvent,FIFOEvent,BasicEvent
我们取Delegate,DefaultStrategy,BasicEvent来分析,其他的只是在它们的基础上加了一些修饰,流程类似。
Delegate类定义如下:#include "Poco/BasicEvent.h"#include "Poco/Delegate.h"#include "Poco/ActiveResult.h"#include <iostream>using Poco::BasicEvent;using Poco::Delegate;using Poco::ActiveResult;class TargetAsync{public:void onAsyncEvent(const void* pSender, int& arg){std::cout << "onAsyncEvent: " << arg << " Current Thread Id is :" << GetCurrentThreadId() << " "<< std::endl;return;}};template<typename RT> class Source{public:BasicEvent<int> theEvent;ActiveResult<RT> AsyncFireEvent(int n){return ActiveResult<RT> (theEvent.notifyAsync(this, n));}};int main(int argc, char** argv){Source<int> source;TargetAsync target;std::cout << "Main Thread Id is :" << GetCurrentThreadId() << " " << std::endl;source.theEvent += Poco::delegate(&target, &TargetAsync::onAsyncEvent);ActiveResult<int> Targs = source.AsyncFireEvent(43);Targs.wait();std::cout << "onEventAsync: " << Targs.data() << std::endl;source.theEvent -= Poco::delegate(&target, &TargetAsync::onAsyncEvent);return 0;}例子里可以看出,同同步事件不同的是,触发事件时,我们调用的是notifyAsync接口。在这个接口里,NotifyAsyncParams对象被创建,并被交由一个主动对象_executeAsync执行。关于主动对象ActiveMethod的介绍,可以从前面的文章POCO C++库学习和分析 -- 线程 (四)中找到。
(版权所有,转载时请注明作者和出处 http://blog.csdn.net/arau_sh/article/details/8673557)