Object.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "../Container/LinkedList.h"
  24. #include "../Core/Variant.h"
  25. namespace Atomic
  26. {
  27. class Context;
  28. class EventHandler;
  29. #define OBJECT(typeName) \
  30. public: \
  31. typedef typeName ClassName; \
  32. virtual Atomic::StringHash GetType() const { return GetTypeStatic(); } \
  33. virtual Atomic::StringHash GetBaseType() const { return GetBaseTypeStatic(); } \
  34. virtual const Atomic::String& GetTypeName() const { return GetTypeNameStatic(); } \
  35. static Atomic::StringHash GetTypeStatic() { static const Atomic::StringHash typeStatic(#typeName); return typeStatic; } \
  36. static const Atomic::String& GetTypeNameStatic() { static const Atomic::String typeNameStatic(#typeName); return typeNameStatic; } \
  37. #define BASEOBJECT(typeName) \
  38. public: \
  39. static Atomic::StringHash GetBaseTypeStatic() { static const Atomic::StringHash baseTypeStatic(#typeName); return baseTypeStatic; } \
  40. /// Base class for objects with type identification, subsystem access and event sending/receiving capability.
  41. class ATOMIC_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 Atomic::StringHash GetType() const = 0;
  52. /// Return base class type hash.
  53. virtual Atomic::StringHash GetBaseType() const = 0;
  54. /// Return type name.
  55. virtual const Atomic::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 a preallocated map for event data. Used for optimization to avoid constant re-allocation of event data maps.
  77. VariantMap& GetEventDataMap() const;
  78. /// Return execution context.
  79. Context* GetContext() const { return context_; }
  80. /// Return subsystem by type.
  81. Object* GetSubsystem(StringHash type) const;
  82. /// Return active event sender. Null outside event handling.
  83. Object* GetEventSender() const;
  84. /// Return active event handler. Null outside event handling.
  85. EventHandler* GetEventHandler() const;
  86. /// Return whether has subscribed to an event without specific sender.
  87. bool HasSubscribedToEvent(StringHash eventType) const;
  88. /// Return whether has subscribed to a specific sender's event.
  89. bool HasSubscribedToEvent(Object* sender, StringHash eventType) const;
  90. /// Return whether has subscribed to any event.
  91. bool HasEventHandlers() const { return !eventHandlers_.Empty(); }
  92. /// Template version of returning a subsystem.
  93. template <class T> T* GetSubsystem() const;
  94. /// Return object category. Categories are (optionally) registered along with the object factory. Return an empty string if the object category is not registered.
  95. const String& GetCategory() const;
  96. bool IsObject() const { return true; }
  97. protected:
  98. /// Execution context.
  99. Context* context_;
  100. private:
  101. /// Find the first event handler with no specific sender.
  102. EventHandler* FindEventHandler(StringHash eventType, EventHandler** previous = 0) const;
  103. /// Find the first event handler with specific sender.
  104. EventHandler* FindSpecificEventHandler(Object* sender, EventHandler** previous = 0) const;
  105. /// Find the first event handler with specific sender and event type.
  106. EventHandler* FindSpecificEventHandler(Object* sender, StringHash eventType, EventHandler** previous = 0) const;
  107. /// Remove event handlers related to a specific sender.
  108. void RemoveEventSender(Object* sender);
  109. /// Event handlers. Sender is null for non-specific handlers.
  110. LinkedList<EventHandler> eventHandlers_;
  111. };
  112. template <class T> T* Object::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
  113. /// Base class for object factories.
  114. class ATOMIC_API ObjectFactory : public RefCounted
  115. {
  116. public:
  117. /// Construct.
  118. ObjectFactory(Context* context) :
  119. context_(context)
  120. {
  121. assert(context_);
  122. }
  123. /// Create an object. Implemented in templated subclasses.
  124. virtual SharedPtr<Object> CreateObject() = 0;
  125. /// Return execution context.
  126. Context* GetContext() const { return context_; }
  127. /// Return type hash of objects created by this factory.
  128. StringHash GetType() const { return type_; }
  129. /// Return base type hash of objects created by this factory.
  130. StringHash GetBaseType() const { return baseType_; }
  131. /// Return type name of objects created by this factory.
  132. const String& GetTypeName() const { return typeName_; }
  133. protected:
  134. /// Execution context.
  135. Context* context_;
  136. /// Object type.
  137. StringHash type_;
  138. /// Object base type.
  139. StringHash baseType_;
  140. /// Object type name.
  141. String typeName_;
  142. };
  143. /// Template implementation of the object factory.
  144. template <class T> class ObjectFactoryImpl : public ObjectFactory
  145. {
  146. public:
  147. /// Construct.
  148. ObjectFactoryImpl(Context* context) :
  149. ObjectFactory(context)
  150. {
  151. type_ = T::GetTypeStatic();
  152. baseType_ = T::GetBaseTypeStatic();
  153. typeName_ = T::GetTypeNameStatic();
  154. }
  155. /// Create an object of the specific type.
  156. virtual SharedPtr<Object> CreateObject() { return SharedPtr<Object>(new T(context_)); }
  157. };
  158. /// Internal helper class for invoking event handler functions.
  159. class ATOMIC_API EventHandler : public LinkedListNode
  160. {
  161. public:
  162. /// Construct with specified receiver.
  163. EventHandler(Object* receiver) :
  164. receiver_(receiver),
  165. sender_(0),
  166. userData_(0)
  167. {
  168. assert(receiver_);
  169. }
  170. /// Construct with specified receiver and userdata.
  171. EventHandler(Object* receiver, void* userData) :
  172. receiver_(receiver),
  173. sender_(0),
  174. userData_(userData)
  175. {
  176. assert(receiver_);
  177. }
  178. /// Destruct.
  179. virtual ~EventHandler() {}
  180. /// Set sender and event type.
  181. void SetSenderAndEventType(Object* sender, StringHash eventType)
  182. {
  183. sender_ = sender;
  184. eventType_ = eventType;
  185. }
  186. /// Invoke event handler function.
  187. virtual void Invoke(VariantMap& eventData) = 0;
  188. /// Return a unique copy of the event handler.
  189. virtual EventHandler* Clone() const = 0;
  190. /// Return event receiver.
  191. Object* GetReceiver() const { return receiver_; }
  192. /// Return event sender. Null if the handler is non-specific.
  193. Object* GetSender() const { return sender_; }
  194. /// Return event type.
  195. const StringHash& GetEventType() const { return eventType_; }
  196. /// Return userdata.
  197. void* GetUserData() const { return userData_; }
  198. protected:
  199. /// Event receiver.
  200. Object* receiver_;
  201. /// Event sender.
  202. Object* sender_;
  203. /// Event type.
  204. StringHash eventType_;
  205. /// Userdata.
  206. void* userData_;
  207. };
  208. /// Template implementation of the event handler invoke helper (stores a function pointer of specific class.)
  209. template <class T> class EventHandlerImpl : public EventHandler
  210. {
  211. public:
  212. typedef void (T::*HandlerFunctionPtr)(StringHash, VariantMap&);
  213. /// Construct with receiver and function pointers.
  214. EventHandlerImpl(T* receiver, HandlerFunctionPtr function) :
  215. EventHandler(receiver),
  216. function_(function)
  217. {
  218. assert(function_);
  219. }
  220. /// Construct with receiver and function pointers and userdata.
  221. EventHandlerImpl(T* receiver, HandlerFunctionPtr function, void* userData) :
  222. EventHandler(receiver, userData),
  223. function_(function)
  224. {
  225. assert(function_);
  226. }
  227. /// Invoke event handler function.
  228. virtual void Invoke(VariantMap& eventData)
  229. {
  230. T* receiver = static_cast<T*>(receiver_);
  231. (receiver->*function_)(eventType_, eventData);
  232. }
  233. /// Return a unique copy of the event handler.
  234. virtual EventHandler* Clone() const
  235. {
  236. return new EventHandlerImpl(static_cast<T*>(receiver_), function_, userData_);
  237. }
  238. private:
  239. /// Class-specific pointer to handler function.
  240. HandlerFunctionPtr function_;
  241. };
  242. /// Describe an event's hash ID and begin a namespace in which to define its parameters.
  243. #define EVENT(eventID, eventName) static const Atomic::StringHash eventID(#eventName); namespace eventName
  244. /// Describe an event's parameter hash ID. Should be used inside an event namespace.
  245. #define PARAM(paramID, paramName) static const Atomic::StringHash paramID(#paramName)
  246. /// Convenience macro to construct an EventHandler that points to a receiver object and its member function.
  247. #define HANDLER(className, function) (new Atomic::EventHandlerImpl<className>(this, &className::function))
  248. /// Convenience macro to construct an EventHandler that points to a receiver object and its member function, and also defines a userdata pointer.
  249. #define HANDLER_USERDATA(className, function, userData) (new Atomic::EventHandlerImpl<className>(this, &className::function, userData))
  250. }