Object.h 15 KB

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