Object.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // Copyright (c) 2008-2014 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 "Variant.h"
  25. namespace Urho3D
  26. {
  27. class Context;
  28. class EventHandler;
  29. #define OBJECT(typeName) \
  30. public: \
  31. virtual Urho3D::ShortStringHash GetType() const { return GetTypeStatic(); } \
  32. virtual Urho3D::ShortStringHash GetBaseType() const { return GetBaseTypeStatic(); } \
  33. virtual const Urho3D::String& GetTypeName() const { return GetTypeNameStatic(); } \
  34. static Urho3D::ShortStringHash GetTypeStatic() { static const Urho3D::ShortStringHash typeStatic(#typeName); return typeStatic; } \
  35. static const Urho3D::String& GetTypeNameStatic() { static const Urho3D::String typeNameStatic(#typeName); return typeNameStatic; } \
  36. #define BASEOBJECT(typeName) \
  37. public: \
  38. static Urho3D::ShortStringHash GetBaseTypeStatic() { static const Urho3D::ShortStringHash baseTypeStatic(#typeName); return baseTypeStatic; } \
  39. /// Base class for objects with type identification, subsystem access and event sending/receiving capability.
  40. class URHO3D_API Object : public RefCounted
  41. {
  42. BASEOBJECT(Object);
  43. friend class Context;
  44. public:
  45. /// Construct.
  46. Object(Context* context);
  47. /// Destruct. Clean up self from event sender & receiver structures.
  48. virtual ~Object();
  49. /// Return type hash.
  50. virtual ShortStringHash GetType() const = 0;
  51. /// Return base class type hash.
  52. virtual ShortStringHash GetBaseType() const = 0;
  53. /// Return type name.
  54. virtual const String& GetTypeName() const = 0;
  55. /// Handle event.
  56. virtual void OnEvent(Object* sender, StringHash eventType, VariantMap& eventData);
  57. /// Subscribe to an event that can be sent by any sender.
  58. void SubscribeToEvent(StringHash eventType, EventHandler* handler);
  59. /// Subscribe to a specific sender's event.
  60. void SubscribeToEvent(Object* sender, StringHash eventType, EventHandler* handler);
  61. /// Unsubscribe from an event.
  62. void UnsubscribeFromEvent(StringHash eventType);
  63. /// Unsubscribe from a specific sender's event.
  64. void UnsubscribeFromEvent(Object* sender, StringHash eventType);
  65. /// Unsubscribe from a specific sender's events.
  66. void UnsubscribeFromEvents(Object* sender);
  67. /// Unsubscribe from all events.
  68. void UnsubscribeFromAllEvents();
  69. /// Unsubscribe from all events except those listed, and optionally only those with userdata (script registered events.)
  70. void UnsubscribeFromAllEventsExcept(const PODVector<StringHash>& exceptions, bool onlyUserData);
  71. /// Send event to all subscribers.
  72. void SendEvent(StringHash eventType);
  73. /// Send event with parameters to all subscribers.
  74. void SendEvent(StringHash eventType, VariantMap& eventData);
  75. /// Return a preallocated map for event data. Used for optimization to avoid constant re-allocation of event data maps.
  76. VariantMap& GetEventDataMap() const;
  77. /// Return execution context.
  78. Context* GetContext() const { return context_; }
  79. /// Return subsystem by type.
  80. Object* GetSubsystem(ShortStringHash type) const;
  81. /// Return active event sender. Null outside event handling.
  82. Object* GetEventSender() const;
  83. /// Return active event handler. Null outside event handling.
  84. EventHandler* GetEventHandler() const;
  85. /// Return whether has subscribed to an event without specific sender.
  86. bool HasSubscribedToEvent(StringHash eventType) const;
  87. /// Return whether has subscribed to a specific sender's event.
  88. bool HasSubscribedToEvent(Object* sender, StringHash eventType) const;
  89. /// Return whether has subscribed to any event.
  90. bool HasEventHandlers() const { return !eventHandlers_.Empty(); }
  91. /// Template version of returning a subsystem.
  92. template <class T> T* GetSubsystem() const;
  93. /// Return object category. Categories are (optionally) registered along with the object factory. Return an empty string if the object category is not registered.
  94. const String& GetCategory() const;
  95. protected:
  96. /// Execution context.
  97. Context* context_;
  98. private:
  99. /// Find the first event handler with no specific sender.
  100. EventHandler* FindEventHandler(StringHash eventType, EventHandler** previous = 0) const;
  101. /// Find the first event handler with specific sender.
  102. EventHandler* FindSpecificEventHandler(Object* sender, EventHandler** previous = 0) const;
  103. /// Find the first event handler with specific sender and event type.
  104. EventHandler* FindSpecificEventHandler(Object* sender, StringHash eventType, EventHandler** previous = 0) const;
  105. /// Remove event handlers related to a specific sender.
  106. void RemoveEventSender(Object* sender);
  107. /// Event handlers. Sender is null for non-specific handlers.
  108. LinkedList<EventHandler> eventHandlers_;
  109. };
  110. template <class T> T* Object::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
  111. /// Base class for object factories.
  112. class URHO3D_API ObjectFactory : public RefCounted
  113. {
  114. public:
  115. /// Construct.
  116. ObjectFactory(Context* context) :
  117. context_(context)
  118. {
  119. assert(context_);
  120. }
  121. /// Create an object. Implemented in templated subclasses.
  122. virtual SharedPtr<Object> CreateObject() = 0;
  123. /// Return execution context.
  124. Context* GetContext() const { return context_; }
  125. /// Return type hash of objects created by this factory.
  126. ShortStringHash GetType() const { return type_; }
  127. /// Return base type hash of objects created by this factory.
  128. ShortStringHash GetBaseType() const { return baseType_; }
  129. /// Return type name of objects created by this factory.
  130. const String& GetTypeName() const { return typeName_; }
  131. protected:
  132. /// Execution context.
  133. Context* context_;
  134. /// Object type.
  135. ShortStringHash type_;
  136. /// Object base type.
  137. ShortStringHash baseType_;
  138. /// Object type name.
  139. String typeName_;
  140. };
  141. /// Template implementation of the object factory.
  142. template <class T> class ObjectFactoryImpl : public ObjectFactory
  143. {
  144. public:
  145. /// Construct.
  146. ObjectFactoryImpl(Context* context) :
  147. ObjectFactory(context)
  148. {
  149. type_ = T::GetTypeStatic();
  150. baseType_ = T::GetBaseTypeStatic();
  151. typeName_ = T::GetTypeNameStatic();
  152. }
  153. /// Create an object of the specific type.
  154. virtual SharedPtr<Object>(CreateObject()) { return SharedPtr<Object>(new T(context_)); }
  155. };
  156. /// Internal helper class for invoking event handler functions.
  157. class URHO3D_API EventHandler : public LinkedListNode
  158. {
  159. public:
  160. /// Construct with specified receiver.
  161. EventHandler(Object* receiver) :
  162. receiver_(receiver),
  163. sender_(0),
  164. userData_(0)
  165. {
  166. assert(receiver_);
  167. }
  168. /// Construct with specified receiver and userdata.
  169. EventHandler(Object* receiver, void* userData) :
  170. receiver_(receiver),
  171. sender_(0),
  172. userData_(userData)
  173. {
  174. assert(receiver_);
  175. }
  176. /// Destruct.
  177. virtual ~EventHandler() {}
  178. /// Set sender and event type.
  179. void SetSenderAndEventType(Object* sender, StringHash eventType)
  180. {
  181. sender_ = sender;
  182. eventType_ = eventType;
  183. }
  184. /// Invoke event handler function.
  185. virtual void Invoke(VariantMap& eventData) = 0;
  186. /// Return event receiver.
  187. Object* GetReceiver() const { return receiver_; }
  188. /// Return event sender. Null if the handler is non-specific.
  189. Object* GetSender() const { return sender_; }
  190. /// Return event type.
  191. const StringHash& GetEventType() const { return eventType_; }
  192. /// Return userdata.
  193. void* GetUserData() const { return userData_; }
  194. protected:
  195. /// Event receiver.
  196. Object* receiver_;
  197. /// Event sender.
  198. Object* sender_;
  199. /// Event type.
  200. StringHash eventType_;
  201. /// Userdata.
  202. void* userData_;
  203. };
  204. /// Template implementation of the event handler invoke helper (stores a function pointer of specific class.)
  205. template <class T> class EventHandlerImpl : public EventHandler
  206. {
  207. public:
  208. typedef void (T::*HandlerFunctionPtr)(StringHash, VariantMap&);
  209. /// Construct with receiver and function pointers.
  210. EventHandlerImpl(T* receiver, HandlerFunctionPtr function) :
  211. EventHandler(receiver),
  212. function_(function)
  213. {
  214. assert(function_);
  215. }
  216. /// Construct with receiver and function pointers and userdata.
  217. EventHandlerImpl(T* receiver, HandlerFunctionPtr function, void* userData) :
  218. EventHandler(receiver, userData),
  219. function_(function)
  220. {
  221. assert(function_);
  222. }
  223. /// Invoke event handler function.
  224. virtual void Invoke(VariantMap& eventData)
  225. {
  226. T* receiver = static_cast<T*>(receiver_);
  227. (receiver->*function_)(eventType_, eventData);
  228. }
  229. private:
  230. /// Class-specific pointer to handler function.
  231. HandlerFunctionPtr function_;
  232. };
  233. #define EVENT(eventID, eventName) static const Urho3D::StringHash eventID(#eventName); namespace eventName
  234. #define PARAM(paramID, paramName) static const Urho3D::ShortStringHash paramID(#paramName)
  235. #define HANDLER(className, function) (new Urho3D::EventHandlerImpl<className>(this, &className::function))
  236. #define HANDLER_USERDATA(className, function, userData) (new Urho3D::EventHandlerImpl<className>(this, &className::function, userData))
  237. }