Object.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. // ATOMIC END
  194. protected:
  195. /// Execution context.
  196. Context* context_;
  197. private:
  198. /// Find the first event handler with no specific sender.
  199. EventHandler* FindEventHandler(StringHash eventType, EventHandler** previous = 0) const;
  200. /// Find the first event handler with specific sender.
  201. EventHandler* FindSpecificEventHandler(Object* sender, EventHandler** previous = 0) const;
  202. /// Find the first event handler with specific sender and event type.
  203. EventHandler* FindSpecificEventHandler(Object* sender, StringHash eventType, EventHandler** previous = 0) const;
  204. /// Remove event handlers related to a specific sender.
  205. void RemoveEventSender(Object* sender);
  206. /// Event handlers. Sender is null for non-specific handlers.
  207. LinkedList<EventHandler> eventHandlers_;
  208. };
  209. template <class T> T* Object::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
  210. /// Base class for object factories.
  211. class ATOMIC_API ObjectFactory : public RefCounted
  212. {
  213. ATOMIC_REFCOUNTED(ObjectFactory)
  214. public:
  215. /// Construct.
  216. ObjectFactory(Context* context) :
  217. context_(context)
  218. {
  219. assert(context_);
  220. }
  221. // ATOMIC BEGIN
  222. /// Create an object. Implemented in templated subclasses.
  223. virtual SharedPtr<Object> CreateObject(const XMLElement& source = XMLElement::EMPTY) = 0;
  224. // ATOMIC END
  225. /// Return execution context.
  226. Context* GetContext() const { return context_; }
  227. /// Return type info of objects created by this factory.
  228. const TypeInfo* GetTypeInfo() const { return typeInfo_; }
  229. /// Return type hash of objects created by this factory.
  230. StringHash GetType() const { return typeInfo_->GetType(); }
  231. // ATOMIC BEGIN
  232. // RefCounted in Atomic carry typename with GetTypeName, so renamed to GetFactoryType
  233. /// Return type name of objects created by this factory.
  234. const String& GetFactoryTypeName() const { return typeInfo_->GetTypeName(); }
  235. // ATOMIC END
  236. protected:
  237. /// Execution context.
  238. Context* context_;
  239. /// Type info.
  240. const TypeInfo* typeInfo_;
  241. };
  242. /// Template implementation of the object factory.
  243. template <class T> class ObjectFactoryImpl : public ObjectFactory
  244. {
  245. public:
  246. /// Construct.
  247. ObjectFactoryImpl(Context* context) :
  248. ObjectFactory(context)
  249. {
  250. typeInfo_ = T::GetTypeInfoStatic();
  251. }
  252. // ATOMIC BEGIN
  253. /// Create an object of the specific type.
  254. virtual SharedPtr<Object> CreateObject(const XMLElement& source = XMLElement::EMPTY) { return SharedPtr<Object>(new T(context_)); }
  255. // ATOMIC END
  256. };
  257. /// Internal helper class for invoking event handler functions.
  258. class ATOMIC_API EventHandler : public LinkedListNode
  259. {
  260. public:
  261. /// Construct with specified receiver and userdata.
  262. EventHandler(Object* receiver, void* userData = 0) :
  263. receiver_(receiver),
  264. sender_(0),
  265. userData_(userData)
  266. {
  267. }
  268. /// Destruct.
  269. virtual ~EventHandler() { }
  270. /// Set sender and event type.
  271. void SetSenderAndEventType(Object* sender, StringHash eventType)
  272. {
  273. sender_ = sender;
  274. eventType_ = eventType;
  275. }
  276. /// Invoke event handler function.
  277. virtual void Invoke(VariantMap& eventData) = 0;
  278. /// Return a unique copy of the event handler.
  279. virtual EventHandler* Clone() const = 0;
  280. /// Return event receiver.
  281. Object* GetReceiver() const { return receiver_; }
  282. /// Return event sender. Null if the handler is non-specific.
  283. Object* GetSender() const { return sender_; }
  284. /// Return event type.
  285. const StringHash& GetEventType() const { return eventType_; }
  286. /// Return userdata.
  287. void* GetUserData() const { return userData_; }
  288. protected:
  289. /// Event receiver.
  290. Object* receiver_;
  291. /// Event sender.
  292. Object* sender_;
  293. /// Event type.
  294. StringHash eventType_;
  295. /// Userdata.
  296. void* userData_;
  297. };
  298. /// Template implementation of the event handler invoke helper (stores a function pointer of specific class.)
  299. template <class T> class EventHandlerImpl : public EventHandler
  300. {
  301. public:
  302. typedef void (T::*HandlerFunctionPtr)(StringHash, VariantMap&);
  303. /// Construct with receiver and function pointers and userdata.
  304. EventHandlerImpl(T* receiver, HandlerFunctionPtr function, void* userData = 0) :
  305. EventHandler(receiver, userData),
  306. function_(function)
  307. {
  308. assert(receiver_);
  309. assert(function_);
  310. }
  311. /// Invoke event handler function.
  312. virtual void Invoke(VariantMap& eventData)
  313. {
  314. T* receiver = static_cast<T*>(receiver_);
  315. (receiver->*function_)(eventType_, eventData);
  316. }
  317. /// Return a unique copy of the event handler.
  318. virtual EventHandler* Clone() const
  319. {
  320. return new EventHandlerImpl(static_cast<T*>(receiver_), function_, userData_);
  321. }
  322. private:
  323. /// Class-specific pointer to handler function.
  324. HandlerFunctionPtr function_;
  325. };
  326. #if ATOMIC_CXX11
  327. /// Template implementation of the event handler invoke helper (std::function instance).
  328. class EventHandler11Impl : public EventHandler
  329. {
  330. public:
  331. /// Construct with receiver and function pointers and userdata.
  332. EventHandler11Impl(std::function<void(StringHash, VariantMap&)> function, void* userData = 0) :
  333. EventHandler(0, userData),
  334. function_(function)
  335. {
  336. assert(function_);
  337. }
  338. /// Invoke event handler function.
  339. virtual void Invoke(VariantMap& eventData)
  340. {
  341. function_(eventType_, eventData);
  342. }
  343. /// Return a unique copy of the event handler.
  344. virtual EventHandler* Clone() const
  345. {
  346. return new EventHandler11Impl(function_, userData_);
  347. }
  348. private:
  349. /// Class-specific pointer to handler function.
  350. std::function<void(StringHash, VariantMap&)> function_;
  351. };
  352. #endif
  353. /// Register event names.
  354. struct ATOMIC_API EventNameRegistrar
  355. {
  356. /// Register an event name for hash reverse mapping.
  357. static StringHash RegisterEventName(const char* eventName);
  358. /// Return Event name or empty string if not found.
  359. static const String& GetEventName(StringHash eventID);
  360. /// Return Event name map.
  361. static HashMap<StringHash, String>& GetEventNameMap();
  362. };
  363. /// Describe an event's hash ID and begin a namespace in which to define its parameters.
  364. #define ATOMIC_EVENT(eventID, eventName) static const Atomic::StringHash eventID(Atomic::EventNameRegistrar::RegisterEventName(#eventName)); namespace eventName
  365. /// Describe an event's parameter hash ID. Should be used inside an event namespace.
  366. #define ATOMIC_PARAM(paramID, paramName) static const Atomic::StringHash paramID(#paramName)
  367. /// Convenience macro to construct an EventHandler that points to a receiver object and its member function.
  368. #define ATOMIC_HANDLER(className, function) (new Atomic::EventHandlerImpl<className>(this, &className::function))
  369. /// Convenience macro to construct an EventHandler that points to a receiver object and its member function, and also defines a userdata pointer.
  370. #define ATOMIC_HANDLER_USERDATA(className, function, userData) (new Atomic::EventHandlerImpl<className>(this, &className::function, userData))
  371. // ATOMIC BEGIN
  372. // Explicit template specializations for most commonly used engine subsystems. They sidestep HashMap lookup and return
  373. // subsystem pointer cached in Context object.
  374. template <> Engine* Object::GetSubsystem<Engine>() const;
  375. template <> Time* Object::GetSubsystem<Time>() const;
  376. template <> WorkQueue* Object::GetSubsystem<WorkQueue>() const;
  377. template <> Profiler* Object::GetSubsystem<Profiler>() const;
  378. template <> FileSystem* Object::GetSubsystem<FileSystem>() const;
  379. template <> Log* Object::GetSubsystem<Log>() const;
  380. template <> ResourceCache* Object::GetSubsystem<ResourceCache>() const;
  381. template <> Localization* Object::GetSubsystem<Localization>() const;
  382. template <> Network* Object::GetSubsystem<Network>() const;
  383. template <> Web* Object::GetSubsystem<Web>() const;
  384. template <> Database* Object::GetSubsystem<Database>() const;
  385. template <> Input* Object::GetSubsystem<Input>() const;
  386. template <> Audio* Object::GetSubsystem<Audio>() const;
  387. template <> UI* Object::GetSubsystem<UI>() const;
  388. template <> SystemUI* Object::GetSubsystem<SystemUI>() const;
  389. template <> Graphics* Object::GetSubsystem<Graphics>() const;
  390. template <> Renderer* Object::GetSubsystem<Renderer>() const;
  391. template <> Console* Object::GetSubsystem<Console>() const;
  392. template <> DebugHud* Object::GetSubsystem<DebugHud>() const;
  393. template <> Metrics* Object::GetSubsystem<Metrics>() const;
  394. // ATOMIC END
  395. }