Context.h 11 KB

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