Context.h 9.4 KB

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