Object.h 13 KB

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