Context.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Attribute.h"
  25. #include "Object.h"
  26. #include "HashSet.h"
  27. namespace Urho3D
  28. {
  29. /// Urho3D execution context. Provides access to subsystems, object factories and attributes, and event receivers.
  30. class Context : public RefCounted
  31. {
  32. friend class Object;
  33. public:
  34. /// Construct.
  35. Context();
  36. /// Destruct.
  37. ~Context();
  38. /// Create an object by type hash. Return pointer to it or null if no factory found.
  39. SharedPtr<Object> CreateObject(ShortStringHash objectType);
  40. /// Register a factory for an object type.
  41. void RegisterFactory(ObjectFactory* factory);
  42. /// Register a subsystem.
  43. void RegisterSubsystem(Object* subsystem);
  44. /// Remove a subsystem.
  45. void RemoveSubsystem(ShortStringHash objectType);
  46. /// Register object attribute.
  47. void RegisterAttribute(ShortStringHash objectType, const AttributeInfo& attr);
  48. /// Remove object attribute.
  49. void RemoveAttribute(ShortStringHash objectType, const char* name);
  50. /// Copy base class attributes to derived class.
  51. void CopyBaseAttributes(ShortStringHash baseType, ShortStringHash derivedType);
  52. /// Template version of registering an object factory.
  53. template <class T> void RegisterFactory();
  54. /// Template version of removing a subsystem.
  55. template <class T> void RemoveSubsystem();
  56. /// Template version of registering an object attribute.
  57. template <class T> void RegisterAttribute(const AttributeInfo& attr);
  58. /// Template version of removing an object attribute.
  59. template <class T> void RemoveAttribute(const char* name);
  60. /// Template version of copying base class attributes to derived class.
  61. template <class T, class U> void CopyBaseAttributes();
  62. /// Return subsystem by type.
  63. Object* GetSubsystem(ShortStringHash type) const;
  64. /// Return all subsystems.
  65. const HashMap<ShortStringHash, SharedPtr<Object> >& GetSubsystems() const { return subsystems_; }
  66. /// Return all object factories.
  67. const HashMap<ShortStringHash, SharedPtr<ObjectFactory> >& GetObjectFactories() const { return factories_; }
  68. /// Return active event sender. Null outside event handling.
  69. Object* GetEventSender() const;
  70. /// Return active event handler. Set by Object. Null outside event handling.
  71. EventHandler* GetEventHandler() const { return eventHandler_; }
  72. /// Return object type name from hash, or empty if unknown.
  73. const String& GetTypeName(ShortStringHash type) const;
  74. /// Template version of returning a subsystem.
  75. template <class T> T* GetSubsystem() const;
  76. /// Return attribute descriptions for an object type, or null if none defined.
  77. const Vector<AttributeInfo>* GetAttributes(ShortStringHash type) const
  78. {
  79. HashMap<ShortStringHash, Vector<AttributeInfo> >::ConstIterator i = attributes_.Find(type);
  80. return i != attributes_.End() ? &i->second_ : 0;
  81. }
  82. /// Return network replication attribute descriptions for an object type, or null if none defined.
  83. const Vector<AttributeInfo>* GetNetworkAttributes(ShortStringHash type) const
  84. {
  85. HashMap<ShortStringHash, Vector<AttributeInfo> >::ConstIterator i = networkAttributes_.Find(type);
  86. return i != networkAttributes_.End() ? &i->second_ : 0;
  87. }
  88. /// Return event receivers for a sender and event type, or null if they do not exist.
  89. HashSet<Object*>* GetEventReceivers(Object* sender, StringHash eventType)
  90. {
  91. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > >::Iterator i = specificEventReceivers_.Find(sender);
  92. if (i != specificEventReceivers_.End())
  93. {
  94. HashMap<StringHash, HashSet<Object*> >::Iterator j = i->second_.Find(eventType);
  95. return j != i->second_.End() ? &j->second_ : 0;
  96. }
  97. else
  98. return 0;
  99. }
  100. /// Return event receivers for an event type, or null if they do not exist.
  101. HashSet<Object*>* GetEventReceivers(StringHash eventType)
  102. {
  103. HashMap<StringHash, HashSet<Object*> >::Iterator i = eventReceivers_.Find(eventType);
  104. return i != eventReceivers_.End() ? &i->second_ : 0;
  105. }
  106. private:
  107. /// Add event receiver.
  108. void AddEventReceiver(Object* receiver, StringHash eventType);
  109. /// Add event receiver for specific event.
  110. void AddEventReceiver(Object* receiver, Object* sender, StringHash eventType);
  111. /// Remove an event sender from all receivers. Called on its destruction.
  112. void RemoveEventSender(Object* sender);
  113. /// Remove event receiver from specific events.
  114. void RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType);
  115. /// Remove event receiver from non-specific events.
  116. void RemoveEventReceiver(Object* receiver, StringHash eventType);
  117. /// Set current event handler. Called by Object.
  118. void SetEventHandler(EventHandler* handler) { eventHandler_ = handler; }
  119. /// Begin event send.
  120. void BeginSendEvent(Object* sender) { eventSenders_.Push(sender); }
  121. /// End event send. Clean up event receivers removed in the meanwhile.
  122. void EndSendEvent();
  123. /// Object factories.
  124. HashMap<ShortStringHash, SharedPtr<ObjectFactory> > factories_;
  125. /// Subsystems.
  126. HashMap<ShortStringHash, SharedPtr<Object> > subsystems_;
  127. /// Attribute descriptions per object type.
  128. HashMap<ShortStringHash, Vector<AttributeInfo> > attributes_;
  129. /// Network replication attribute descriptions per object type.
  130. HashMap<ShortStringHash, Vector<AttributeInfo> > networkAttributes_;
  131. /// Event receivers for non-specific events.
  132. HashMap<StringHash, HashSet<Object*> > eventReceivers_;
  133. /// Event receivers for specific senders' events.
  134. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > > specificEventReceivers_;
  135. /// Event sender stack.
  136. PODVector<Object*> eventSenders_;
  137. /// Active event handler. Not stored in a stack for performance reasons; is needed only in esoteric cases.
  138. EventHandler* eventHandler_;
  139. };
  140. template <class T> void Context::RegisterFactory() { RegisterFactory(new ObjectFactoryImpl<T>(this)); }
  141. template <class T> void Context::RemoveSubsystem() { RemoveSubsystem(T::GetTypeStatic()); }
  142. template <class T> void Context::RegisterAttribute(const AttributeInfo& attr) { RegisterAttribute(T::GetTypeStatic(), attr); }
  143. template <class T> void Context::RemoveAttribute(const char* name) { RemoveAttribute(T::GetTypeStatic(), name); }
  144. template <class T, class U> void Context::CopyBaseAttributes() { CopyBaseAttributes(T::GetTypeStatic(), U::GetTypeStatic()); }
  145. template <class T> T* Context::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
  146. }