Browse Source

Testing static registration of subsystems

dont know if its ok or really bad ...
svifylabs 9 years ago
parent
commit
2a0a9df4dd

+ 32 - 1
Source/Urho3D/Core/Context.h

@@ -25,6 +25,7 @@
 #include "../Core/Attribute.h"
 #include "../Core/Attribute.h"
 #include "../Core/Object.h"
 #include "../Core/Object.h"
 #include "../Container/HashSet.h"
 #include "../Container/HashSet.h"
+#include "../Container/Ptr.h"
 
 
 namespace Urho3D
 namespace Urho3D
 {
 {
@@ -70,6 +71,8 @@ public:
     template <class T> void RegisterFactory();
     template <class T> void RegisterFactory();
     /// Template version of registering an object factory with category.
     /// Template version of registering an object factory with category.
     template <class T> void RegisterFactory(const char* category);
     template <class T> void RegisterFactory(const char* category);
+    /// Template version of registering a subsystem.
+    template <class T> void RegisterSubsystemStatic();
     /// Template version of removing a subsystem.
     /// Template version of removing a subsystem.
     template <class T> void RemoveSubsystem();
     template <class T> void RemoveSubsystem();
     /// Template version of registering an object attribute.
     /// Template version of registering an object attribute.
@@ -114,6 +117,8 @@ public:
     AttributeInfo* GetAttribute(StringHash objectType, const char* name);
     AttributeInfo* GetAttribute(StringHash objectType, const char* name);
     /// Template version of returning a subsystem.
     /// Template version of returning a subsystem.
     template <class T> T* GetSubsystem() const;
     template <class T> T* GetSubsystem() const;
+    /// Template version of returning a subsystem that saved the Subsystem in a static variable for fast access.
+    template <class T> T* GetSubsystemStatic();
     /// Template version of returning a specific attribute description.
     /// Template version of returning a specific attribute description.
     template <class T> AttributeInfo* GetAttribute(const char* name);
     template <class T> AttributeInfo* GetAttribute(const char* name);
 
 
@@ -155,6 +160,8 @@ public:
     }
     }
 
 
 private:
 private:
+    /// Template version of caching the Subsystem in a static variable for fast access.
+    template <class T> WeakPtr<T>& CacheSubsystem();
     /// Add event receiver.
     /// Add event receiver.
     void AddEventReceiver(Object* receiver, StringHash eventType);
     void AddEventReceiver(Object* receiver, StringHash eventType);
     /// Add event receiver for specific event.
     /// Add event receiver for specific event.
@@ -206,7 +213,7 @@ template <class T> void Context::RegisterFactory(const char* category)
     RegisterFactory(new ObjectFactoryImpl<T>(this), category);
     RegisterFactory(new ObjectFactoryImpl<T>(this), category);
 }
 }
 
 
-template <class T> void Context::RemoveSubsystem() { RemoveSubsystem(T::GetTypeStatic()); }
+template <class T> void Context::RemoveSubsystem() { RemoveSubsystem(T::GetTypeStatic());  CacheSubsystem<T>().Reset(); }
 
 
 template <class T> void Context::RegisterAttribute(const AttributeInfo& attr) { RegisterAttribute(T::GetTypeStatic(), attr); }
 template <class T> void Context::RegisterAttribute(const AttributeInfo& attr) { RegisterAttribute(T::GetTypeStatic(), attr); }
 
 
