Object.h 16 KB

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