eventManager.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #include "console/console.h"
  23. #include "messaging/dispatcher.h"
  24. #include "collection/simpleHashTable.h"
  25. #ifndef _EVENTMANAGER_H_
  26. #define _EVENTMANAGER_H_
  27. //-----------------------------------------------------------------------------
  28. /// Listener class used by the EventManager to dispatch messages to specific
  29. /// callbacks.
  30. //-----------------------------------------------------------------------------
  31. class EventManagerListener : public Dispatcher::IMessageListener
  32. {
  33. friend class EventManager;
  34. /// Stores subscription information for a subscriber.
  35. struct Subscriber
  36. {
  37. SimObject* listener; ///< The listener object.
  38. StringTableEntry callback; ///< The callback to execute when the event is triggered.
  39. StringTableEntry event; ///< The event being listened for.
  40. };
  41. /// Subscriber table hashed by event name.
  42. SimpleHashTable< Vector<Subscriber> > mSubscribers;
  43. public:
  44. /// Called by the EventManager queue when an event is triggered. Calls all listeners subscribed to the triggered event.
  45. virtual bool onMessageReceived( StringTableEntry queue, const char* event, const char* data );
  46. virtual bool onMessageObjectReceived( StringTableEntry queue, Message *msg ) { return true; };
  47. };
  48. /// @addtogroup msgsys Message System
  49. // @{
  50. //-----------------------------------------------------------------------------
  51. /// The EventManager class is a wrapper for the standard messaging system. It
  52. /// provides functionality for management of event queues, events, and
  53. /// subscriptions.
  54. ///
  55. /// Creating an EventManager is as simple as calling <tt>new EventManager</tt>
  56. /// and specifying a queue name.
  57. ///
  58. /// Example Usage:
  59. ///
  60. /// @code
  61. /// // Create the EventManager.
  62. /// $MyEventManager = new EventManager() { queue = "MyEventManager"; };
  63. ///
  64. /// // Create an event.
  65. /// $MyEventManager.registerEvent( "SomeCoolEvent" );
  66. ///
  67. /// // Create a listener and subscribe.
  68. /// $MyListener = new ScriptMsgListener() { class = MyListener; };
  69. /// $MyEventManager.subscribe( $MyListener, "SomeCoolEvent" );
  70. ///
  71. /// function MyListener::onSomeCoolEvent( %this, %data )
  72. /// {
  73. /// echo( "onSomeCoolEvent Triggered" );
  74. /// }
  75. ///
  76. /// // Trigger the event.
  77. /// $MyEventManager.postEvent( "SomeCoolEvent", "Data" );
  78. /// @endcode
  79. //-----------------------------------------------------------------------------
  80. class EventManager : public SimObject
  81. {
  82. typedef SimObject Parent;
  83. private:
  84. /// The name of the message queue.
  85. StringTableEntry mQueue;
  86. /// Registered events.
  87. Vector<StringTableEntry> mEvents;
  88. /// The event listener. Listens for all events and dispatches them to the appropriate subscribers.
  89. EventManagerListener mListener;
  90. /// List of all EventManagers.
  91. static Vector<EventManager*> smEventManagers;
  92. public:
  93. DECLARE_CONOBJECT( EventManager );
  94. EventManager();
  95. virtual ~EventManager();
  96. static void initPersistFields();
  97. /// @name Properties
  98. /// @{
  99. /// Sets the message queue.
  100. static bool setMessageQueue( void* obj, const char* data )
  101. {
  102. static_cast<EventManager*>( obj )->setMessageQueue( data );
  103. return false;
  104. };
  105. void setMessageQueue( const char* queue );
  106. /// @}
  107. /// @name Event Management
  108. /// @{
  109. /// Checks if an event is registered.
  110. bool isRegisteredEvent( const char* eventName );
  111. /// Registers an event.
  112. bool registerEvent( const char* eventName );
  113. /// Removes an event.
  114. void unregisterEvent( const char* eventName );
  115. /// Removes all events.
  116. void unregisterAllEvents();
  117. /// Triggers an event.
  118. bool postEvent( const char* eventName, const char* data );
  119. /// Adds a subscription to an event.
  120. bool subscribe( SimObject *callbackObj, const char* event, const char* callback = NULL );
  121. /// Remove a subscriber from an event.
  122. void remove( SimObject *cbObj, const char* event );
  123. /// @}
  124. /// @name Debug Output
  125. /// @{
  126. /// Prints all registered events to the console.
  127. void dumpEvents();
  128. /// Prints all subscribers to the console.
  129. void dumpSubscribers();
  130. /// Prints subscribers to a specific event to the console.
  131. void dumpSubscribers( const char* event );
  132. /// @}
  133. /// @name Event Manager Tracking
  134. /// @{
  135. /// Adds an EventManager.
  136. static void addEventManager( EventManager* em ) { smEventManagers.push_back( em ); };
  137. /// Removes an EventManager.
  138. static void removeEventManager( EventManager* em )
  139. {
  140. for( Vector<EventManager*>::iterator iter = smEventManagers.begin(); iter != smEventManagers.end(); iter++ )
  141. {
  142. if( *iter == em )
  143. {
  144. smEventManagers.erase_fast( iter );
  145. break;
  146. }
  147. }
  148. };
  149. /// Retrieves an EventManager.
  150. static EventManager* getEventManager( const char* name )
  151. {
  152. StringTableEntry queue = StringTable->insert( name );
  153. for( Vector<EventManager*>::iterator iter = smEventManagers.begin(); iter != smEventManagers.end(); iter++ )
  154. {
  155. if( ( *iter )->mQueue == queue )
  156. return *iter;
  157. }
  158. return NULL;
  159. };
  160. /// Prints all the EventManagers to the console.
  161. static void printEventManagers()
  162. {
  163. for( Vector<EventManager*>::iterator iter = smEventManagers.begin(); iter != smEventManagers.end(); iter++ )
  164. ( *iter )->dumpSubscribers();
  165. }
  166. /// @}
  167. };
  168. // @}
  169. #endif // _EVENTMANAGER_H_