@@ -216,8 +223,32 @@ template <class T, class U> void Context::CopyBaseAttributes() { CopyBaseAttribu
 
 
 template <class T> T* Context::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
 template <class T> T* Context::GetSubsystem() const { return static_cast<T*>(GetSubsystem(T::GetTypeStatic())); }
 
 
+template <class T> T* Context::GetSubsystemStatic() { return CacheSubsystem<T>().Get(); }
+
 template <class T> AttributeInfo* Context::GetAttribute(const char* name) { return GetAttribute(T::GetTypeStatic(), name); }
 template <class T> AttributeInfo* Context::GetAttribute(const char* name) { return GetAttribute(T::GetTypeStatic(), name); }
 
 
+template <class T> void Context::RegisterSubsystemStatic()
+{
+// check if already created 
+    if (CacheSubsystem<T>().Null())
+    {
+        // check if already registered
+        HashMap<StringHash, SharedPtr<Object> >::Iterator it = subsystems_.Find(T::GetTypeStatic());
+        if (it != subsystems_.End())
+        {
+            CacheSubsystem<T>() =static_cast<T*>( it->second_.Get());
+        }
+        else
+        {
+            T* subSystem = new T(this);
+            RegisterSubsystem(subSystem);
+            CacheSubsystem<T>() = subSystem;
+        }
+    }
+}
+
+template <class T> WeakPtr<T>& Context::CacheSubsystem() { static WeakPtr<T> cachedSubsystem(NULL); return cachedSubsystem; }
+
 template <class T> void Context::UpdateAttributeDefaultValue(const char* name, const Variant& defaultValue)
 template <class T> void Context::UpdateAttributeDefaultValue(const char* name, const Variant& defaultValue)
 {
 {
     UpdateAttributeDefaultValue(T::GetTypeStatic(), name, defaultValue);
     UpdateAttributeDefaultValue(T::GetTypeStatic(), name, defaultValue);

+ 1 - 21
Source/Urho3D/Core/EventProfiler.cpp

@@ -37,7 +37,6 @@ static const int NAME_MAX_LENGTH = 30;
 
 
 EventProfiler::EventProfiler(Context* context) : 
 EventProfiler::EventProfiler(Context* context) : 
     Object(context),
     Object(context),
-    active_(false),
     current_(0),
     current_(0),
     root_(0),
     root_(0),
     intervalFrames_(0),
     intervalFrames_(0),
@@ -54,25 +53,9 @@ EventProfiler::~EventProfiler()
     root_ = 0;
     root_ = 0;
 }
 }
 
 
-void EventProfiler::SetActive(bool active)
-{
-    if (!active && active_)
-    {
-        /// deleting all blocks and creating new root block.
-        delete root_;
-        root_ = 0;
-        root_ = new EventProfilerBlock(0, "Root");
-        current_ = root_;
-        current_->name_ = "Root";
-    }
-    
-    active_ = active;
-}
 
 
 void EventProfiler::BeginFrame()
 void EventProfiler::BeginFrame()
 {
 {
-    if (!active_)
-        return;
     // End the previous frame if any
     // End the previous frame if any
     EndFrame();
     EndFrame();
 
 
@@ -82,8 +65,7 @@ void EventProfiler::BeginFrame()
 
 
 void EventProfiler::EndFrame()
 void EventProfiler::EndFrame()
 {
 {
-    if (!active_)
-        return;
+
     if (current_ != root_)
     if (current_ != root_)
     {
     {
         EndBlock();
         EndBlock();
@@ -98,8 +80,6 @@ void EventProfiler::EndFrame()
 
 
 void EventProfiler::BeginInterval()
 void EventProfiler::BeginInterval()
 {
 {
-    if (!active_)
-        return;
     root_->BeginInterval();
     root_->BeginInterval();
     intervalFrames_ = 0;
     intervalFrames_ = 0;
 }
 }

+ 2 - 9
Source/Urho3D/Core/EventProfiler.h

@@ -174,16 +174,11 @@ public:
     /// Destruct.
     /// Destruct.
     virtual ~EventProfiler();
     virtual ~EventProfiler();
 
 
-    /// Activate the event profiler to collect information. Request deactivation, will delete all blocks!
-    void SetActive(bool active);
-    /// Return true if active.
-    bool IsActive() const { return active_; }
-
     /// Begin timing a profiling block.
     /// Begin timing a profiling block.
     void BeginBlock(StringHash eventID)
     void BeginBlock(StringHash eventID)
     {
     {
         // Profiler supports only the main thread currently
         // Profiler supports only the main thread currently
-        if (!active_ || !Thread::IsMainThread())
+        if (!Thread::IsMainThread())
             return;
             return;
 
 
         current_ = current_->GetChild(eventID);
         current_ = current_->GetChild(eventID);
@@ -193,7 +188,7 @@ public:
     /// End timing the current profiling block.
     /// End timing the current profiling block.
     void EndBlock()
     void EndBlock()
     {
     {
-        if (!active_ || !Thread::IsMainThread())
+        if (!Thread::IsMainThread())
             return;
             return;
 
 
         if (current_ != root_)
         if (current_ != root_)
@@ -219,8 +214,6 @@ public:
 private:
 private:
     /// Return profiling data as text output for a specified profiling block.
     /// Return profiling data as text output for a specified profiling block.
     void PrintData(EventProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const;
     void PrintData(EventProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const;
-    /// Is the profiler collecting event information.
-    bool active_;
     /// Current profiling block.
     /// Current profiling block.
     EventProfilerBlock* current_;
     EventProfilerBlock* current_;
     /// Root profiling block.
     /// Root profiling block.

+ 4 - 5
Source/Urho3D/Core/Object.cpp

@@ -305,9 +305,8 @@ void Object::SendEvent(StringHash eventType, VariantMap& eventData)
         return;
         return;
     }
     }
 #ifdef URHO3D_PROFILING
 #ifdef URHO3D_PROFILING
-    EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
-    if (eventProfiler)
-        eventProfiler->BeginBlock(eventType);
+    if (context_->GetSubsystemStatic<EventProfiler>())
+        context_->GetSubsystemStatic<EventProfiler>()->BeginBlock(eventType);
 #endif
 #endif
 
 
     // Make a weak pointer to self to check for destruction during event handling
     // Make a weak pointer to self to check for destruction during event handling
@@ -407,8 +406,8 @@ void Object::SendEvent(StringHash eventType, VariantMap& eventData)
     context->EndSendEvent();
     context->EndSendEvent();
 
 
 #ifdef URHO3D_PROFILING
 #ifdef URHO3D_PROFILING
-    if (eventProfiler)
-        eventProfiler->EndBlock();
+    if (context_->GetSubsystemStatic<EventProfiler>())
+        context_->GetSubsystemStatic<EventProfiler>()->EndBlock();
 #endif
 #endif
 }
 }
 
 

+ 1 - 1
Source/Urho3D/Core/Object.h

@@ -357,7 +357,7 @@ private:
 #endif
 #endif
 
 
 /// Register event names.
 /// Register event names.
-struct EventNameRegistrar
+struct URHO3D_API EventNameRegistrar
 {
 {
     static StringHash RegisterEventName(const char* eventName);
     static StringHash RegisterEventName(const char* eventName);
     /// Return Event name or empty string if not found.
     /// Return Event name or empty string if not found.

+ 5 - 0
Source/Urho3D/Core/Timer.cpp

@@ -164,6 +164,11 @@ unsigned Time::GetSystemTime()
     return Tick();
     return Tick();
 }
 }
 
 
+long long Time::GetSystemHiresTime()
+{
+    return HiresTick();
+}
+
 unsigned Time::GetTimeSinceEpoch()
 unsigned Time::GetTimeSinceEpoch()
 {
 {
     return (unsigned)time(NULL);
     return (unsigned)time(NULL);

+ 2 - 0
Source/Urho3D/Core/Timer.h

@@ -106,6 +106,8 @@ public:
 
 
     /// Get system time as milliseconds.
     /// Get system time as milliseconds.
     static unsigned GetSystemTime();
     static unsigned GetSystemTime();
+    /// Get system time as microseconds.
+    static long long GetSystemHiresTime();
     /// Get system time as seconds since 1.1.1970.
     /// Get system time as seconds since 1.1.1970.
     static unsigned GetTimeSinceEpoch();
     static unsigned GetTimeSinceEpoch();
     /// Get a date/time stamp as a string.
     /// Get a date/time stamp as a string.

+ 12 - 14
Source/Urho3D/Engine/DebugHud.cpp

@@ -238,20 +238,18 @@ void DebugHud::SetMode(unsigned mode)
     memoryText_->SetVisible((mode & DEBUGHUD_SHOW_MEMORY) != 0);
     memoryText_->SetVisible((mode & DEBUGHUD_SHOW_MEMORY) != 0);
     eventProfilerText_->SetVisible((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0);
     eventProfilerText_->SetVisible((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0);
 #ifdef URHO3D_PROFILING
 #ifdef URHO3D_PROFILING
-    if ((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0)
-    {
-        EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
-        if (!eventProfiler)
-            context_->RegisterSubsystem(eventProfiler = new EventProfiler(context_));
-        if (eventProfiler)
-            eventProfiler->SetActive(true);
-    }
-    else
-    {
-        EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
-        if (eventProfiler)
-            eventProfiler->SetActive(false);
-    }
+//     if ((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0)
+//     {
+//         EventProfiler* eventProfiler = context_->GetSubsystemStatic<EventProfiler>();
+//         if (!eventProfiler)
+//             context_->RegisterSubsystemStatic<EventProfiler>();
+//     }
+//     else
+//     {
+//         if (context_->GetSubsystemStatic<EventProfiler>())
+//             context_->RemoveSubsystem<EventProfiler>();
+// 
+//     }
 #endif
 #endif
     mode_ = mode;
     mode_ = mode;
 }
 }

+ 38 - 51
Source/Urho3D/Engine/Engine.cpp

@@ -115,26 +115,26 @@ Engine::Engine(Context* context) :
     context_->RegisterSubsystem(this);
     context_->RegisterSubsystem(this);
 
 
     // Create subsystems which do not depend on engine initialization or startup parameters
     // Create subsystems which do not depend on engine initialization or startup parameters
-    context_->RegisterSubsystem(new Time(context_));
-    context_->RegisterSubsystem(new WorkQueue(context_));
+    context_->RegisterSubsystemStatic<Time>();
+    context_->RegisterSubsystemStatic<WorkQueue>();
 #ifdef URHO3D_PROFILING
 #ifdef URHO3D_PROFILING
-    context_->RegisterSubsystem(new Profiler(context_));
+    context_->RegisterSubsystemStatic<Profiler>();
 #endif
 #endif
-    context_->RegisterSubsystem(new FileSystem(context_));
+    context_->RegisterSubsystemStatic<FileSystem>();
 #ifdef URHO3D_LOGGING
 #ifdef URHO3D_LOGGING
-    context_->RegisterSubsystem(new Log(context_));
+    context_->RegisterSubsystemStatic<Log>();
 #endif
 #endif
-    context_->RegisterSubsystem(new ResourceCache(context_));
-    context_->RegisterSubsystem(new Localization(context_));
+    context_->RegisterSubsystemStatic<ResourceCache>();
+    context_->RegisterSubsystemStatic<Localization>();
 #ifdef URHO3D_NETWORK
 #ifdef URHO3D_NETWORK
-    context_->RegisterSubsystem(new Network(context_));
+    context_->RegisterSubsystemStatic<Network>();
 #endif
 #endif
 #ifdef URHO3D_DATABASE
 #ifdef URHO3D_DATABASE
-    context_->RegisterSubsystem(new Database(context_));
+    context_->RegisterSubsystemStatic<Database>();
 #endif
 #endif
-    context_->RegisterSubsystem(new Input(context_));
-    context_->RegisterSubsystem(new Audio(context_));
-    context_->RegisterSubsystem(new UI(context_));
+    context_->RegisterSubsystemStatic<Input>();
+    context_->RegisterSubsystemStatic<Audio>();
+    context_->RegisterSubsystemStatic<UI>();
 
 
     // Register object factories for libraries which are not automatically registered along with subsystem creation
     // Register object factories for libraries which are not automatically registered along with subsystem creation
     RegisterSceneLibrary(context_);
     RegisterSceneLibrary(context_);
@@ -167,8 +167,8 @@ bool Engine::Initialize(const VariantMap& parameters)
     // Register the rest of the subsystems
     // Register the rest of the subsystems
     if (!headless_)
     if (!headless_)
     {
     {
-        context_->RegisterSubsystem(new Graphics(context_));
-        context_->RegisterSubsystem(new Renderer(context_));
+        context_->RegisterSubsystemStatic<Graphics>();
+        context_->RegisterSubsystemStatic<Renderer>();
     }
     }
     else
     else
     {
     {
@@ -426,11 +426,7 @@ bool Engine::Initialize(const VariantMap& parameters)
 #endif
 #endif
 #ifdef URHO3D_PROFILING
 #ifdef URHO3D_PROFILING
     if (GetParameter(parameters, "EventProfiler", true).GetBool())
     if (GetParameter(parameters, "EventProfiler", true).GetBool())
-    {
-        EventProfiler* evpr = new EventProfiler(context_);
-        context_->RegisterSubsystem(evpr);
-        evpr->SetActive(true);
-    }
+        context_->RegisterSubsystemStatic<EventProfiler>();   
 #endif
 #endif
     frameTimer_.Reset();
     frameTimer_.Reset();
 
 
@@ -452,13 +448,13 @@ void Engine::RunFrame()
 
 
     // Note: there is a minimal performance cost to looking up subsystems (uses a hashmap); if they would be looked up several
     // Note: there is a minimal performance cost to looking up subsystems (uses a hashmap); if they would be looked up several
     // times per frame it would be better to cache the pointers
     // times per frame it would be better to cache the pointers
-    Time* time = GetSubsystem<Time>();
-    Input* input = GetSubsystem<Input>();
-    Audio* audio = GetSubsystem<Audio>();
+    Time* time = context_->GetSubsystemStatic<Time>();
+    Input* input = context_->GetSubsystemStatic<Input>();
+    Audio* audio = context_->GetSubsystemStatic<Audio>();
 #ifdef URHO3D_PROFILING
 #ifdef URHO3D_PROFILING
-    EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
-    if (eventProfiler)
-        eventProfiler->BeginFrame();
+
+    if (context_->GetSubsystemStatic<EventProfiler>())
+        context_->GetSubsystemStatic<EventProfiler>()->BeginFrame();
 #endif
 #endif
     time->BeginFrame(timeStep_);
     time->BeginFrame(timeStep_);
 
 
@@ -488,8 +484,8 @@ void Engine::RunFrame()
 
 
     time->EndFrame();
     time->EndFrame();
 #ifdef URHO3D_PROFILING
 #ifdef URHO3D_PROFILING
-    if (eventProfiler)
-        eventProfiler->EndFrame();
+    if (context_->GetSubsystemStatic<EventProfiler>())
+        context_->GetSubsystemStatic<EventProfiler>()->EndFrame();
 #endif
 #endif
 }
 }
 
 
@@ -499,14 +495,10 @@ Console* Engine::CreateConsole()
         return 0;
         return 0;
 
 
     // Return existing console if possible
     // Return existing console if possible
-    Console* console = GetSubsystem<Console>();
-    if (!console)
-    {
-        console = new Console(context_);
-        context_->RegisterSubsystem(console);
-    }
-
-    return console;
+    if (!context_->GetSubsystemStatic<Console>())
+        context_->RegisterSubsystemStatic<Console>();
+    
+    return context_->GetSubsystemStatic<Console>();
 }
 }
 
 
 DebugHud* Engine::CreateDebugHud()
 DebugHud* Engine::CreateDebugHud()
@@ -515,14 +507,10 @@ DebugHud* Engine::CreateDebugHud()
         return 0;
         return 0;
 
 
     // Return existing debug HUD if possible
     // Return existing debug HUD if possible
-    DebugHud* debugHud = GetSubsystem<DebugHud>();
-    if (!debugHud)
-    {
-        debugHud = new DebugHud(context_);
-        context_->RegisterSubsystem(debugHud);
-    }
+    if (!context_->GetSubsystemStatic<DebugHud>())
+        context_->RegisterSubsystemStatic<DebugHud>();
 
 
-    return debugHud;
+    return context_->GetSubsystemStatic<DebugHud>();
 }
 }
 
 
 void Engine::SetTimeStepSmoothing(int frames)
 void Engine::SetTimeStepSmoothing(int frames)
@@ -575,15 +563,14 @@ void Engine::Exit()
 
 
 void Engine::DumpProfiler()
 void Engine::DumpProfiler()
 {
 {
-    Profiler* profiler = GetSubsystem<Profiler>();
-    if (profiler)
-        URHO3D_LOGRAW(profiler->PrintData(true, true) + "\n");
+    if (context_->GetSubsystemStatic<Profiler>())
+        URHO3D_LOGRAW(context_->GetSubsystemStatic<Profiler>()->PrintData(true, true) + "\n");
 }
 }
 
 
 void Engine::DumpResources(bool dumpFileName)
 void Engine::DumpResources(bool dumpFileName)
 {
 {
 #ifdef URHO3D_LOGGING
 #ifdef URHO3D_LOGGING
-    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    ResourceCache* cache = context_->GetSubsystemStatic<ResourceCache>();
     const HashMap<StringHash, ResourceGroup>& resourceGroups = cache->GetAllResources();
     const HashMap<StringHash, ResourceGroup>& resourceGroups = cache->GetAllResources();
     URHO3D_LOGRAW("\n");
     URHO3D_LOGRAW("\n");
 
 
@@ -676,12 +663,12 @@ void Engine::Render()
     URHO3D_PROFILE(Render);
     URHO3D_PROFILE(Render);
 
 
     // If device is lost, BeginFrame will fail and we skip rendering
     // If device is lost, BeginFrame will fail and we skip rendering
-    Graphics* graphics = GetSubsystem<Graphics>();
+    Graphics* graphics = context_->GetSubsystemStatic<Graphics>();
     if (!graphics->BeginFrame())
     if (!graphics->BeginFrame())
         return;
         return;
 
 
-    GetSubsystem<Renderer>()->Render();
-    GetSubsystem<UI>()->Render();
+    context_->GetSubsystemStatic<Renderer>()->Render();
+    context_->GetSubsystemStatic<UI>()->Render();
     graphics->EndFrame();
     graphics->EndFrame();
 }
 }
 
 
@@ -691,7 +678,7 @@ void Engine::ApplyFrameLimit()
         return;
         return;
 
 
     unsigned maxFps = maxFps_;
     unsigned maxFps = maxFps_;
-    Input* input = GetSubsystem<Input>();
+    Input* input = context_->GetSubsystemStatic<Input>();
     if (input && !input->HasFocus())
     if (input && !input->HasFocus())
         maxFps = Min(maxInactiveFps_, maxFps);
         maxFps = Min(maxInactiveFps_, maxFps);
 
 
@@ -936,7 +923,7 @@ void Engine::HandleExitRequested(StringHash eventType, VariantMap& eventData)
 
 
 void Engine::DoExit()
 void Engine::DoExit()
 {
 {
-    Graphics* graphics = GetSubsystem<Graphics>();
+    Graphics* graphics = context_->GetSubsystemStatic<Graphics>();
     if (graphics)
     if (graphics)
         graphics->Close();
         graphics->Close();