Object.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "HashMap.h"
  25. #include "Map.h"
  26. #include "Ptr.h"
  27. #include "Variant.h"
  28. class Context;
  29. class EventHandler;
  30. /// Base class for objects with type identification, subsystem access and event sending/receiving capability.
  31. class Object : public RefCounted
  32. {
  33. friend class Context;
  34. public:
  35. /// Construct.
  36. Object(Context* context);
  37. /// Destruct. Clean up self from event sender & receiver structures.
  38. virtual ~Object();
  39. /// Return type hash.
  40. virtual ShortStringHash GetType() const = 0;
  41. /// Return type name.
  42. virtual const String& GetTypeName() const = 0;
  43. /// Handle event.
  44. virtual void OnEvent(Object* sender, bool broadcast, StringHash eventType, VariantMap& eventData);
  45. /// Subscribe to an event that can be sent by any sender.
  46. void SubscribeToEvent(StringHash eventType, EventHandler* handler);
  47. /// Subscribe to a specific sender's event.
  48. void SubscribeToEvent(Object* sender, StringHash eventType, EventHandler* handler);
  49. /// Unsubscribe from an event.
  50. void UnsubscribeFromEvent(StringHash eventType);
  51. /// Unsubscribe from a specific sender's event.
  52. void UnsubscribeFromEvent(Object* sender, StringHash eventType);
  53. /// Unsubscribe from a specific sender's events.
  54. void UnsubscribeFromEvents(Object* sender);
  55. /// Unsubscribe from all events.
  56. void UnsubscribeFromAllEvents();
  57. /// Unsubscribe from all events with userdata defined in the handler.
  58. void UnsubscribeFromAllEventsWithUserData();
  59. /// Send event to all subscribers.
  60. void SendEvent(StringHash eventType);
  61. /// Send event with parameters to all subscribers.
  62. void SendEvent(StringHash eventType, VariantMap& eventData);
  63. /// Send event to a specific receiver.
  64. void SendEvent(Object* receiver, StringHash eventType);
  65. /// Send event with parameters to a specific receiver.
  66. void SendEvent(Object* receiver, StringHash eventType, VariantMap& eventData);
  67. /// Return execution context.
  68. Context* GetContext() const { return context_; }
  69. /// Return subsystem by type.
  70. Object* GetSubsystem(ShortStringHash type) const;
  71. /// Return active event sender. Null outside event handling.
  72. Object* GetEventSender() const;
  73. /// Return active event handler. Null outside event handling.
  74. EventHandler* GetEventHandler() const;
  75. /// Return whether has subscribed to an event without specific sender.
  76. bool HasSubscribedToEvent(StringHash eventType) const;
  77. /// Return whether has subscribed to a specific sender's event.
  78. bool HasSubscribedToEvent(Object* sender, StringHash eventType) const;
  79. /// Template version of returning a subsystem.
  80. template <class T> T* GetSubsystem() const;
  81. protected:
  82. /// Execution context.
  83. Context* context_;
  84. private:
  85. /// Remove event handlers related to a specific sender.
  86. void RemoveEventSender(Object* sender);
  87. /// Event handlers. Sender is null for non-specific handlers.
  88. Map<Pair<Object*, StringHash>, SharedPtr<EventHandler> > eventHandlers_;
  89. };
  90. template <class T> T* Object::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
  91. /// Base class for object factories.
  92. class ObjectFactory : public RefCounted
  93. {
  94. public:
  95. /// Construct.
  96. ObjectFactory(Context* context) :
  97. context_(context)
  98. {
  99. assert(context_);
  100. }
  101. /// Create an object. Implemented in templated subclasses.
  102. virtual SharedPtr<Object> CreateObject() = 0;
  103. /// Return execution context.
  104. Context* GetContext() const { return context_; }
  105. /// Return type hash of objects created by this factory.
  106. ShortStringHash GetType() const { return type_; }
  107. /// Return type name of objects created by this factory.
  108. const String& GetTypeName() const { return typeName_; }
  109. protected:
  110. /// Execution context.
  111. Context* context_;
  112. /// Object type.
  113. ShortStringHash type_;
  114. /// Object type name.
  115. String typeName_;
  116. };
  117. /// Template implementation of the object factory.
  118. template <class T> class ObjectFactoryImpl : public ObjectFactory
  119. {
  120. public:
  121. /// Construct.
  122. ObjectFactoryImpl(Context* context) :
  123. ObjectFactory(context)
  124. {
  125. type_ = T::GetTypeStatic();
  126. typeName_ = T::GetTypeNameStatic();
  127. }
  128. /// Create an object of the specific type.
  129. virtual SharedPtr<Object>(CreateObject()) { return SharedPtr<Object>(new T(context_)); }
  130. };
  131. /// Internal helper class for invoking event handler functions.
  132. class EventHandler : public RefCounted
  133. {
  134. public:
  135. /// Construct with specified receiver.
  136. EventHandler(Object* receiver) :
  137. receiver_(receiver),
  138. userData_(0)
  139. {
  140. assert(receiver_);
  141. }
  142. /// Construct with specified receiver and userdata.
  143. EventHandler(Object* receiver, void* userData) :
  144. receiver_(receiver),
  145. userData_(userData)
  146. {
  147. assert(receiver_);
  148. }
  149. /// Destruct.
  150. virtual ~EventHandler() {}
  151. /// Invoke event handler function.
  152. virtual void Invoke(StringHash eventType, VariantMap& eventData) = 0;
  153. /// Return event receiver.
  154. Object* GetReceiver() const { return receiver_; }
  155. /// Return userdata.
  156. void* GetUserData() const { return userData_; }
  157. protected:
  158. /// Event receiver.
  159. Object* receiver_;
  160. /// Userdata.
  161. void* userData_;
  162. };
  163. /// Template implementation of the event handler invoke helper (stores a function pointer of specific class.)
  164. template <class T> class EventHandlerImpl : public EventHandler
  165. {
  166. public:
  167. typedef void (T::*HandlerFunctionPtr)(StringHash, VariantMap&);
  168. /// Construct with receiver and function pointers.
  169. EventHandlerImpl(T* receiver, HandlerFunctionPtr function) :
  170. EventHandler(receiver),
  171. function_(function)
  172. {
  173. assert(function_);
  174. }
  175. /// Construct with receiver and function pointers and userdata.
  176. EventHandlerImpl(T* receiver, HandlerFunctionPtr function, void* userData) :
  177. EventHandler(receiver, userData),
  178. function_(function)
  179. {
  180. assert(function_);
  181. }
  182. /// Invoke event handler function.
  183. virtual void Invoke(StringHash eventType, VariantMap& eventData)
  184. {
  185. T* receiver = static_cast<T*>(receiver_);
  186. (receiver->*function_)(eventType, eventData);
  187. }
  188. private:
  189. /// Class-specific pointer to handler function.
  190. HandlerFunctionPtr function_;
  191. };
  192. #define OBJECT(typeName) \
  193. private: \
  194. static const ShortStringHash typeStatic; \
  195. static const String typeNameStatic; \
  196. public: \
  197. virtual ShortStringHash GetType() const { return GetTypeStatic(); } \
  198. virtual const String& GetTypeName() const { return GetTypeNameStatic(); } \
  199. static ShortStringHash GetTypeStatic() { return typeStatic; } \
  200. static const String& GetTypeNameStatic() { return typeNameStatic; } \
  201. #define OBJECTTYPESTATIC(typeName) \
  202. const ShortStringHash typeName::typeStatic(#typeName); \
  203. const String typeName::typeNameStatic(#typeName); \
  204. #define EVENT(eventID, eventName) static const StringHash eventID(#eventName); namespace eventName
  205. #define PARAM(paramID, paramName) static const ShortStringHash paramID(#paramName)
  206. #define HANDLER(className, function) (new EventHandlerImpl<className>(this, &className::function))
  207. #define HANDLER_USERDATA(className, function, userData) (new EventHandlerImpl<className>(this, &className::function, userData))