Context.h 11 KB

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