[964] | 1 | /* |
---|
| 2 | Copyright (C) 2005-2006 Feeling Software Inc. |
---|
| 3 | MIT License: http://www.opensource.org/licenses/mit-license.php |
---|
| 4 | */ |
---|
| 5 | /* |
---|
| 6 | This file was taken off the Protect project on 26-09-2005 |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | #ifndef _EVENT_H_ |
---|
| 11 | #define _EVENT_H_ |
---|
| 12 | |
---|
| 13 | #include "FUtils/FUFunctor.h" |
---|
| 14 | |
---|
| 15 | // Zero argument Event |
---|
| 16 | class FUEvent0 |
---|
| 17 | { |
---|
| 18 | private: |
---|
| 19 | typedef IFunctor0<void> Handler; |
---|
| 20 | typedef vector<Handler*> HandlerList; |
---|
| 21 | HandlerList handlers; |
---|
| 22 | |
---|
| 23 | public: |
---|
| 24 | FUEvent0() {} |
---|
| 25 | ~FUEvent0() |
---|
| 26 | { |
---|
| 27 | FUAssert(handlers.empty(), CLEAR_POINTER_VECTOR(handlers)); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | size_t GetHandlerCount() { return handlers.size(); } |
---|
| 31 | |
---|
| 32 | void InsertHandler(Handler* functor) |
---|
| 33 | { |
---|
| 34 | handlers.push_back(functor); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | template<class Class> |
---|
| 38 | void RemoveHandler(Class* handle, void (Class::*_function)(void)) |
---|
| 39 | { |
---|
| 40 | void* function = *(void**)&_function; |
---|
| 41 | HandlerList::iterator it; |
---|
| 42 | for (it = handlers.begin(); it != handlers.end(); ++it) |
---|
| 43 | { |
---|
| 44 | if ((*it)->Compare(handle, function)) |
---|
| 45 | { |
---|
| 46 | delete (*it); |
---|
| 47 | handlers.erase(it); |
---|
| 48 | break; |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void operator()() |
---|
| 54 | { |
---|
| 55 | HandlerList::iterator it; |
---|
| 56 | for (it = handlers.begin(); it != handlers.end(); ++it) |
---|
| 57 | { |
---|
| 58 | (*(*it))(); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | // One argument Event |
---|
| 64 | template <typename Arg1> |
---|
| 65 | class FUEvent1 |
---|
| 66 | { |
---|
| 67 | private: |
---|
| 68 | typedef IFunctor1<Arg1, void> Handler; |
---|
| 69 | typedef vector<Handler> HandlerList; |
---|
| 70 | HandlerList handlers; |
---|
| 71 | |
---|
| 72 | public: |
---|
| 73 | FUEvent1() {} |
---|
| 74 | |
---|
| 75 | ~FUEvent1() |
---|
| 76 | { |
---|
| 77 | FUAssert(handlers.empty(), CLEAR_POINTER_VECTOR(handlers)); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | size_t GetHandlerCount() { return handlers.size(); } |
---|
| 81 | |
---|
| 82 | void InsertHandler(Handler* functor) |
---|
| 83 | { |
---|
| 84 | handlers.push_back(functor); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | template <class Class, class _A1> |
---|
| 88 | void RemoveHandler(Class* handle, void (Class::*_function)(_A1)) |
---|
| 89 | { |
---|
| 90 | typename HandlerList::iterator it; |
---|
| 91 | void* function = *(void**)&_function; |
---|
| 92 | for (it = handlers.begin(); it != handlers.end(); ++it) |
---|
| 93 | { |
---|
| 94 | if ((*it)->Compare(handle, function)) |
---|
| 95 | { |
---|
| 96 | delete (*it); |
---|
| 97 | handlers.erase(it); |
---|
| 98 | break; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void operator()(Arg1 sArgument1) |
---|
| 104 | { |
---|
| 105 | typename HandlerList::iterator it; |
---|
| 106 | for (it = handlers.begin(); it != handlers.end(); ++it) |
---|
| 107 | { |
---|
| 108 | (*(*it))(sArgument1); |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | }; |
---|
| 112 | |
---|
| 113 | // Two arguments Event |
---|
| 114 | template <typename Arg1, typename Arg2> |
---|
| 115 | class FUEvent2 |
---|
| 116 | { |
---|
| 117 | private: |
---|
| 118 | typedef IFunctor2<Arg1, Arg2, void> Handler; |
---|
| 119 | typedef vector<Handler*> HandlerList; |
---|
| 120 | HandlerList handlers; |
---|
| 121 | |
---|
| 122 | public: |
---|
| 123 | FUEvent2() {} |
---|
| 124 | |
---|
| 125 | ~FUEvent2() |
---|
| 126 | { |
---|
| 127 | FUAssert(handlers.empty(), CLEAR_POINTER_VECTOR(handlers)); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | size_t GetHandlerCount() { return handlers.size(); } |
---|
| 131 | |
---|
| 132 | void InsertHandler(Handler* functor) |
---|
| 133 | { |
---|
| 134 | handlers.push_back(functor); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | template <class Class, class _A1, class _A2> |
---|
| 138 | void RemoveHandler(Class* handle, void (Class::*_function)(_A1, _A2)) |
---|
| 139 | { |
---|
| 140 | void* function = *(void**)&_function; |
---|
| 141 | typename HandlerList::iterator it; |
---|
| 142 | for (it = handlers.begin(); it != handlers.end(); ++it) |
---|
| 143 | { |
---|
| 144 | if ((*it)->Compare(handle, function)) |
---|
| 145 | { |
---|
| 146 | delete (*it); |
---|
| 147 | handlers.erase(it); |
---|
| 148 | break; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | void operator()(Arg1 sArgument1, Arg2 sArgument2) |
---|
| 154 | { |
---|
| 155 | typename HandlerList::iterator it; |
---|
| 156 | for (it = handlers.begin(); it != handlers.end(); ++it) |
---|
| 157 | { |
---|
| 158 | (*(*it))(sArgument1, sArgument2); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | }; |
---|
| 162 | |
---|
| 163 | #endif // _EVENT_H_ |
---|