Skip to content

Commit

Permalink
Added explicit Object::GetSubsystem() template specializations that r…
Browse files Browse the repository at this point in the history
…eturn cached subsystem pointers instead of looking them up in the hashmap every time.

Moved around code that caches subsystem pointers to more appropriate places.
Crash fix - do not initialize SystemUI in headless mode.
  • Loading branch information
rokups committed Jun 27, 2017
1 parent 4b61629 commit 5657dfe
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 45 deletions.
21 changes: 0 additions & 21 deletions Source/Atomic/Core/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,6 @@ class GlobalEventListener
virtual void EndSendEvent(Context* context, Object* sender, StringHash eventType, VariantMap& eventData) = 0;
};

class Metrics;
class Engine;
class Time;
class WorkQueue;
class Profiler;
class FileSystem;
class Log;
class ResourceCache;
class Localization;
class Network;
class Web;
class Database;
class Input;
class Audio;
class UI;
class SystemUI;
class Graphics;
class Renderer;
class Console;
class DebugHud;

// ATOMIC END

/// Tracking structure for event receivers.
Expand Down
100 changes: 100 additions & 0 deletions Source/Atomic/Core/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,106 @@ void Object::UnsubscribeFromEventReceiver(Object* receiver)

}

template <> Engine* Object::GetSubsystem<Engine>() const
{
return context_->engine_;
}

template <> Time* Object::GetSubsystem<Time>() const
{
return context_->time_;
}

template <> WorkQueue* Object::GetSubsystem<WorkQueue>() const
{
return context_->workQueue_;
}

template <> Profiler* Object::GetSubsystem<Profiler>() const
{
return context_->profiler_;
}

template <> FileSystem* Object::GetSubsystem<FileSystem>() const
{
return context_->fileSystem_;
}

template <> Log* Object::GetSubsystem<Log>() const
{
return context_->log_;
}

template <> ResourceCache* Object::GetSubsystem<ResourceCache>() const
{
return context_->cache_;
}

template <> Localization* Object::GetSubsystem<Localization>() const
{
return context_->l18n_;
}

template <> Network* Object::GetSubsystem<Network>() const
{
return context_->network_;
}

template <> Web* Object::GetSubsystem<Web>() const
{
return context_->web_;
}

template <> Database* Object::GetSubsystem<Database>() const
{
return context_->db_;
}

template <> Input* Object::GetSubsystem<Input>() const
{
return context_->input_;
}

template <> Audio* Object::GetSubsystem<Audio>() const
{
return context_->audio_;
}

template <> UI* Object::GetSubsystem<UI>() const
{
return context_->ui_;
}

template <> SystemUI* Object::GetSubsystem<SystemUI>() const
{
return context_->systemUi_;
}

template <> Graphics* Object::GetSubsystem<Graphics>() const
{
return context_->graphics_;
}

template <> Renderer* Object::GetSubsystem<Renderer>() const
{
return context_->renderer_;
}

template <> Console* Object::GetSubsystem<Console>() const
{
return context_->console_;
}

template <> DebugHud* Object::GetSubsystem<DebugHud>() const
{
return context_->debugHud_;
}

template <> Metrics* Object::GetSubsystem<Metrics>() const
{
return context_->metrics_;
}

// ATOMIC END

}
49 changes: 49 additions & 0 deletions Source/Atomic/Core/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ namespace Atomic
class Context;
class EventHandler;

// ATOMIC BEGIN
class Engine;
class Time;
class WorkQueue;
class Profiler;
class FileSystem;
class Log;
class ResourceCache;
class Localization;
class Network;
class Web;
class Database;
class Input;
class Audio;
class UI;
class SystemUI;
class Graphics;
class Renderer;
class Console;
class DebugHud;
class Metrics;
// ATOMIC END

