Revision 1 (by (no author), 2006/09/28 22:53:53) Initial import
/// \file event.h
/// Event handler class

#ifndef EVENT_H_INCLUDED
#define EVENT_H_INCLUDED

#include <list>

template <class Type = void> class DelegateBase
{
	public: // Public functions
		virtual void Call(Type* args) = 0;
};

template <class Target, class Type = void> class Delegate: public DelegateBase<Type>
{
	public: // Public variables
		Target* target;
		void (Target::*method)(Type*);
		
	public: // Public functions
		Delegate(Target* target, void (Target::*method)(Type*)): target(target), method(method)
		{ }
		
		void Call(Type* args)
		{ (target->*method)(args); }
};

template<class Type = void> class Event
{
	public: // Public variables
		std::list<DelegateBase<Type>*> delegates;
		
	public: // Public functions
		~Event()
		{
			for(typename std::list<DelegateBase<Type>*>::iterator i = delegates.begin(); i != delegates.end(); i++)
				delete *i;
		}
		void operator +=(DelegateBase<Type>* add)
		{
			delegates.push_back(add);
		}
		void operator -=(DelegateBase<Type>* del)
		{
			delegates.remove(del);
		}
		void Trigger(Type* args)
		{
			for(typename std::list<DelegateBase<Type>*>::iterator i = delegates.begin(); i != delegates.end(); i++)
				(*i)->Call(args);
		}
};

#endif

// The end