Object.h 10 KB

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