Context.h 7.7 KB

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