Object.h 14 KB

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