BWAPI
|
00001 #pragma once 00002 #include "../../Utils/Utils.h" 00003 #include <utility> 00004 #include <boost/tuple/tuple.hpp> 00005 00006 class Component; 00007 00012 struct EventHandlerBase 00013 { 00019 EventHandlerBase(const Component& owner) 00020 #ifdef _DEBUG_COMPONENT 00021 : m_owner(owner) 00022 #endif 00023 { 00024 unused(owner); 00025 } 00026 00027 virtual ~EventHandlerBase() {} 00028 00029 #ifdef _DEBUG_COMPONENT 00030 00033 const Component& m_owner; 00034 #endif 00035 }; 00036 00041 template <class Arguments> 00042 struct EventHandlerT : public EventHandlerBase 00043 { 00048 EventHandlerT(const Component& owner) 00049 : EventHandlerBase(owner) 00050 {} 00051 00052 typedef Arguments arguments; 00053 00059 virtual void onEvent(void* data, arguments& args) = 0; 00060 }; 00061 00065 struct EventHandler : EventHandlerT<boost::tuple<>> 00066 { 00067 EventHandler(const Component& owner) 00068 : EventHandlerT(owner) 00069 {} 00070 }; 00071 00076 template<class T1> 00077 struct EventHandler1 : EventHandlerT<boost::tuple<T1>> 00078 { 00079 EventHandler1(const Component& owner) 00080 : EventHandlerT(owner) 00081 {} 00082 }; 00083 00089 template<class T1, class T2> 00090 struct EventHandler2 : EventHandlerT<boost::tuple<T1,T2>> 00091 { 00092 EventHandler2(const Component& owner) 00093 : EventHandlerT(owner) 00094 {} 00095 }; 00096 00103 template<class T1, class T2, class T3> 00104 struct EventHandler3 : EventHandlerT<boost::tuple<T1,T2,T3>> 00105 { 00106 EventHandler3(const Component& owner) 00107 : EventHandlerT(owner) 00108 {} 00109 }; 00110 00118 template<class T1, class T2, class T3, class T4> 00119 struct EventHandler4 : EventHandlerT<boost::tuple<T1,T2,T3,T4>> 00120 { 00121 EventHandler4(const Component& owner) 00122 : EventHandlerT(owner) 00123 {} 00124 }; 00125 00134 template<class T1, class T2, class T3, class T4, class T5> 00135 struct EventHandler5 : EventHandlerT<boost::tuple<T1,T2,T3,T4,T5>> 00136 { 00137 EventHandler5(const Component& owner) 00138 : EventHandlerT(owner) 00139 {} 00140 }; 00141 00142 //------------------------------------------------------------------------------ 00143 00144 #define EVENT_HANDLER_NAME(Type, Function) \ 00145 EventHandler_##Type##_##Function 00146 00147 #define EVENT_HANDLER(Type, Function) \ 00148 struct EVENT_HANDLER_NAME(Type, Function) : EventHandler \ 00149 { \ 00150 EVENT_HANDLER_NAME(Type, Function)(Type& object) \ 00151 : EventHandler(object) \ 00152 , m_object(object) {} \ 00153 virtual ~EVENT_HANDLER_NAME(Type, Function)() {} \ 00154 virtual void onEvent(void* data, arguments&) \ 00155 { \ 00156 m_object.Function(data); \ 00157 } \ 00158 private: \ 00159 Type& m_object; \ 00160 } 00161 00162 #define EVENT_HANDLER1(Type, Function, T1) \ 00163 struct EVENT_HANDLER_NAME(Type, Function) : EventHandler1<T1> \ 00164 { \ 00165 EVENT_HANDLER_NAME(Type, Function)(Type& object) \ 00166 : EventHandler1(object) \ 00167 , m_object(object) {} \ 00168 virtual ~EVENT_HANDLER_NAME(Type, Function)() {} \ 00169 virtual void onEvent(void* data, arguments& args) \ 00170 { \ 00171 m_object.Function(data, args.get<0>()); \ 00172 } \ 00173 private: \ 00174 Type& m_object; \ 00175 } 00176 00177 #define EVENT_HANDLER2(Type, Function, T1, T2) \ 00178 struct EVENT_HANDLER_NAME(Type, Function) : EventHandler2<T1,T2> \ 00179 { \ 00180 EVENT_HANDLER_NAME(Type, Function)(Type& object) \ 00181 : EventHandler2(object) \ 00182 , m_object(object) {} \ 00183 virtual ~EVENT_HANDLER_NAME(Type, Function)() {} \ 00184 virtual void onEvent(void* data, arguments& args) \ 00185 { \ 00186 m_object.Function(data, args.get<0>(), args.get<1>()); \ 00187 } \ 00188 private: \ 00189 Type& m_object; \ 00190 } 00191 00192 #define EVENT_HANDLER3(Type, Function, T1, T2, T3) \ 00193 struct EVENT_HANDLER_NAME(Type, Function) : EventHandler3<T1,T2,T3> \ 00194 { \ 00195 EVENT_HANDLER_NAME(Type, Function)(Type& object) \ 00196 : EventHandler3(object) \ 00197 , m_object(object) {} \ 00198 virtual ~EVENT_HANDLER_NAME(Type, Function)() {} \ 00199 virtual void onEvent(void* data, arguments& args) \ 00200 { \ 00201 m_object.Function(data, args.get<0>(), args.get<1>(), args.get<2>()); \ 00202 } \ 00203 private: \ 00204 Type& m_object; \ 00205 } 00206 00207 #define EVENT_HANDLER4(Type, Function, T1, T2, T3, T4) \ 00208 struct EVENT_HANDLER_NAME(Type, Function) : EventHandler4<T1,T2,T3,T4> \ 00209 { \ 00210 EVENT_HANDLER_NAME(Type, Function)(Type& object) \ 00211 : EventHandler4(object) \ 00212 , m_object(object) {} \ 00213 virtual ~EVENT_HANDLER_NAME(Type, Function)() {} \ 00214 virtual void onEvent(void* data, arguments& args) \ 00215 { \ 00216 m_object.Function(data, args.get<0>(), args.get<1>(), args.get<2>(), args.get<3>()); \ 00217 } \ 00218 private: \ 00219 Type& m_object; \ 00220 } 00221 00222 #define EVENT_HANDLER5(Type, Function, T1, T2, T3, T4, T5) \ 00223 struct EVENT_HANDLER_NAME(Type, Function) : EventHandler5<T1,T2,T3,T4,T5> \ 00224 { \ 00225 EVENT_HANDLER_NAME(Type, Function)(Type& object) \ 00226 : EventHandler5(object) \ 00227 , m_object(object) {} \ 00228 virtual ~EVENT_HANDLER_NAME(Type, Function)() {} \ 00229 virtual void onEvent(void* data, arguments& args) \ 00230 { \ 00231 m_object.Function(data, args.get<0>(), args.get<1>(), args.get<2>(), args.get<3>(), args.get<4>()); \ 00232 } \ 00233 private: \ 00234 Type& m_object; \ 00235 }