Context.h 8.2 KB

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