FUtils/FUEvent.h

00001 /*
00002     Copyright (C) 2005-2006 Feeling Software Inc.
00003     MIT License: http://www.opensource.org/licenses/mit-license.php
00004 */
00005 /*
00006     This file was taken off the Protect project on 26-09-2005
00007 */
00008 
00009 
00010 #ifndef _EVENT_H_
00011 #define _EVENT_H_
00012 
00013 #include "FUtils/FUFunctor.h"
00014 
00015 // Zero argument Event
00016 class FUEvent0
00017 {
00018 private:
00019     typedef IFunctor0<void> Handler;
00020     typedef vector<Handler*> HandlerList;
00021     HandlerList handlers;
00022 
00023 public:
00024     FUEvent0() {}
00025     ~FUEvent0()
00026     {
00027         FUAssert(handlers.empty(), CLEAR_POINTER_VECTOR(handlers));
00028     }
00029 
00030     size_t GetHandlerCount() { return handlers.size(); }
00031 
00032     void InsertHandler(Handler* functor)
00033     {
00034         handlers.push_back(functor);
00035     }
00036 
00037     template<class Class>
00038     void RemoveHandler(Class* handle, void (Class::*_function)(void))
00039     {
00040         void* function = *(void**)&_function;
00041         HandlerList::iterator it;
00042         for (it = handlers.begin(); it != handlers.end(); ++it)
00043         {
00044             if ((*it)->Compare(handle, function))
00045             {
00046                 delete (*it);
00047                 handlers.erase(it);
00048                 break;
00049             }
00050         }
00051     }
00052 
00053     void operator()()
00054     {
00055         HandlerList::iterator it;
00056         for (it = handlers.begin(); it != handlers.end(); ++it)
00057         {
00058             (*(*it))();
00059         }
00060     }
00061 };
00062 
00063 // One argument Event
00064 template <typename Arg1>
00065 class FUEvent1
00066 {
00067 private:
00068     typedef IFunctor1<Arg1, void> Handler;
00069     typedef vector<Handler> HandlerList;
00070     HandlerList handlers;
00071 
00072 public:
00073     FUEvent1() {}
00074 
00075     ~FUEvent1()
00076     {
00077         FUAssert(handlers.empty(), CLEAR_POINTER_VECTOR(handlers));
00078     }
00079 
00080     size_t GetHandlerCount() { return handlers.size(); }
00081 
00082     void InsertHandler(Handler* functor)
00083     {
00084         handlers.push_back(functor);
00085     }
00086 
00087     template <class Class, class _A1>
00088     void RemoveHandler(Class* handle, void (Class::*_function)(_A1))
00089     {
00090         typename HandlerList::iterator it;
00091         void* function = *(void**)&_function;
00092         for (it = handlers.begin(); it != handlers.end(); ++it)
00093         {
00094             if ((*it)->Compare(handle, function))
00095             {
00096                 delete (*it);
00097                 handlers.erase(it);
00098                 break;
00099             }
00100         }
00101     }
00102 
00103     void operator()(Arg1 sArgument1)
00104     {
00105         typename HandlerList::iterator it;
00106         for (it = handlers.begin(); it != handlers.end(); ++it)
00107         {
00108             (*(*it))(sArgument1);
00109         }
00110     }
00111 };
00112 
00113 // Two arguments Event
00114 template <typename Arg1, typename Arg2>
00115 class FUEvent2
00116 {
00117 private:
00118     typedef IFunctor2<Arg1, Arg2, void> Handler;
00119     typedef vector<Handler*> HandlerList;
00120     HandlerList handlers;
00121 
00122 public:
00123     FUEvent2() {}
00124 
00125     ~FUEvent2()
00126     {
00127         FUAssert(handlers.empty(), CLEAR_POINTER_VECTOR(handlers));
00128     }
00129 
00130     size_t GetHandlerCount() { return handlers.size(); }
00131 
00132     void InsertHandler(Handler* functor)
00133     {
00134         handlers.push_back(functor);
00135     }
00136 
00137     template <class Class, class _A1, class _A2>
00138     void RemoveHandler(Class* handle, void (Class::*_function)(_A1, _A2))
00139     {
00140         void* function = *(void**)&_function;
00141         typename HandlerList::iterator it;
00142         for (it = handlers.begin(); it != handlers.end(); ++it)
00143         {
00144             if ((*it)->Compare(handle, function))
00145             {
00146                 delete (*it);
00147                 handlers.erase(it);
00148                 break;
00149             }
00150         }
00151     }
00152 
00153     void operator()(Arg1 sArgument1, Arg2 sArgument2)
00154     {
00155         typename HandlerList::iterator it;
00156         for (it = handlers.begin(); it != handlers.end(); ++it)
00157         {
00158             (*(*it))(sArgument1, sArgument2);
00159         }
00160     }
00161 };
00162 
00163 #endif // _EVENT_H_

Generated on Fri May 12 16:44:39 2006 for FCollada by  doxygen 1.4.6-NO