Object.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "LinkedList.h"
  24. #include "Ptr.h"
  25. #include "Variant.h"
  26. namespace Urho3D
  27. {
  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, 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. /// Return execution context.
  64. Context* GetContext() const { return context_; }
  65. /// Return subsystem by type.
  66. Object* GetSubsystem(ShortStringHash type) const;
  67. /// Return active event sender. Null outside event handling.
  68. Object* GetEventSender() const;
  69. /// Return active event handler. Null outside event handling.
  70. EventHandler* GetEventHandler() const;
  71. /// Return whether has subscribed to an event without specific sender.
  72. bool HasSubscribedToEvent(StringHash eventType) const;
  73. /// Return whether has subscribed to a specific sender's event.
  74. bool HasSubscribedToEvent(Object* sender, StringHash eventType) const;
  75. /// Template version of returning a subsystem.
  76. template <class T> T* GetSubsystem() const;
  77. protected:
  78. /// Execution context.
  79. Context* context_;
  80. private:
  81. /// Find the first event handler with no specific sender.
  82. EventHandler* FindEventHandler(StringHash eventType, EventHandler** previous = 0) const;
  83. /// Find the first event handler with specific sender.
  84. EventHandler* FindSpecificEventHandler(Object* sender, EventHandler** previous = 0) const;
  85. /// Find the first event handler with specific sender and event type.
  86. EventHandler* FindSpecificEventHandler(Object* sender, StringHash eventType, EventHandler** previous = 0) const;
  87. /// Remove event handlers related to a specific sender.
  88. void RemoveEventSender(Object* sender);
  89. /// Event handlers. Sender is null for non-specific handlers.
  90. LinkedList<EventHandler> eventHandlers_;
  91. };
  92. template <class T> T* Object::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
  93. /// Base class for object factories.
  94. class ObjectFactory : public RefCounted
  95. {
  96. public:
  97. /// Construct.
  98. ObjectFactory(Context* context) :
  99. context_(context)
  100. {
  101. assert(context_);
  102. }
  103. /// Create an object. Implemented in templated subclasses.
  104. virtual SharedPtr<Object> CreateObject() = 0;
  105. /// Return execution context.
  106. Context* GetContext() const { return context_; }
  107. /// Return type hash of objects created by this factory.
  108. ShortStringHash GetType() const { return type_; }
  109. /// Return type name of objects created by this factory.
  110. const String& GetTypeName() const { return typeName_; }
  111. protected:
  112. /// Execution context.
  113. Context* context_;
  114. /// Object type.
  115. ShortStringHash type_;
  116. /// Object type name.
  117. String typeName_;
  118. };
  119. /// Template implementation of the object factory.
  120. template <class T> class ObjectFactoryImpl : public ObjectFactory
  121. {
  122. public:
  123. /// Construct.
  124. ObjectFactoryImpl(Context* context) :
  125. ObjectFactory(context)
  126. {
  127. type_ = T::GetTypeStatic();
  128. typeName_ = T::GetTypeNameStatic();
  129. }
  130. /// Create an object of the specific type.
  131. virtual SharedPtr<Object>(CreateObject()) { return SharedPtr<Object>(new T(context_)); }
  132. };
  133. /// Internal helper class for invoking event handler functions.
  134. class EventHandler : public LinkedListNode
  135. {
  136. public:
  137. /// Construct with specified receiver.
  138. EventHandler(Object* receiver) :
  139. receiver_(receiver),
  140. sender_(0),
  141. userData_(0)
  142. {
  143. assert(receiver_);
  144. }
  145. /// Construct with specified receiver and userdata.
  146. EventHandler(Object* receiver, void* userData) :
  147. receiver_(receiver),
  148. sender_(0),
  149. userData_(userData)
  150. {
  151. assert(receiver_);
  152. }
  153. /// Destruct.
  154. virtual ~EventHandler() {}
  155. /// Set sender and event type.
  156. void SetSenderAndEventType(Object* sender, StringHash eventType)
  157. {
  158. sender_ = sender;
  159. eventType_ = eventType;
  160. }
  161. /// Invoke event handler function.
  162. virtual void Invoke(VariantMap& eventData) = 0;
  163. /// Return event receiver.
  164. Object* GetReceiver() const { return receiver_; }
  165. /// Return event sender. Null if the handler is non-specific.
  166. Object* GetSender() const { return sender_; }
  167. /// Return event type.
  168. const StringHash& GetEventType() const { return eventType_; }
  169. /// Return userdata.
  170. void* GetUserData() const { return userData_; }
  171. protected:
  172. /// Event receiver.
  173. Object* receiver_;
  174. /// Event sender.
  175. Object* sender_;
  176. /// Event type.
  177. StringHash eventType_;
  178. /// Userdata.
  179. void* userData_;
  180. };
  181. /// Template implementation of the event handler invoke helper (stores a function pointer of specific class.)
  182. template <class T> class EventHandlerImpl : public EventHandler
  183. {
  184. public:
  185. typedef void (T::*HandlerFunctionPtr)(StringHash, VariantMap&);
  186. /// Construct with receiver and function pointers.
  187. EventHandlerImpl(T* receiver, HandlerFunctionPtr function) :
  188. EventHandler(receiver),
  189. function_(function)
  190. {
  191. assert(function_);
  192. }
  193. /// Construct with receiver and function pointers and userdata.
  194. EventHandlerImpl(T* receiver, HandlerFunctionPtr function, void* userData) :
  195. EventHandler(receiver, userData),
  196. function_(function)
  197. {
  198. assert(function_);
  199. }
  200. /// Invoke event handler function.
  201. virtual void Invoke(VariantMap& eventData)
  202. {
  203. T* receiver = static_cast<T*>(receiver_);
  204. (receiver->*function_)(eventType_, eventData);
  205. }
  206. private:
  207. /// Class-specific pointer to handler function.
  208. HandlerFunctionPtr function_;
  209. };
  210. #define OBJECT(typeName) \
  211. private: \
  212. static const ShortStringHash typeStatic; \
  213. static const String typeNameStatic; \
  214. public: \
  215. virtual ShortStringHash GetType() const { return GetTypeStatic(); } \
  216. virtual const String& GetTypeName() const { return GetTypeNameStatic(); } \
  217. static ShortStringHash GetTypeStatic() { return typeStatic; } \
  218. static const String& GetTypeNameStatic() { return typeNameStatic; } \
  219. #define OBJECTTYPESTATIC(typeName) \
  220. const ShortStringHash typeName::typeStatic(#typeName); \
  221. const String typeName::typeNameStatic(#typeName); \
  222. #define EVENT(eventID, eventName) static const StringHash eventID(#eventName); namespace eventName
  223. #define PARAM(paramID, paramName) static const ShortStringHash paramID(#paramName)
  224. #define HANDLER(className, function) (new EventHandlerImpl<className>(this, &className::function))
  225. #define HANDLER_USERDATA(className, function, userData) (new EventHandlerImpl<className>(this, &className::function, userData))
  226. }