Context.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 "HashMap.h"
  27. #include "HashSet.h"
  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 String& 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 String& 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. /// Template version of returning a subsystem.
  74. template <class T> T* GetSubsystem() const;
  75. /// Return attribute descriptions for an object type, or null if none defined.
  76. const Vector<AttributeInfo>* GetAttributes(ShortStringHash type) const
  77. {
  78. HashMap<ShortStringHash, Vector<AttributeInfo> >::ConstIterator i = attributes_.Find(type);
  79. return i != attributes_.End() ? &i->second_ : 0;
  80. }
  81. /// Return network replication attribute descriptions for an object type, or null if none defined.
  82. const Vector<AttributeInfo>* GetNetworkAttributes(ShortStringHash type) const
  83. {
  84. HashMap<ShortStringHash, Vector<AttributeInfo> >::ConstIterator i = networkAttributes_.Find(type);
  85. return i != networkAttributes_.End() ? &i->second_ : 0;
  86. }
  87. /// Return event receivers for a sender and event type, or null if they do not exist.
  88. HashSet<Object*>* GetEventReceivers(Object* sender, StringHash eventType)
  89. {
  90. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > >::Iterator i = specificEventReceivers_.Find(sender);
  91. if (i != specificEventReceivers_.End())
  92. {
  93. HashMap<StringHash, HashSet<Object*> >::Iterator j = i->second_.Find(eventType);
  94. return j != i->second_.End() ? &j->second_ : 0;
  95. }
  96. else
  97. return 0;
  98. }
  99. /// Return event receivers for an event type, or null if they do not exist.
  100. HashSet<Object*>* GetEventReceivers(StringHash eventType)
  101. {
  102. HashMap<StringHash, HashSet<Object*> >::Iterator i = eventReceivers_.Find(eventType);
  103. return i != eventReceivers_.End() ? &i->second_ : 0;
  104. }
  105. private:
  106. /// Add event receiver.
  107. void AddEventReceiver(Object* receiver, StringHash eventType);
  108. /// Add event receiver for specific event.
  109. void AddEventReceiver(Object* receiver, Object* sender, StringHash eventType);
  110. /// Remove an event sender from all receivers. Called on its destruction.
  111. void RemoveEventSender(Object* sender);
  112. /// Remove event receiver from specific events.
  113. void RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType);
  114. /// Remove event receiver from non-specific events.
  115. void RemoveEventReceiver(Object* receiver, StringHash eventType);
  116. /// %Set current event handler. Called by Object.
  117. void SetEventHandler(EventHandler* handler) { eventHandler_ = handler; }
  118. /// Begin event send.
  119. void BeginSendEvent(Object* sender) { eventSenders_.Push(sender); }
  120. /// End event send. Clean up event receivers removed in the meanwhile.
  121. void EndSendEvent();
  122. /// Object factories.
  123. HashMap<ShortStringHash, SharedPtr<ObjectFactory> > factories_;
  124. /// Subsystems.
  125. HashMap<ShortStringHash, SharedPtr<Object> > subsystems_;
  126. /// Attribute descriptions per object type.
  127. HashMap<ShortStringHash, Vector<AttributeInfo> > attributes_;
  128. /// Network replication attribute descriptions per object type.
  129. HashMap<ShortStringHash, Vector<AttributeInfo> > networkAttributes_;
  130. /// Event receivers for non-specific events.
  131. HashMap<StringHash, HashSet<Object*> > eventReceivers_;
  132. /// Event receivers for specific senders' events.
  133. HashMap<Object*, HashMap<StringHash, HashSet<Object*> > > specificEventReceivers_;
  134. /// Event sender stack.
  135. PODVector<Object*> eventSenders_;
  136. /// Active event handler. Not stored in a stack for performance reasons; is needed only in esoteric cases.
  137. EventHandler* eventHandler_;
  138. };
  139. template <class T> void Context::RegisterFactory() { RegisterFactory(new ObjectFactoryImpl<T>(this)); }
  140. template <class T> void Context::RemoveSubsystem() { RemoveSubsystem(T::GetTypeStatic()); }
  141. template <class T> void Context::RegisterAttribute(const AttributeInfo& attr) { RegisterAttribute(T::GetTypeStatic(), attr); }
  142. template <class T> void Context::RemoveAttribute(const String& name) { RemoveAttribute(T::GetTypeStatic(), name); }
  143. template <class T, class U> void Context::CopyBaseAttributes() { CopyBaseAttributes(T::GetTypeStatic(), U::GetTypeStatic()); }
  144. template <class T> T* Context::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }