Click here to Skip to main content
13,152,321 members (39,802 online)
Click here to Skip to main content

Stats

140.8K views
1.8K downloads
68 bookmarked
Posted 5 Apr 2004

Generic Observer Pattern and Events in C++

, 5 Apr 2004
Efficient and generic way to implement Events in C++. Easy and intuitive way to connect an event to any member function of any class with no limitations.
#ifndef CPP_EVENT_HEADER
#define CPP_EVENT_HEADER

#include <map>

typedef  int CppEventHandler;

template <typename ReturnT,typename ParamT>
class EventHandlerBase1
{
public:
	virtual ReturnT notify(ParamT param) = 0;
};

template <typename ListenerT,typename ReturnT,typename ParamT>
class EventHandler1 : public EventHandlerBase1<ReturnT,ParamT>
{
	typedef ReturnT (ListenerT::*PtrMember)(ParamT);
	ListenerT* m_object;
	PtrMember m_member;
	
public:

	EventHandler1(ListenerT* object, PtrMember member)
		: m_object(object), m_member(member)
	{}

	ReturnT notify(ParamT param)
	{
		return (m_object->*m_member)(param);		
	}	
};

template <typename ReturnT,typename ParamT>
class CppEvent1
{
	typedef std::map<int,EventHandlerBase1<ReturnT,ParamT> *> HandlersMap;
	HandlersMap m_handlers;
	int m_count;

public:


	CppEvent1()
		: m_count(0) {}

	template <typename ListenerT>
	CppEventHandler attach(ListenerT* object,ReturnT (ListenerT::*member)(ParamT))
	{
		typedef ReturnT (ListenerT::*PtrMember)(ParamT);	
		m_handlers[m_count] = (new EventHandler1<ListenerT,ReturnT,ParamT>(object,member));
		m_count++;	
		return m_count-1;
	}

	bool detach(CppEventHandler id)
	{
		HandlersMap::iterator it = m_handlers.find(id);

		if(it == m_handlers.end())
			return false;
		
		delete it->second;
		m_handlers.erase(it);				
		return true;
	}

	ReturnT notify(ParamT param)
	{
		HandlersMap::iterator it = m_handlers.begin();
		for(; it != m_handlers.end(); it++)
		{
			it->second->notify(param);
		}

		return true;
	}
};

// do the same for delegates with 2 parameters
template <typename ReturnT,typename ParamT,typename ParamT1>
class EventHandlerBase2
{
public:
	virtual ReturnT notify(ParamT param,ParamT1 param1) = 0;
};

template <typename ListenerT,typename ReturnT,typename ParamT,typename ParamT1>
class EventHandler2 : public EventHandlerBase2<ReturnT, ParamT, ParamT1>
{
	typedef ReturnT (ListenerT::*PtrMember)(ParamT,ParamT1);
	ListenerT* m_object;
	PtrMember m_member;
	
public:

	EventHandler2(ListenerT* object, PtrMember member)
		: m_object(object), m_member(member)
	{}

	ReturnT notify(ParamT param,ParamT1 param1)
	{
		return (m_object->*m_member)(param,param1);		
	}	
};

template <typename ReturnT,typename ParamT,typename ParamT1>
class CppEvent2
{
	typedef std::map<int,EventHandlerBase2<ReturnT,ParamT,ParamT1> *> HandlersMap;
	HandlersMap m_handlers;
	int m_count;

public:

	
	CppEvent2()
		: m_count(0) {}

	template <typename ListenerT>
	CppEventHandler attach(ListenerT* object,ReturnT (ListenerT::*member)(ParamT,ParamT1))
	{
		typedef ReturnT (ListenerT::*PtrMember)(ParamT,ParamT1);	
		m_handlers[m_count] = (new EventHandler2<ListenerT,ReturnT,ParamT,ParamT1>(object,member));
		m_count++;	
		return m_count-1;
	}

	bool detach(CppEventHandler id)
	{
		HandlersMap::iterator it = m_handlers.find(id);

		if(it == m_handlers.end())
			return false;
		
		delete it->second;
		m_handlers.erase(it);
		return true;
	}

	ReturnT notify(ParamT param,ParamT1 param1)
	{
		HandlersMap::iterator it = m_handlers.begin();
		for(; it != m_handlers.end(); it++)
		{
			it->second->notify(param,param1);
		}

		return true;
	}
};

// do the same for delegates with 3 parameters
template <typename ReturnT,typename ParamT,typename ParamT1,typename ParamT2>
class EventHandlerBase3
{
public:
	virtual ReturnT notify(ParamT param,ParamT1 param1,ParamT2 param2) = 0;
};

template <typename ListenerT,typename ReturnT,typename ParamT,typename ParamT1,typename ParamT2>
class EventHandler3 : public EventHandlerBase3<ReturnT,ParamT,ParamT1,ParamT2>
{
	typedef ReturnT (ListenerT::*PtrMember)(ParamT,ParamT1,ParamT2);
	ListenerT* m_object;
	PtrMember m_member;
	
public:

	EventHandler3(ListenerT* object, PtrMember member)
		: m_object(object), m_member(member)
	{}

	ReturnT notify(ParamT param,ParamT1 param1,ParamT2 param2)
	{
		return (m_object->*m_member)(param,param1,param2);		
	}	
};

template <typename ReturnT,typename ParamT,typename ParamT1,typename ParamT2>
class CppEvent3
{
	typedef std::map<int,EventHandlerBase3<ReturnT,ParamT,ParamT1,ParamT2> *> HandlersMap;
	HandlersMap m_handlers;
	int m_count;

public:

	
	CppEvent3()
		: m_count(0) {}

	template <typename ListenerT>
	CppEventHandler attach(ListenerT* object,ReturnT (ListenerT::*member)(ParamT,ParamT1,ParamT2))
	{
		typedef ReturnT (ListenerT::*PtrMember)(ParamT,ParamT1,ParamT2);	
		m_handlers[m_count] = (new EventHandler3<ListenerT,ReturnT,ParamT,ParamT1,ParamT2>(object,member));
		m_count++;	
		return m_count-1;
	}

	bool detach(CppEventHandler id)
	{
		HandlersMap::iterator it = m_handlers.find(id);

		if(it == m_handlers.end())
			return false;
		
		delete it->second;
		m_handlers.erase(it);
		return true;
	}

	ReturnT notify(ParamT param,ParamT1 param1,ParamT2 param2)
	{
		HandlersMap::iterator it = m_handlers.begin();
		for(; it != m_handlers.end(); it++)
		{
			it->second->notify(param,param1,param2);
		}

		return true;
	}
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Eduard Baranovsky
Web Developer
Canada Canada
Baranovsky Eduard has been a software developer for more then 10 years. He has an experence in image processing, computer graphics and distributed systems design.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 6 Apr 2004
Article Copyright 2004 by Eduard Baranovsky
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid