eventManager.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _EVENTMANAGER_H_
  23. #define _EVENTMANAGER_H_
  24. #ifndef _CONSOLE_H_
  25. #include "console/console.h"
  26. #endif
  27. #ifndef _DISPATCHER_H_
  28. #include "util/messaging/dispatcher.h"
  29. #endif
  30. #ifndef _TSIMPLEHASHTABLE_H
  31. #include "core/tSimpleHashTable.h"
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. /// Listener class used by the EventManager to dispatch messages to specific
  35. /// callbacks.
  36. //-----------------------------------------------------------------------------
  37. class EventManagerListener : public Dispatcher::IMessageListener
  38. {
  39. friend class EventManager;
  40. /// Stores subscription information for a subscriber.
  41. struct Subscriber
  42. {
  43. SimObjectPtr< SimObject > listener; ///< The listener object.
  44. StringTableEntry callback; ///< The callback to execute when the event is triggered.
  45. StringTableEntry event; ///< The event being listened for.
  46. U32 callDepth;
  47. bool removeFlag;
  48. };
  49. /// Subscriber table hashed by event name.
  50. SimpleHashTable< Vector<Subscriber> > mSubscribers;
  51. public:
  52. // Ensure that the subscriber map doesn't use case-sensitive string comparisons.
  53. EventManagerListener(): mSubscribers(64, false) {}
  54. /// Called by the EventManager queue when an event is triggered. Calls all listeners subscribed to the triggered event.
  55. virtual bool onMessageReceived( StringTableEntry queue, const char* event, const char* data );
  56. virtual bool onMessageObjectReceived( StringTableEntry queue, Message *msg ) { return true; };
  57. };
  58. /// @addtogroup msgsys Message System
  59. // @{
  60. //-----------------------------------------------------------------------------
  61. /// The EventManager class is a wrapper for the standard messaging system. It
  62. /// provides functionality for management of event queues, events, and
  63. /// subscriptions.
  64. ///
  65. /// Creating an EventManager is as simple as calling <tt>new EventManager</tt>
  66. /// and specifying a queue name.
  67. ///
  68. /// Example Usage:
  69. ///
  70. /// @code
  71. /// // Create the EventManager.
  72. /// $MyEventManager = new EventManager() { queue = "MyEventManager"; };
  73. ///
  74. /// // Create an event.
  75. /// $MyEventManager.registerEvent( "SomeCoolEvent" );
  76. ///
  77. /// // Create a listener and subscribe.
  78. /// $MyListener = new ScriptMsgListener() { class = MyListener; };
  79. /// $MyEventManager.subscribe( $MyListener, "SomeCoolEvent" );
  80. ///
  81. /// function MyListener::onSomeCoolEvent( %this, %data )
  82. /// {
  83. /// echo( "onSomeCoolEvent Triggered" );
  84. /// }
  85. ///
  86. /// // Trigger the event.
  87. /// $MyEventManager.postEvent( "SomeCoolEvent", "Data" );
  88. /// @endcode
  89. //-----------------------------------------------------------------------------
  90. class EventManager : public SimObject
  91. {
  92. typedef SimObject Parent;
  93. private:
  94. /// The name of the message queue.
  95. StringTableEntry mQueue;
  96. /// Registered events.
  97. Vector<StringTableEntry> mEvents;
  98. /// The event listener. Listens for all events and dispatches them to the appropriate subscribers.
  99. EventManagerListener mListener;
  100. /// List of all EventManagers.
  101. static Vector<EventManager*> smEventManagers;
  102. /// Sets the message queue.
  103. static bool _setMessageQueue( void *obj, const char *index, const char *data )
  104. {
  105. static_cast<EventManager*>( obj )->setMessageQueue( data );
  106. return false;
  107. };
  108. public:
  109. DECLARE_CONOBJECT( EventManager );
  110. EventManager();
  111. virtual ~EventManager();
  112. static void initPersistFields();
  113. /// @name Properties
  114. /// @{
  115. StringTableEntry getMessageQueue() const { return mQueue; }
  116. void setMessageQueue( const char* queue );
  117. /// @}
  118. /// @name Event Management
  119. /// @{
  120. /// Checks if an event is registered.
  121. bool isRegisteredEvent( const char* eventName );
  122. /// Registers an event.
  123. bool registerEvent( const char* eventName );
  124. /// Removes an event.
  125. void unregisterEvent( const char* eventName );
  126. /// Removes all events.
  127. void unregisterAllEvents();
  128. /// Triggers an event.
  129. bool postEvent( const char* eventName, const char* data );
  130. /// Adds a subscription to an event.
  131. bool subscribe( SimObject *callbackObj, const char* event, const char* callback = NULL );
  132. /// Remove a subscriber from an event.
  133. void remove( SimObject *cbObj, const char* event );
  134. void removeAll( SimObject *cbObj );
  135. /// @}
  136. /// @name Debug Output
  137. /// @{
  138. /// Prints all registered events to the console.
  139. void dumpEvents();
  140. /// Prints all subscribers to the console.
  141. void dumpSubscribers();
  142. /// Prints subscribers to a specific event to the console.
  143. void dumpSubscribers( const char* event );
  144. /// @}
  145. /// @name Event Manager Tracking
  146. /// @{
  147. /// Adds an EventManager.
  148. static void addEventManager( EventManager* em ) { smEventManagers.push_back( em ); };
  149. /// Removes an EventManager.
  150. static void removeEventManager( EventManager* em )
  151. {
  152. for( Vector<EventManager*>::iterator iter = smEventManagers.begin(); iter != smEventManagers.end(); iter++ )
  153. {
  154. if( *iter == em )
  155. {
  156. smEventManagers.erase_fast( iter );
  157. break;
  158. }
  159. }
  160. };
  161. /// Retrieves an EventManager.
  162. static EventManager* getEventManager( const char* name )
  163. {
  164. StringTableEntry queue = StringTable->insert( name );
  165. for( Vector<EventManager*>::iterator iter = smEventManagers.begin(); iter != smEventManagers.end(); iter++ )
  166. {
  167. if( ( *iter )->mQueue == queue )
  168. return *iter;
  169. }
  170. return NULL;
  171. };
  172. /// Prints all the EventManagers to the console.
  173. static void printEventManagers()
  174. {
  175. for( Vector<EventManager*>::iterator iter = smEventManagers.begin(); iter != smEventManagers.end(); iter++ )
  176. ( *iter )->dumpSubscribers();
  177. }
  178. /// @}
  179. };
  180. // @}
  181. #endif // _EVENTMANAGER_H_