Object.h 18 KB

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