1,Notification
// 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;
}