Poco 的通知和事件

6 篇文章 0 订阅

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yours风之恋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值