// // Copyright (c) 2008-2014 the Urho3D project. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #pragma once #include "Attribute.h" #include "Object.h" #include "HashSet.h" namespace Urho3D { /// Urho3D execution context. Provides access to subsystems, object factories and attributes, and event receivers. class URHO3D_API Context : public RefCounted { friend class Object; public: /// Construct. Context(); /// Destruct. ~Context(); /// Create an object by type hash. Return pointer to it or null if no factory found. SharedPtr CreateObject(StringHash objectType); /// Register a factory for an object type. void RegisterFactory(ObjectFactory* factory); /// Register a factory for an object type and specify the object category. void RegisterFactory(ObjectFactory* factory, const char* category); /// Register a subsystem. void RegisterSubsystem(Object* subsystem); /// Remove a subsystem. void RemoveSubsystem(StringHash objectType); /// Register object attribute. void RegisterAttribute(StringHash objectType, const AttributeInfo& attr); /// Remove object attribute. void RemoveAttribute(StringHash objectType, const char* name); /// Update object attribute's default value. void UpdateAttributeDefaultValue(StringHash objectType, const char* name, const Variant& defaultValue); /// Return a preallocated map for event data. Used for optimization to avoid constant re-allocation of event data maps. VariantMap& GetEventDataMap(); /// Copy base class attributes to derived class. void CopyBaseAttributes(StringHash baseType, StringHash derivedType); /// Template version of registering an object factory. template void RegisterFactory(); /// Template version of registering an object factory with category. template void RegisterFactory(const char* category); /// Template version of removing a subsystem. template void RemoveSubsystem(); /// Template version of registering an object attribute. template void RegisterAttribute(const AttributeInfo& attr); /// Template version of removing an object attribute. template void RemoveAttribute(const char* name); /// Template version of copying base class attributes to derived class. template void CopyBaseAttributes(); /// Template version of updating an object attribute's default value. template void UpdateAttributeDefaultValue(const char* name, const Variant& defaultValue); /// Return subsystem by type. Object* GetSubsystem(StringHash type) const; /// Return all subsystems. const HashMap >& GetSubsystems() const { return subsystems_; } /// Return all object factories. const HashMap >& GetObjectFactories() const { return factories_; } /// Return all object categories. const HashMap >& GetObjectCategories() const { return objectCategories_; } /// Return active event sender. Null outside event handling. Object* GetEventSender() const; /// Return active event handler. Set by Object. Null outside event handling. EventHandler* GetEventHandler() const { return eventHandler_; } /// Return object type name from hash, or empty if unknown. const String& GetTypeName(StringHash objectType) const; /// Return a specific attribute description for an object, or null if not found. AttributeInfo* GetAttribute(StringHash objectType, const char* name); /// Template version of returning a subsystem. template T* GetSubsystem() const; /// Template version of returning a specific attribute description. template AttributeInfo* GetAttribute(const char* name); /// Return attribute descriptions for an object type, or null if none defined. const Vector* GetAttributes(StringHash type) const { HashMap >::ConstIterator i = attributes_.Find(type); return i != attributes_.End() ? &i->second_ : 0; } /// Return network replication attribute descriptions for an object type, or null if none defined. const Vector* GetNetworkAttributes(StringHash type) const { HashMap >::ConstIterator i = networkAttributes_.Find(type); return i != networkAttributes_.End() ? &i->second_ : 0; } /// Return all registered attributes. const HashMap >& GetAllAttributes() const { return attributes_; } /// Return event receivers for a sender and event type, or null if they do not exist. HashSet* GetEventReceivers(Object* sender, StringHash eventType) { HashMap > >::Iterator i = specificEventReceivers_.Find(sender); if (i != specificEventReceivers_.End()) { HashMap >::Iterator j = i->second_.Find(eventType); return j != i->second_.End() ? &j->second_ : 0; } else return 0; } /// Return event receivers for an event type, or null if they do not exist. HashSet* GetEventReceivers(StringHash eventType) { HashMap >::Iterator i = eventReceivers_.Find(eventType); return i != eventReceivers_.End() ? &i->second_ : 0; } private: /// Add event receiver. void AddEventReceiver(Object* receiver, StringHash eventType); /// Add event receiver for specific event. void AddEventReceiver(Object* receiver, Object* sender, StringHash eventType); /// Remove an event sender from all receivers. Called on its destruction. void RemoveEventSender(Object* sender); /// Remove event receiver from specific events. void RemoveEventReceiver(Object* receiver, Object* sender, StringHash eventType); /// Remove event receiver from non-specific events. void RemoveEventReceiver(Object* receiver, StringHash eventType); /// Set current event handler. Called by Object. void SetEventHandler(EventHandler* handler) { eventHandler_ = handler; } /// Begin event send. void BeginSendEvent(Object* sender) { eventSenders_.Push(sender); } /// End event send. Clean up event receivers removed in the meanwhile. void EndSendEvent() { eventSenders_.Pop(); } /// Object factories. HashMap > factories_; /// Subsystems. HashMap > subsystems_; /// Attribute descriptions per object type. HashMap > attributes_; /// Network replication attribute descriptions per object type. HashMap > networkAttributes_; /// Event receivers for non-specific events. HashMap > eventReceivers_; /// Event receivers for specific senders' events. HashMap > > specificEventReceivers_; /// Event sender stack. PODVector eventSenders_; /// Event data stack. PODVector eventDataMaps_; /// Active event handler. Not stored in a stack for performance reasons; is needed only in esoteric cases. EventHandler* eventHandler_; /// Object categories. HashMap > objectCategories_; }; template void Context::RegisterFactory() { RegisterFactory(new ObjectFactoryImpl(this)); } template void Context::RegisterFactory(const char* category) { RegisterFactory(new ObjectFactoryImpl(this), category); } template void Context::RemoveSubsystem() { RemoveSubsystem(T::GetTypeStatic()); } template void Context::RegisterAttribute(const AttributeInfo& attr) { RegisterAttribute(T::GetTypeStatic(), attr); } template void Context::RemoveAttribute(const char* name) { RemoveAttribute(T::GetTypeStatic(), name); } template void Context::CopyBaseAttributes() { CopyBaseAttributes(T::GetTypeStatic(), U::GetTypeStatic()); } template T* Context::GetSubsystem() const { return static_cast(GetSubsystem(T::GetTypeStatic())); } template AttributeInfo* Context::GetAttribute(const char* name) { return GetAttribute(T::GetTypeStatic(), name); } template void Context::UpdateAttributeDefaultValue(const char* name, const Variant& defaultValue) { UpdateAttributeDefaultValue(T::GetTypeStatic(), name, defaultValue); } }