Poco 的通知和事件
2015-12-04 14:32
281 查看
1,Notification
// NotificationQueue
// POCOEvent.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "Poco/NotificationCenter.h" #include "Poco/Notification.h" #include "Poco/Observer.h" #include "Poco/NObserver.h" #include "Poco/AutoPtr.h" #include <iostream> using Poco::NotificationCenter; using Poco::Notification; using Poco::Observer; using Poco::NObserver; using Poco::AutoPtr; class BaseNotification : public Notification { }; class SubNotification : public BaseNotification { }; class Target { public: void handleBase(BaseNotification* pNf) { std::cout << "handleBase" << pNf->name() << std::endl; pNf->release(); } void handleSub(const AutoPtr<SubNotification>& pnf) { std::cout << "HandleSub" << pnf->name() << std::endl; } }; int main(int argc, _TCHAR* argv[]) { NotificationCenter nc; Target target; nc.addObserver(Observer<Target,BaseNotification>(target,&Target::handleBase)); nc.addObserver(NObserver<Target,SubNotification>(target,&Target::handleSub)); nc.postNotification((new BaseNotification)); nc.postNotification((new SubNotification)); nc.removeObserver(Observer<Target,BaseNotification>(target,&Target::handleBase)); nc.removeObserver(NObserver<Target,SubNotification>(target,&Target::handleSub)); system("pause"); return 0; }
// NotificationQueue
// POCOEvent.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "Poco/Notification.h" #include "Poco/NotificationQueue.h" #include "Poco/ThreadPool.h" #include "Poco/Runnable.h" #include "Poco/AutoPtr.h" using Poco::Notification; using Poco::NotificationQueue; using Poco::ThreadPool; using Poco::Runnable; using Poco::AutoPtr; #include <iostream> class WorkNotification : public Notification { public : WorkNotification(int data): _data(data){} int data() const { return _data; } private: int _data; }; class Worker : public Runnable { public: Worker(NotificationQueue& queue): _queue(queue) {} void run() { AutoPtr<Notification> pNf(_queue.waitDequeueNotification()); while(pNf) { WorkNotification* pWorkNf = dynamic_cast<WorkNotification*>(pNf.get()); if(pWorkNf) { std::cout << "OK" << std::endl; } pNf = _queue.waitDequeueNotification(); } } private: NotificationQueue& _queue; }; int main(int argc, _TCHAR* argv[]) { NotificationQueue queue; Worker worker1(queue); Worker worker2(queue); ThreadPool::defaultPool().start(worker1); ThreadPool::defaultPool().start(worker2); for(int i=0; i<100; ++i) { queue.enqueueNotification(new WorkNotification(i)); } while(!queue.empty()) { Poco::Thread::sleep(100); } queue.wakeUpAll(); ThreadPool::defaultPool().joinAll(); system("pause"); return 0; }3,Event
// POCOEvent.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "Poco/BasicEvent.h" #include "Poco/Delegate.h" using Poco::BasicEvent; using Poco::Delegate; #include <iostream> class Source { public: BasicEvent<int> theEvent; void fireEvent(int n) { //theEvent(this,n); theEvent.notify(this,n); } }; class Target { public: void onEvent(const void* pSender, int& args) { std::cout << "onEvent:" << args << std::endl; } }; int main(int argc, _TCHAR* argv[]) { Source source; Target target; source.theEvent += Poco::delegate(&target,&Target::onEvent); source.fireEvent(42); source.theEvent -= Poco::delegate(&target,&Target::onEvent); system("pause"); return 0; }
相关文章推荐
- 去掉集合中重复元素
- VS中批注的使用
- SharePoint 2013 - Designer Workflow
- 揭秘 | 百万并发直播网站龙珠的性能秘籍
- 指针类型学习总结--迷途指针
- 转:Android中的复制粘贴
- 博君一笑:在ARC中如何强制释放某一个NSObject?
- 10006---jQuery事件
- git merge fast-forward squash no-ff
- textField中只能输入数字
- online_judge_1013
- 【算法】递归(recursion)+经典例题个人分析
- 《图像局部不变性特征与描述》阅读笔记(3)-- 点与边缘检测
- Java 大作业回忆--day02-- PC端的初次尝试
- poj 1679 The Unique MST
- 【剑指offer】之丑数
- 微软算法100题58 从尾到头输出链表(java)
- Freemaker的了解
- How browsers work
- Flume 核心组件介绍