Context.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/HashSet.h"
  24. #include "../Core/Attribute.h"
  25. #include "../Core/Object.h"
  26. namespace Atomic
  27. {
  28. // ATOMIC BEGIN
  29. class GlobalEventListener
  30. {
  31. public:
  32. virtual void BeginSendEvent(Context* context, Object* sender, StringHash eventType, VariantMap& eventData) = 0;
  33. virtual void EndSendEvent(Context* context, Object* sender, StringHash eventType, VariantMap& eventData) = 0;
  34. };
  35. // ATOMIC END
  36. /// Urho3D execution context. Provides access to subsystems, object factories and attributes, and event receivers.
  37. class ATOMIC_API Context : public RefCounted
  38. {
  39. friend class Object;
  40. ATOMIC_REFCOUNTED(Context)
  41. public:
  42. /// Construct.
  43. Context();
  44. /// Destruct.
  45. ~Context();
  46. /// Create an object by type. Return pointer to it or null if no factory found.
  47. template <class T> inline SharedPtr<T> CreateObject()
  48. {
  49. return StaticCast<T>(CreateObject(T::GetTypeStatic()));
  50. }
  51. // ATOMIC BEGIN
  52. /// Create an object by type hash. Return pointer to it or null if no factory found.
  53. SharedPtr<Object> CreateObject(StringHash objectType, const XMLElement &source = XMLElement::EMPTY);
  54. // ATOMIC END
  55. /// Register a factory for an object type.
  56. void RegisterFactory(ObjectFactory* factory);
  57. /// Register a factory for an object type and specify the object category.
  58. void RegisterFactory(ObjectFactory* factory, const char* category);
  59. /// Register a subsystem.
  60. void RegisterSubsystem(Object* subsystem);
  61. /// Remove a subsystem.
  62. void RemoveSubsystem(StringHash objectType);
  63. /// Register object attribute.
  64. void RegisterAttribute(StringHash objectType, const AttributeInfo& attr);
  65. /// Remove object attribute.
  66. void RemoveAttribute(StringHash objectType, const char* name);
  67. /// Update object attribute's default value.
  68. void UpdateAttributeDefaultValue(StringHash objectType, const char* name, const Variant& defaultValue);
  69. /// Return a preallocated map for event data. Used for optimization to avoid constant re-allocation of event data maps.
  70. VariantMap& GetEventDataMap();
  71. /// Copy base class attributes to derived class.
  72. void CopyBaseAttributes(StringHash baseType, StringHash derivedType);
  73. /// Template version of registering an object factory.
  74. template <class T> void RegisterFactory();
  75. /// Template version of registering an object factory with category.
  76. template <class T> void RegisterFactory(const char* category);
  77. /// Template version of removing a subsystem.
  78. template <class T> void RemoveSubsystem();
  79. /// Template version of registering an object attribute.
  80. template <class T> void RegisterAttribute(const AttributeInfo& attr);
  81. /// Template version of removing an object attribute.
  82. template <class T> void RemoveAttribute(const char* name);
  83. /// Template version of copying base class attributes to derived class.
  84. template <class T, class U> void CopyBaseAttributes();
  85. /// Template version of updating an object attribute's default value.
  86. template <class T> void UpdateAttributeDefaultValue(const char* name, const Variant& defaultValue);
  87. /// Return subsystem by type.
  88. Object* GetSubsystem(StringHash type) const;
  89. /// Return global variable based on key
  90. const Variant& GetGlobalVar(StringHash key) const ;
  91. /// Return all global variables.
  92. const VariantMap& GetGlobalVars() const { return globalVars_; }
  93. /// Set global variable with the respective key and value
  94. void SetGlobalVar(StringHash key, const Variant& value);
  95. /// Return all subsystems.
  96. const HashMap<StringHash, SharedPtr<Object> >& GetSubsystems() const { return subsystems_; }
  97. /// Return all object factories.
  98. const HashMap<StringHash, SharedPtr<ObjectFactory> >& GetObjectFactories() const { return factories_; }
  99. /// Return all object categories.
  100. const HashMap<String, Vector<StringHash> >& GetObjectCategories() const { return objectCategories_; }
  101. /// Return active event sender. Null outside event handling.
  102. Object* GetEventSender() const;
  103. /// Return active event handler. Set by Object. Null outside event handling.
  104. EventHandler* GetEventHandler() const { return eventHandler_; }
  105. /// Return object type name from hash, or empty if unknown.
  106. const String& GetTypeName(StringHash objectType) const;
  107. /// Return a specific attribute description for an object, or null if not found.
  108. AttributeInfo* GetAttribute(StringHash objectType, const char* name);
  109. /// Template version of returning a subsystem.
  110. template <class T> T* GetSubsystem() const;
  111. /// Template version of returning a specific attribute description.
  112. template <class T> AttributeInfo* GetAttribute(const char* name);
  113. /// Return attribute descriptions for an object type, or null if none defined.
  114. const Vector<AttributeInfo>* GetAttributes(StringHash type) const
  115. {
  116. HashMap<StringHash, Vector<AttributeInfo> >::ConstIterator i = attributes_.Find(type);
  117. return i != attributes_.End() ? &i->second_ : 0;
  118. }
  119. /// Return network replication attribute descriptions for an object type, or null if none defined.
  120. const Vector<AttributeInfo>* GetNetworkAttributes(StringHash type) const
  121. {
  122. HashMap<StringHash, Vector<AttributeInfo> >::ConstIterator i = networkAttributes_.Find(type);
  123. return i != networkAttributes_.End() ? &i->second_ : 0;
  124. }
  125. /// Return all registered attributes.
  126. const HashMap<StringHash, Vector<AttributeInfo> >& GetAllAttributes() const { return attributes_; }
  127. /// Return event receivers for a sender and event type, or null if they do not exist.
  128. HashSet<Object*>* GetEventReceivers(Object* sender, StringHash eventType)
  129. {
  130. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > >::Iterator i = specificEventReceivers_.Find(sender);
  131. if (i != specificEventReceivers_.End())
  132. {
  133. HashMap<StringHash, HashSet<Object*> >::Iterator j = i->second_.Find(eventType);
  134. return j != i->second_.End() ? &j->second_ : 0;
  135. }
  136. else
  137. return 0;
  138. }
  139. /// Return event receivers for an event type, or null if they do not exist.
  140. HashSet<Object*>* GetEventReceivers(StringHash eventType)
  141. {
  142. HashMap<StringHash, HashSet<Object*> >::Iterator i = eventReceivers_.Find(eventType);
  143. return i != eventReceivers_.End() ? &i->second_ : 0;
  144. }
  145. // ATOMIC BEGIN
  146. /// Get whether an Editor Context
  147. bool GetEditorContext() { return editorContext_; }
  148. /// Get whether an Editor Context
  149. void SetEditorContext(bool editor) { editorContext_ = editor; }
  150. // hook for listening into events
  151. void AddGlobalEventListener(GlobalEventListener* listener) { globalEventListeners_.Push(listener); }
  152. void RemoveGlobalEventListener(GlobalEventListener* listener) { globalEventListeners_.Erase(globalEventListeners_.Find(listener)); }
  153. // ATOMIC END
  154. private:
  155. /// Add event receiver.
  156. void AddEventReceiver(Object* receiver, StringHash eventType);
  157. /// Add event receiver for specific event.
  158. void AddEventReceiver(Object* receiver, Object* sender, StringHash eventType);
  159. /// Remove an event sender from all receivers. Called on its destruction.
  160. void RemoveEventSender(Object* sender);
  161. /// Remove event receiver from specific events.
  162. void RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType);
  163. /// Remove event receiver from non-specific events.
  164. void RemoveEventReceiver(Object* receiver, StringHash eventType);
  165. /// Begin event send.
  166. void BeginSendEvent(Object* sender, StringHash eventType);
  167. /// End event send. Clean up event receivers removed in the meanwhile.
  168. void EndSendEvent();
  169. /// Set current event handler. Called by Object.
  170. void SetEventHandler(EventHandler* handler) { eventHandler_ = handler; }
  171. /// Object factories.
  172. HashMap<StringHash, SharedPtr<ObjectFactory> > factories_;
  173. /// Subsystems.
  174. HashMap<StringHash, SharedPtr<Object> > subsystems_;
  175. /// Attribute descriptions per object type.
  176. HashMap<StringHash, Vector<AttributeInfo> > attributes_;
  177. /// Network replication attribute descriptions per object type.
  178. HashMap<StringHash, Vector<AttributeInfo> > networkAttributes_;
  179. /// Event receivers for non-specific events.
  180. HashMap<StringHash, HashSet<Object*> > eventReceivers_;
  181. /// Event receivers for specific senders' events.
  182. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > > specificEventReceivers_;
  183. /// Event sender stack.
  184. PODVector<Object*> eventSenders_;
  185. /// Event data stack.
  186. PODVector<VariantMap*> eventDataMaps_;
  187. /// Active event handler. Not stored in a stack for performance reasons; is needed only in esoteric cases.
  188. EventHandler* eventHandler_;
  189. /// Object categories.
  190. HashMap<String, Vector<StringHash> > objectCategories_;
  191. /// Variant map for global variables that can persist throughout application execution.
  192. VariantMap globalVars_;
  193. // ATOMIC BEGIN
  194. /// Begin event send.
  195. void GlobalBeginSendEvent(Object* sender, StringHash eventType, VariantMap& eventData) {
  196. for (unsigned i = 0; i < globalEventListeners_.Size(); i++)
  197. globalEventListeners_[i]->BeginSendEvent(this, sender, eventType, eventData);
  198. eventSenders_.Push(sender);
  199. }
  200. /// End event send. Clean up event receivers removed in the meanwhile.
  201. void GlobalEndSendEvent(Object* sender, StringHash eventType, VariantMap& eventData) {
  202. for (unsigned i = 0; i < globalEventListeners_.Size(); i++)
  203. globalEventListeners_[i]->EndSendEvent(this, sender, eventType, eventData);
  204. eventSenders_.Pop();
  205. }
  206. PODVector<GlobalEventListener*> globalEventListeners_;
  207. bool editorContext_;
  208. // ATOMIC END
  209. };
  210. template <class T> void Context::RegisterFactory() { RegisterFactory(new ObjectFactoryImpl<T>(this)); }
  211. template <class T> void Context::RegisterFactory(const char* category)
  212. {
  213. RegisterFactory(new ObjectFactoryImpl<T>(this), category);
  214. }
  215. template <class T> void Context::RemoveSubsystem() { RemoveSubsystem(T::GetTypeStatic()); }
  216. template <class T> void Context::RegisterAttribute(const AttributeInfo& attr) { RegisterAttribute(T::GetTypeStatic(), attr); }
  217. template <class T> void Context::RemoveAttribute(const char* name) { RemoveAttribute(T::GetTypeStatic(), name); }
  218. template <class T, class U> void Context::CopyBaseAttributes() { CopyBaseAttributes(T::GetTypeStatic(), U::GetTypeStatic()); }
  219. template <class T> T* Context::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
  220. template <class T> AttributeInfo* Context::GetAttribute(const char* name) { return GetAttribute(T::GetTypeStatic(), name); }
  221. template <class T> void Context::UpdateAttributeDefaultValue(const char* name, const Variant& defaultValue)
  222. {
  223. UpdateAttributeDefaultValue(T::GetTypeStatic(), name, defaultValue);
  224. }
  225. }