Context.h 9.7 KB

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