00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_CSUTIL_EVENTHANDLERS_H__
00021 #define __CS_CSUTIL_EVENTHANDLERS_H__
00022
00023 #include "cssysdef.h"
00024 #include "csextern.h"
00025 #include "iutil/eventnames.h"
00026 #include "iutil/eventhandlers.h"
00027 #include "iutil/eventh.h"
00028 #include "csutil/csstring.h"
00029 #include "csutil/eventnames.h"
00030 #include "csutil/scf_implementation.h"
00031 #include "csutil/threading/rwmutex.h"
00032 #include "csutil/hash.h"
00033 #include "csutil/strset.h"
00034 #include "csutil/ref.h"
00035
00036 struct iObjectRegistry;
00037
00045 struct iEventHandler;
00046
00052 class CS_CRYSTALSPACE_EXPORT csEventHandlerRegistry :
00053 public scfImplementation1<csEventHandlerRegistry, iEventHandlerRegistry>
00054 {
00055 public:
00056 csEventHandlerRegistry(iObjectRegistry*);
00057 ~csEventHandlerRegistry();
00063 csHandlerID GetGenericID (const char*);
00064 static csHandlerID GetGenericID (iObjectRegistry *reg,
00065 const char* name)
00066 {
00067 return GetRegistry (reg)->GetGenericID (name);
00068 }
00069 csHandlerID GetGenericPreBoundID (csHandlerID);
00070 static csHandlerID GetGenericPreBoundID (
00071 iObjectRegistry *reg, csHandlerID id)
00072 {
00073 return GetRegistry (reg)->GetGenericPreBoundID (id);
00074 }
00075 csHandlerID GetGenericPostBoundID (csHandlerID);
00076 static csHandlerID GetGenericPostBoundID (
00077 iObjectRegistry *reg, csHandlerID id)
00078 {
00079 return GetRegistry (reg)->GetGenericPostBoundID (id);
00080 }
00081
00086 csHandlerID GetID (iEventHandler *);
00087 static csHandlerID GetID (iObjectRegistry *reg,
00088 iEventHandler *h)
00089 {
00090 return GetRegistry (reg)->GetID (h);
00091 }
00092
00093 csHandlerID RegisterID (iEventHandler *);
00094 static csHandlerID RegisterID (iObjectRegistry *reg,
00095 iEventHandler *h)
00096 {
00097 return GetRegistry (reg)->RegisterID (h);
00098 }
00099
00106 csHandlerID GetID (const char*);
00107 static csHandlerID GetID (iObjectRegistry *reg,
00108 const char* name)
00109 {
00110 return GetRegistry (reg)->GetID (name);
00111 }
00112
00116 void ReleaseID (csHandlerID id);
00117 static void ReleaseID (iObjectRegistry *reg,
00118 csHandlerID id)
00119 {
00120 GetRegistry (reg)->ReleaseID (id);
00121 }
00125 void ReleaseID (iEventHandler *);
00126 static void ReleaseID (iObjectRegistry *reg,
00127 iEventHandler *h)
00128 {
00129 GetRegistry (reg)->ReleaseID (h);
00130 }
00136 iEventHandler* GetHandler (csHandlerID id);
00137 static inline iEventHandler* GetHandler (
00138 iObjectRegistry *reg, csHandlerID id)
00139 {
00140 return GetRegistry (reg)->GetHandler (id);
00141 };
00142
00148 bool IsInstanceOf (csHandlerID instanceid,
00149 csHandlerID genericid);
00150 static inline bool IsInstanceOf (iObjectRegistry *reg,
00151 csHandlerID instanceid, csHandlerID genericid)
00152 {
00153 return GetRegistry (reg)->IsInstanceOf (instanceid, genericid);
00154 };
00155
00159 bool IsInstance (csHandlerID id);
00160 static inline bool IsInstance (iObjectRegistry *reg,
00161 csHandlerID id)
00162 {
00163 return GetRegistry (reg)->IsInstance (id);
00164 };
00165
00169 csHandlerID const GetGeneric (csHandlerID id);
00170 static inline csHandlerID GetGeneric (iObjectRegistry *reg,
00171 csHandlerID id)
00172 {
00173 return GetRegistry (reg)->GetGeneric (id);
00174 };
00175
00179 const char* GetString (csHandlerID id);
00180 static inline const char* GetString (
00181 iObjectRegistry *reg, csHandlerID id)
00182 {
00183 return GetRegistry (reg)->GetString (id);
00184 };
00185
00186 static csRef<iEventHandlerRegistry> GetRegistry (
00187 iObjectRegistry *object_reg);
00188
00189 private:
00190 iObjectRegistry *object_reg;
00191 csStringSet names;
00192 csHash<csHandlerID, csHandlerID> instantiation;
00193 struct KnownEventHandler
00194 {
00195 iEventHandler* handler;
00196 int refcount;
00197
00198 KnownEventHandler (iEventHandler* handler) : handler (handler),
00199 refcount (1) {}
00200 };
00201 csHash<KnownEventHandler, csHandlerID> idToHandler;
00202 csHash<csHandlerID, csPtrKey<iEventHandler> > handlerToID;
00203 csHash<csHandlerID, csHandlerID> handlerPres;
00204 csHash<csHandlerID, csHandlerID> handlerPosts;
00205 uint32 instanceCounter;
00206 CS::Threading::ReadWriteMutex mutex;
00207 };
00208
00209
00210
00211
00212
00213 struct iFrameEventSignpost : public iEventHandler
00214 {
00215 public:
00216 iFrameEventSignpost () { }
00217 virtual ~iFrameEventSignpost () { }
00218 CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS
00219 virtual bool HandleEvent (iEvent&)
00220 {
00221 return false;
00222 }
00223 };
00224
00225
00226 class FrameSignpost_Logic3D
00227 : public scfImplementation2<FrameSignpost_Logic3D,
00228 iFrameEventSignpost,
00229 scfFakeInterface<iEventHandler> >
00230 {
00231 private:
00232 FrameSignpost_Logic3D () : scfImplementationType (this) { }
00233 public:
00234 CS_EVENTHANDLER_NAMES("crystalspace.signpost.logic3d")
00235 virtual const csHandlerID * GenericPrec
00236 (csRef<iEventHandlerRegistry> &,
00237 csRef<iEventNameRegistry> &,
00238 csEventID) const;
00239 virtual const csHandlerID * GenericSucc
00240 (csRef<iEventHandlerRegistry> &r1,
00241 csRef<iEventNameRegistry> &r2,
00242 csEventID e) const;
00243 };
00244
00245 class FrameSignpost_3D2D
00246 : public scfImplementation2<FrameSignpost_3D2D,
00247 iFrameEventSignpost,
00248 scfFakeInterface<iEventHandler> >
00249 {
00250 private:
00251 FrameSignpost_3D2D () : scfImplementationType (this) { }
00252 public:
00253 CS_EVENTHANDLER_NAMES("crystalspace.signpost.3d2d")
00254 virtual const csHandlerID * GenericPrec
00255 (csRef<iEventHandlerRegistry> &,
00256 csRef<iEventNameRegistry> &,
00257 csEventID) const;
00258 virtual const csHandlerID * GenericSucc
00259 (csRef<iEventHandlerRegistry> &r1,
00260 csRef<iEventNameRegistry> &r2,
00261 csEventID e) const;
00262 };
00263
00264 class FrameSignpost_2DConsole
00265 : public scfImplementation2<FrameSignpost_2DConsole,
00266 iFrameEventSignpost,
00267 scfFakeInterface<iEventHandler> >
00268 {
00269 private:
00270 FrameSignpost_2DConsole () : scfImplementationType (this) { }
00271 public:
00272 CS_EVENTHANDLER_NAMES("crystalspace.signpost.2dconsole")
00273 virtual const csHandlerID * GenericPrec
00274 (csRef<iEventHandlerRegistry> &,
00275 csRef<iEventNameRegistry> &,
00276 csEventID) const;
00277 virtual const csHandlerID * GenericSucc
00278 (csRef<iEventHandlerRegistry> &r1,
00279 csRef<iEventNameRegistry> &r2,
00280 csEventID e) const;
00281 };
00282
00283 class FrameSignpost_ConsoleDebug
00284 : public scfImplementation2<FrameSignpost_ConsoleDebug,
00285 iFrameEventSignpost,
00286 scfFakeInterface<iEventHandler> >
00287 {
00288 private:
00289 FrameSignpost_ConsoleDebug () : scfImplementationType (this) { }
00290 public:
00291 CS_EVENTHANDLER_NAMES("crystalspace.signpost.consoledebug")
00292 virtual const csHandlerID * GenericPrec
00293 (csRef<iEventHandlerRegistry> &,
00294 csRef<iEventNameRegistry> &,
00295 csEventID) const;
00296 virtual const csHandlerID * GenericSucc
00297 (csRef<iEventHandlerRegistry> &r1,
00298 csRef<iEventNameRegistry> &r2,
00299 csEventID e) const;
00300 };
00301
00302 class FrameSignpost_DebugFrame
00303 : public scfImplementation2<FrameSignpost_DebugFrame,
00304 iFrameEventSignpost,
00305 scfFakeInterface<iEventHandler> >
00306 {
00307 private:
00308 FrameSignpost_DebugFrame () : scfImplementationType (this) { }
00309 public:
00310 CS_EVENTHANDLER_NAMES("crystalspace.signpost.debugframe")
00311 virtual const csHandlerID * GenericPrec
00312 (csRef<iEventHandlerRegistry> &,
00313 csRef<iEventNameRegistry> &,
00314 csEventID) const;
00315 virtual const csHandlerID * GenericSucc
00316 (csRef<iEventHandlerRegistry> &r1,
00317 csRef<iEventNameRegistry> &r2,
00318 csEventID e) const;
00319 };
00320
00321
00327 #define CS_EVENTHANDLER_PHASE_LOGIC(x) \
00328 CS_EVENTHANDLER_NAMES(x) \
00329 CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS \
00330 virtual const csHandlerID * GenericPrec \
00331 (csRef<iEventHandlerRegistry> &, csRef<iEventNameRegistry> &, \
00332 csEventID) const { \
00333 return 0; \
00334 } \
00335 virtual const csHandlerID * GenericSucc \
00336 (csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2, \
00337 csEventID event) const { \
00338 if (event != csevFrame(r2)) \
00339 return 0; \
00340 static csHandlerID succConstraint[6] = { \
00341 FrameSignpost_Logic3D::StaticID(r1), \
00342 FrameSignpost_3D2D::StaticID(r1), \
00343 FrameSignpost_2DConsole::StaticID(r1), \
00344 FrameSignpost_ConsoleDebug::StaticID(r1), \
00345 FrameSignpost_DebugFrame::StaticID(r1), \
00346 CS_HANDLERLIST_END \
00347 }; \
00348 return succConstraint; \
00349 }
00350
00357 #define CS_EVENTHANDLER_PHASE_3D(x) \
00358 CS_EVENTHANDLER_NAMES(x) \
00359 CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS \
00360 virtual const csHandlerID * GenericPrec \
00361 (csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2, \
00362 csEventID event) const { \
00363 if (event != csevFrame(r2)) \
00364 return 0; \
00365 static csHandlerID precConstraint[2] = { \
00366 FrameSignpost_Logic3D::StaticID(r1), \
00367 CS_HANDLERLIST_END \
00368 }; \
00369 return precConstraint; \
00370 } \
00371 virtual const csHandlerID * GenericSucc \
00372 (csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2, \
00373 csEventID event) const { \
00374 if (event != csevFrame(r2)) \
00375 return 0; \
00376 static csHandlerID succConstraint[5] = { \
00377 FrameSignpost_3D2D::StaticID(r1), \
00378 FrameSignpost_2DConsole::StaticID(r1), \
00379 FrameSignpost_ConsoleDebug::StaticID(r1), \
00380 FrameSignpost_DebugFrame::StaticID(r1), \
00381 CS_HANDLERLIST_END \
00382 }; \
00383 return succConstraint; \
00384 }
00385
00392 #define CS_EVENTHANDLER_PHASE_2D(x) \
00393 CS_EVENTHANDLER_NAMES(x) \
00394 CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS \
00395 virtual const csHandlerID * GenericPrec \
00396 (csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2, \
00397 csEventID event) const { \
00398 if (event != csevFrame(r2)) \
00399 return 0; \
00400 static csHandlerID precConstraint[3] = { \
00401 FrameSignpost_Logic3D::StaticID(r1), \
00402 FrameSignpost_3D2D::StaticID(r1), \
00403 CS_HANDLERLIST_END \
00404 }; \
00405 return precConstraint; \
00406 } \
00407 virtual const csHandlerID * GenericSucc \
00408 (csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2, \
00409 csEventID event) const { \
00410 if (event != csevFrame(r2)) \
00411 return 0; \
00412 static csHandlerID succConstraint[4] = { \
00413 FrameSignpost_2DConsole::StaticID(r1), \
00414 FrameSignpost_ConsoleDebug::StaticID(r1), \
00415 FrameSignpost_DebugFrame::StaticID(r1), \
00416 CS_HANDLERLIST_END \
00417 }; \
00418 return succConstraint; \
00419 }
00420
00427 #define CS_EVENTHANDLER_PHASE_CONSOLE(x) \
00428 CS_EVENTHANDLER_NAMES(x) \
00429 CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS \
00430 virtual const csHandlerID * GenericPrec \
00431 (csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2, \
00432 csEventID event) const { \
00433 if (event != csevFrame(r2)) \
00434 return 0; \
00435 static csHandlerID precConstraint[4] = { \
00436 FrameSignpost_Logic3D::StaticID(r1), \
00437 FrameSignpost_3D2D::StaticID(r1), \
00438 FrameSignpost_2DConsole::StaticID(r1), \
00439 CS_HANDLERLIST_END \
00440 }; \
00441 return precConstraint; \
00442 } \
00443 virtual const csHandlerID * GenericSucc \
00444 (csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2, \
00445 csEventID event) const { \
00446 if (event != csevFrame(r2)) \
00447 return 0; \
00448 static csHandlerID succConstraint[3] = { \
00449 FrameSignpost_ConsoleDebug::StaticID(r1), \
00450 FrameSignpost_DebugFrame::StaticID(r1), \
00451 CS_HANDLERLIST_END \
00452 }; \
00453 return succConstraint; \
00454 }
00455
00462 #define CS_EVENTHANDLER_PHASE_DEBUG(x) \
00463 CS_EVENTHANDLER_NAMES(x) \
00464 CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS \
00465 virtual const csHandlerID * GenericPrec \
00466 (csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2, \
00467 csEventID event) const { \
00468 if (event != csevFrame(r2)) \
00469 return 0; \
00470 static csHandlerID precConstraint[5] = { \
00471 FrameSignpost_Logic3D::StaticID(r1), \
00472 FrameSignpost_3D2D::StaticID(r1), \
00473 FrameSignpost_2DConsole::StaticID(r1), \
00474 FrameSignpost_ConsoleDebug::StaticID(r1), \
00475 CS_HANDLERLIST_END \
00476 }; \
00477 return precConstraint; \
00478 } \
00479 virtual const csHandlerID * GenericSucc \
00480 (csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2, \
00481 csEventID event) const { \
00482 if (event != csevFrame(r2)) \
00483 return 0; \
00484 static csHandlerID succConstraint[2] = { \
00485 FrameSignpost_DebugFrame::StaticID(r1), \
00486 CS_HANDLERLIST_END \
00487 }; \
00488 return succConstraint; \
00489 }
00490
00496 #define CS_EVENTHANDLER_PHASE_FRAME(x) \
00497 CS_EVENTHANDLER_NAMES(x) \
00498 CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS \
00499 virtual const csHandlerID * GenericPrec \
00500 (csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2, \
00501 csEventID event) const { \
00502 if (event != csevFrame(r2)) \
00503 return 0; \
00504 static csHandlerID precConstraint[6]= { \
00505 FrameSignpost_Logic3D::StaticID(r1), \
00506 FrameSignpost_3D2D::StaticID(r1), \
00507 FrameSignpost_2DConsole::StaticID(r1), \
00508 FrameSignpost_ConsoleDebug::StaticID(r1), \
00509 FrameSignpost_DebugFrame::StaticID(r1), \
00510 CS_HANDLERLIST_END \
00511 }; \
00512 return precConstraint; \
00513 } \
00514 virtual const csHandlerID * GenericSucc \
00515 (csRef<iEventHandlerRegistry> &, csRef<iEventNameRegistry> &, \
00516 csEventID) const { \
00517 return 0; \
00518 }
00519
00520 #endif // __CS_CSUTIL_EVENTHANDLERS_H__