Explorar o código

Revert "Testing static registration of subsystems"

This reverts commit 2a0a9df4dd75c24f3702182010c541db3123900b.
svifylabs %!s(int64=9) %!d(string=hai) anos
pai
achega
16257f80c5

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

@@ -25,7 +25,6 @@
 #include "../Core/Attribute.h"
 #include "../Core/Object.h"
 #include "../Container/HashSet.h"
-#include "../Container/Ptr.h"
 
 namespace Urho3D
 {
@@ -71,8 +70,6 @@ public:
     template <class T> void RegisterFactory();
     /// Template version of registering an object factory with 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 <class T> void RemoveSubsystem();
     /// Template version of registering an object attribute.
@@ -117,8 +114,6 @@ public:
     AttributeInfo* GetAttribute(StringHash objectType, const char* name);
     /// Template version of returning a subsystem.
     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 <class T> AttributeInfo* GetAttribute(const char* name);
 
@@ -160,8 +155,6 @@ public:
     }
 
 private:
-    /// Template version of caching the Subsystem in a static variable for fast access.
-    template <class T> WeakPtr<T>& CacheSubsystem();
     /// Add event receiver.
     void AddEventReceiver(Object* receiver, StringHash eventType);
     /// Add event receiver for specific event.
@@ -213,7 +206,7 @@ template <class T> void Context::RegisterFactory(const char* category)
     RegisterFactory(new ObjectFactoryImpl<T>(this), category);
 }
 
-template <class T> void Context::RemoveSubsystem() { RemoveSubsystem(T::GetTypeStatic());  CacheSubsystem<T>().Reset(); }
+template <class T> void Context::RemoveSubsystem() { RemoveSubsystem(T::GetTypeStatic()); }
 
 template <class T> void Context::RegisterAttribute(const AttributeInfo& attr) { RegisterAttribute(T::GetTypeStatic(), attr); }
 
@@ -223,32 +216,8 @@ 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::GetSubsystemStatic() { return CacheSubsystem<T>().Get(); }
-
 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)
 {
     UpdateAttributeDefaultValue(T::GetTypeStatic(), name, defaultValue);

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

@@ -37,6 +37,7 @@ static const int NAME_MAX_LENGTH = 30;
 
 EventProfiler::EventProfiler(Context* context) : 
     Object(context),
+    active_(false),
     current_(0),
     root_(0),
     intervalFrames_(0),
@@ -53,9 +54,25 @@ EventProfiler::~EventProfiler()
     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()
 {
+    if (!active_)
+        return;
     // End the previous frame if any
     EndFrame();
 
@@ -65,7 +82,8 @@ void EventProfiler::BeginFrame()
 
 void EventProfiler::EndFrame()
 {
-
+    if (!active_)
+        return;
     if (current_ != root_)
     {
         EndBlock();
@@ -80,6 +98,8 @@ void EventProfiler::EndFrame()
 
 void EventProfiler::BeginInterval()
 {
+    if (!active_)
+        return;
     root_->BeginInterval();
     intervalFrames_ = 0;
 }

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

@@ -174,11 +174,16 @@ public:
     /// Destruct.
     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.
     void BeginBlock(StringHash eventID)
     {
         // Profiler supports only the main thread currently
-        if (!Thread::IsMainThread())
+        if (!active_ || !Thread::IsMainThread())
             return;
 
         current_ = current_->GetChild(eventID);
@@ -188,7 +193,7 @@ public:
     /// End timing the current profiling block.
     void EndBlock()
     {
-        if (!Thread::IsMainThread())
+        if (!active_ || !Thread::IsMainThread())
             return;
 
         if (current_ != root_)
@@ -214,6 +219,8 @@ public:
 private:
     /// 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;
+    /// Is the profiler collecting event information.
+    bool active_;
     /// Current profiling block.
     EventProfilerBlock* current_;
     /// Root profiling block.

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

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

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

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

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

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

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

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

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

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

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

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