Object.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 URHO3D_API 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 except those listed, and optionally only those with userdata (script registered events.)
  58. void UnsubscribeFromAllEventsExcept(const PODVector<StringHash>& exceptions, bool onlyUserData);
  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. /// Return object category. Categories are (optionally) registered along with the object factory. Return an empty string if the object category is not registered.
  78. const String& GetCategory() const;
  79. protected:
  80. /// Execution context.
  81. Context* context_;
  82. private:
  83. /// Find the first event handler with no specific sender.
  84. EventHandler* FindEventHandler(StringHash eventType, EventHandler** previous = 0) const;
  85. /// Find the first event handler with specific sender.
  86. EventHandler* FindSpecificEventHandler(Object* sender, EventHandler** previous = 0) const;
  87. /// Find the first event handler with specific sender and event type.
  88. EventHandler* FindSpecificEventHandler(Object* sender, StringHash eventType, EventHandler** previous = 0) const;
  89. /// Remove event handlers related to a specific sender.
  90. void RemoveEventSender(Object* sender);
  91. /// Event handlers. Sender is null for non-specific handlers.
  92. LinkedList<EventHandler> eventHandlers_;
  93. };
  94. template <class T> T* Object::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
  95. /// Base class for object factories.
  96. class URHO3D_API ObjectFactory : public RefCounted
  97. {
  98. public:
  99. /// Construct.
  100. ObjectFactory(Context* context) :
  101. context_(context)
  102. {
  103. assert(context_);
  104. }
  105. /// Create an object. Implemented in templated subclasses.
  106. virtual SharedPtr<Object> CreateObject() = 0;
  107. /// Return execution context.
  108. Context* GetContext() const { return context_; }
  109. /// Return type hash of objects created by this factory.
  110. ShortStringHash GetType() const { return type_; }
  111. /// Return type name of objects created by this factory.
  112. const String& GetTypeName() const { return typeName_; }
  113. protected:
  114. /// Execution context.
  115. Context* context_;
  116. /// Object type.
  117. ShortStringHash type_;
  118. /// Object type name.
  119. String typeName_;
  120. };
  121. /// Template implementation of the object factory.
  122. template <class T> class ObjectFactoryImpl : public ObjectFactory
  123. {
  124. public:
  125. /// Construct.
  126. ObjectFactoryImpl(Context* context) :
  127. ObjectFactory(context)
  128. {
  129. type_ = T::GetTypeStatic();
  130. typeName_ = T::GetTypeNameStatic();
  131. }
  132. /// Create an object of the specific type.
  133. virtual SharedPtr<Object>(CreateObject()) { return SharedPtr<Object>(new T(context_)); }
  134. };
  135. /// Internal helper class for invoking event handler functions.
  136. class URHO3D_API EventHandler : public LinkedListNode
  137. {
  138. public:
  139. /// Construct with specified receiver.
  140. EventHandler(Object* receiver) :
  141. receiver_(receiver),
  142. sender_(0),
  143. userData_(0)
  144. {
  145. assert(receiver_);
  146. }
  147. /// Construct with specified receiver and userdata.
  148. EventHandler(Object* receiver, void* userData) :
  149. receiver_(receiver),
  150. sender_(0),
  151. userData_(userData)
  152. {
  153. assert(receiver_);
  154. }
  155. /// Destruct.
  156. virtual ~EventHandler() {}
  157. /// Set sender and event type.
  158. void SetSenderAndEventType(Object* sender, StringHash eventType)
  159. {
  160. sender_ = sender;
  161. eventType_ = eventType;
  162. }
  163. /// Invoke event handler function.
  164. virtual void Invoke(VariantMap& eventData) = 0;
  165. /// Return event receiver.
  166. Object* GetReceiver() const { return receiver_; }
  167. /// Return event sender. Null if the handler is non-specific.
  168. Object* GetSender() const { return sender_; }
  169. /// Return event type.
  170. const StringHash& GetEventType() const { return eventType_; }
  171. /// Return userdata.
  172. void* GetUserData() const { return userData_; }
  173. protected:
  174. /// Event receiver.
  175. Object* receiver_;
  176. /// Event sender.
  177. Object* sender_;
  178. /// Event type.
  179. StringHash eventType_;
  180. /// Userdata.
  181. void* userData_;
  182. };
  183. /// Template implementation of the event handler invoke helper (stores a function pointer of specific class.)
  184. template <class T> class EventHandlerImpl : public EventHandler
  185. {
  186. public:
  187. typedef void (T::*HandlerFunctionPtr)(StringHash, VariantMap&);
  188. /// Construct with receiver and function pointers.
  189. EventHandlerImpl(T* receiver, HandlerFunctionPtr function) :
  190. EventHandler(receiver),
  191. function_(function)
  192. {
  193. assert(function_);
  194. }
  195. /// Construct with receiver and function pointers and userdata.
  196. EventHandlerImpl(T* receiver, HandlerFunctionPtr function, void* userData) :
  197. EventHandler(receiver, userData),
  198. function_(function)
  199. {
  200. assert(function_);
  201. }
  202. /// Invoke event handler function.
  203. virtual void Invoke(VariantMap& eventData)
  204. {
  205. T* receiver = static_cast<T*>(receiver_);
  206. (receiver->*function_)(eventType_, eventData);
  207. }
  208. private:
  209. /// Class-specific pointer to handler function.
  210. HandlerFunctionPtr function_;
  211. };
  212. #define OBJECT(typeName) \
  213. public: \
  214. virtual Urho3D::ShortStringHash GetType() const { return GetTypeStatic(); } \
  215. virtual const Urho3D::String& GetTypeName() const { return GetTypeNameStatic(); } \
  216. static Urho3D::ShortStringHash GetTypeStatic() { static const Urho3D::ShortStringHash typeStatic(#typeName); return typeStatic; } \
  217. static const Urho3D::String& GetTypeNameStatic() { static const Urho3D::String typeNameStatic(#typeName); return typeNameStatic; } \
  218. #define EVENT(eventID, eventName) static const Urho3D::StringHash eventID(#eventName); namespace eventName
  219. #define PARAM(paramID, paramName) static const Urho3D::ShortStringHash paramID(#paramName)
  220. #define HANDLER(className, function) (new Urho3D::EventHandlerImpl<className>(this, &className::function))
  221. #define HANDLER_USERDATA(className, function, userData) (new Urho3D::EventHandlerImpl<className>(this, &className::function, userData))
  222. }