/// Type info.
class ATOMIC_API TypeInfo
{
Expand Down Expand Up @@ -409,4 +432,30 @@ struct ATOMIC_API EventNameRegistrar
/// Convenience macro to construct an EventHandler that points to a receiver object and its member function, and also defines a userdata pointer.
#define ATOMIC_HANDLER_USERDATA(className, function, userData) (new Atomic::EventHandlerImpl<className>(this, &className::function, userData))


// ATOMIC BEGIN
// Explicit template specializations for most commonly used engine subsystems. They sidestep HashMap lookup and return
// subsystem pointer cached in Context object.
template <> Engine* Object::GetSubsystem<Engine>() const;
template <> Time* Object::GetSubsystem<Time>() const;
template <> WorkQueue* Object::GetSubsystem<WorkQueue>() const;
template <> Profiler* Object::GetSubsystem<Profiler>() const;
template <> FileSystem* Object::GetSubsystem<FileSystem>() const;
template <> Log* Object::GetSubsystem<Log>() const;
template <> ResourceCache* Object::GetSubsystem<ResourceCache>() const;
template <> Localization* Object::GetSubsystem<Localization>() const;
template <> Network* Object::GetSubsystem<Network>() const;
template <> Web* Object::GetSubsystem<Web>() const;
template <> Database* Object::GetSubsystem<Database>() const;
template <> Input* Object::GetSubsystem<Input>() const;
template <> Audio* Object::GetSubsystem<Audio>() const;
template <> UI* Object::GetSubsystem<UI>() const;
template <> SystemUI* Object::GetSubsystem<SystemUI>() const;
template <> Graphics* Object::GetSubsystem<Graphics>() const;
template <> Renderer* Object::GetSubsystem<Renderer>() const;
template <> Console* Object::GetSubsystem<Console>() const;
template <> DebugHud* Object::GetSubsystem<DebugHud>() const;
template <> Metrics* Object::GetSubsystem<Metrics>() const;
// ATOMIC END

}
61 changes: 37 additions & 24 deletions Source/Atomic/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,31 @@ Engine::Engine(Context* context) :
// ATOMIC BEGIN
SubscribeToEvent(E_PAUSERESUMEREQUESTED, ATOMIC_HANDLER(Engine, HandlePauseResumeRequested));
SubscribeToEvent(E_PAUSESTEPREQUESTED, ATOMIC_HANDLER(Engine, HandlePauseStepRequested));

context_->engine_ = context_->GetSubsystem<Engine>();
context_->time_ = context_->GetSubsystem<Time>();
context_->workQueue_ = context_->GetSubsystem<WorkQueue>();
#ifdef ATOMIC_PROFILING
context_->profiler_ = context_->GetSubsystem<Profiler>();
#endif
context_->fileSystem_ = context_->GetSubsystem<FileSystem>();
#ifdef ATOMIC_LOGGING
context_->log_ = context_->GetSubsystem<Log>();
#endif
context_->cache_ = context_->GetSubsystem<ResourceCache>();
context_->l18n_ = context_->GetSubsystem<Localization>();
#ifdef ATOMIC_NETWORK
context_->network_ = context_->GetSubsystem<Network>();
#endif
#ifdef ATOMIC_WEB
context_->web_ = context_->GetSubsystem<Web>();
#endif
#ifdef ATOMIC_DATABASE
context_->db_ = context_->GetSubsystem<Database>();
#endif
context_->input_ = context_->GetSubsystem<Input>();
context_->audio_ = context_->GetSubsystem<Audio>();
context_->ui_ = context_->GetSubsystem<UI>();
// ATOMIC END
}

Expand All @@ -206,6 +231,10 @@ bool Engine::Initialize(const VariantMap& parameters)
{
context_->RegisterSubsystem(new Graphics(context_));
context_->RegisterSubsystem(new Renderer(context_));
// ATOMIC BEGIN
context_->graphics_ = context_->GetSubsystem<Graphics>();
context_->renderer_ = context_->GetSubsystem<Renderer>();
// ATOMIC END
}
else
{
Expand Down Expand Up @@ -349,30 +378,14 @@ bool Engine::Initialize(const VariantMap& parameters)
#endif

// ATOMIC BEGIN
context_->RegisterSubsystem(new SystemUI(context_));

context_->engine_ = GetSubsystem<Engine>();
context_->time_ = GetSubsystem<Time>();
context_->workQueue_ = GetSubsystem<WorkQueue>();
context_->profiler_ = GetSubsystem<Profiler>();
context_->fileSystem_ = GetSubsystem<FileSystem>();
context_->log_ = GetSubsystem<Log>();
context_->cache_ = GetSubsystem<ResourceCache>();
context_->l18n_ = GetSubsystem<Localization>();
context_->network_ = GetSubsystem<Network>();
context_->web_ = GetSubsystem<Web>();
#ifdef ATOMIC_DATABASE
context_->db_ = GetSubsystem<Database>();
#endif
context_->input_ = GetSubsystem<Input>();
context_->audio_ = GetSubsystem<Audio>();
context_->ui_ = GetSubsystem<UI>();
context_->systemUi_ = GetSubsystem<SystemUI>();
context_->graphics_ = GetSubsystem<Graphics>();
context_->renderer_ = GetSubsystem<Renderer>();
context_->console_ = GetSubsystem<Console>();
context_->debugHud_ = GetSubsystem<DebugHud>();
context_->metrics_ = GetSubsystem<Metrics>();
if (!headless_)
{
context_->RegisterSubsystem(new SystemUI(context_));
context_->systemUi_ = context_->GetSubsystem<SystemUI>();
context_->console_ = context_->GetSubsystem<Console>();
context_->debugHud_ = context_->GetSubsystem<DebugHud>();
}
context_->metrics_ = context_->GetSubsystem<Metrics>();
// ATOMIC END

frameTimer_.Reset();
Expand Down

0 comments on commit 5657dfe

Please sign in to comment.