Context.h 7.6 KB

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