From 1e2705ef06790f5d369a8cde9d3d8a774da78da2 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Fri, 20 Sep 2024 17:29:42 +0200 Subject: [PATCH] removing all singletons --- runtime/Includes/Core/Application.h | 7 +++++++ runtime/Includes/Core/Memory.h | 12 +++++++----- runtime/Includes/Core/Profiler.h | 15 ++++++++------- runtime/Includes/Core/SDLManager.h | 20 ++++++++------------ runtime/Includes/PreCompiled.h | 1 + runtime/Includes/Utils/Singleton.h | 20 -------------------- runtime/Sources/Core/Application.cpp | 13 ++++++++++--- runtime/Sources/Core/Memory.cpp | 8 ++++++++ runtime/Sources/Core/Profiler.cpp | 3 +++ runtime/Sources/Core/SDLManager.cpp | 9 +++++++-- 10 files changed, 59 insertions(+), 49 deletions(-) delete mode 100644 runtime/Includes/Utils/Singleton.h diff --git a/runtime/Includes/Core/Application.h b/runtime/Includes/Core/Application.h index f0b265d..bc8c5c9 100644 --- a/runtime/Includes/Core/Application.h +++ b/runtime/Includes/Core/Application.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include namespace mlx @@ -47,12 +49,17 @@ namespace mlx ~Application(); private: + std::unique_ptr p_mem_manager; // Putting ptr here to initialise them before inputs, even if it f*cks the padding + std::unique_ptr p_sdl_manager; FpsManager m_fps; Inputs m_in; ImageRegistry m_image_registry; std::vector> m_graphics; std::function f_loop_hook; std::unique_ptr p_render_core; + #ifdef PROFILER + std::unique_ptr p_profiler; + #endif Handle p_param = nullptr; }; } diff --git a/runtime/Includes/Core/Memory.h b/runtime/Includes/Core/Memory.h index ad0bff9..7597f01 100644 --- a/runtime/Includes/Core/Memory.h +++ b/runtime/Includes/Core/Memory.h @@ -3,21 +3,23 @@ namespace mlx { - class MemManager : public Singleton + class MemManager { - friend class Singleton; - public: + MemManager(); + static void* Malloc(std::size_t size); static void* Calloc(std::size_t n, std::size_t size); static void* Realloc(void* ptr, std::size_t size); static void Free(void* ptr); - private: - MemManager() = default; + inline static bool IsInit() noexcept { return s_instance != nullptr; } + inline static MemManager& Get() noexcept { return *s_instance; } + ~MemManager(); private: + static MemManager* s_instance; inline static std::list s_blocks; }; } diff --git a/runtime/Includes/Core/Profiler.h b/runtime/Includes/Core/Profiler.h index ec977bc..3d5e56d 100644 --- a/runtime/Includes/Core/Profiler.h +++ b/runtime/Includes/Core/Profiler.h @@ -1,8 +1,6 @@ #ifndef __MLX_PROFILER__ #define __MLX_PROFILER__ -#include - namespace mlx { using FloatingPointMilliseconds = std::chrono::duration; @@ -14,20 +12,21 @@ namespace mlx std::thread::id thread_id; }; - class Profiler : public Singleton + class Profiler { - friend class Singleton; - public: Profiler(const Profiler&) = delete; Profiler(Profiler&&) = delete; + Profiler() { BeginRuntimeSession(); s_instance = this; } void AppendProfileData(ProfileResult&& result); - private: - Profiler() { BeginRuntimeSession(); } + inline static bool IsInit() noexcept { return s_instance != nullptr; } + inline static Profiler& Get() noexcept { return *s_instance; } + ~Profiler(); + private: void BeginRuntimeSession(); void WriteProfile(const ProfileResult& result); void EndRuntimeSession(); @@ -44,6 +43,8 @@ namespace mlx } private: + static Profiler* s_instance; + std::unordered_map> m_profile_data; std::ofstream m_output_stream; std::mutex m_mutex; diff --git a/runtime/Includes/Core/SDLManager.h b/runtime/Includes/Core/SDLManager.h index 61f273d..e314f05 100644 --- a/runtime/Includes/Core/SDLManager.h +++ b/runtime/Includes/Core/SDLManager.h @@ -5,13 +5,10 @@ namespace mlx { - class SDLManager : public Singleton + class SDLManager { - friend class Singleton; - public: - void Init() noexcept; - void Shutdown() noexcept; + SDLManager(); Handle CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden); void DestroyWindow(Handle window) noexcept; @@ -30,18 +27,17 @@ namespace mlx std::int32_t GetXRel() const noexcept; std::int32_t GetYRel() const noexcept; - private: - SDLManager() = default; - ~SDLManager() = default; + inline static bool IsInit() noexcept { return s_instance != nullptr; } + inline static SDLManager& Get() noexcept { return *s_instance; } + + ~SDLManager(); private: + static SDLManager* s_instance; + std::unordered_set m_windows_registry; func::function f_callback; void* p_callback_data = nullptr; - std::int32_t m_x; - std::int32_t m_y; - std::int32_t m_rel_x; - std::int32_t m_rel_y; bool m_drop_sdl_responsability = false; }; } diff --git a/runtime/Includes/PreCompiled.h b/runtime/Includes/PreCompiled.h index f948b11..bc3efbc 100644 --- a/runtime/Includes/PreCompiled.h +++ b/runtime/Includes/PreCompiled.h @@ -93,6 +93,7 @@ #include #include #include +#include using Handle = void*; diff --git a/runtime/Includes/Utils/Singleton.h b/runtime/Includes/Utils/Singleton.h deleted file mode 100644 index e5e76ad..0000000 --- a/runtime/Includes/Utils/Singleton.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef __MLX_SINGLETON__ -#define __MLX_SINGLETON__ - -#include - -namespace mlx -{ - template - class Singleton : public NonCopyable - { - public: - inline static T& Get() - { - static T instance; - return instance; - } - }; -} - -#endif // __MLX_SINGLETON__ diff --git a/runtime/Sources/Core/Application.cpp b/runtime/Sources/Core/Application.cpp index b854f34..cd53f58 100644 --- a/runtime/Sources/Core/Application.cpp +++ b/runtime/Sources/Core/Application.cpp @@ -8,7 +8,7 @@ namespace mlx { - Application::Application() : m_fps(), m_in() + Application::Application() : p_mem_manager(std::make_unique()), p_sdl_manager(std::make_unique()), m_fps(), m_in() { EventBus::RegisterListener({[](const EventBase& event) { @@ -16,8 +16,11 @@ namespace mlx std::abort(); }, "__MlxApplication" }); + #ifdef PROFILER + p_profiler = std::make_unique(); + #endif + m_fps.Init(); - SDLManager::Get().Init(); p_render_core = std::make_unique(); } @@ -89,6 +92,10 @@ namespace mlx Application::~Application() { p_render_core.reset(); - SDLManager::Get().Shutdown(); + p_sdl_manager.reset(); + #ifdef PROFILER + p_profiler.reset(); + #endif + p_mem_manager.reset(); } } diff --git a/runtime/Sources/Core/Memory.cpp b/runtime/Sources/Core/Memory.cpp index 36f7689..f5f8ce6 100644 --- a/runtime/Sources/Core/Memory.cpp +++ b/runtime/Sources/Core/Memory.cpp @@ -4,6 +4,13 @@ namespace mlx { + MemManager* MemManager::s_instance = nullptr; + + MemManager::MemManager() + { + s_instance = this; + } + void* MemManager::Malloc(std::size_t size) { void* ptr = std::malloc(size); @@ -49,5 +56,6 @@ namespace mlx { std::free(ptr); }); + s_instance = nullptr; } } diff --git a/runtime/Sources/Core/Profiler.cpp b/runtime/Sources/Core/Profiler.cpp index a3649f7..a4bb9d6 100644 --- a/runtime/Sources/Core/Profiler.cpp +++ b/runtime/Sources/Core/Profiler.cpp @@ -4,6 +4,8 @@ namespace mlx { + Profiler* Profiler::s_instance = nullptr; + void Profiler::BeginRuntimeSession() { std::lock_guard lock(m_mutex); @@ -63,5 +65,6 @@ namespace mlx if(!m_runtime_session_began) return; EndRuntimeSession(); + s_instance = nullptr; } } diff --git a/runtime/Sources/Core/SDLManager.cpp b/runtime/Sources/Core/SDLManager.cpp index f96fa58..13031aa 100644 --- a/runtime/Sources/Core/SDLManager.cpp +++ b/runtime/Sources/Core/SDLManager.cpp @@ -26,9 +26,13 @@ namespace mlx }; } - void SDLManager::Init() noexcept + SDLManager* SDLManager::s_instance = nullptr; + + SDLManager::SDLManager() { MLX_PROFILE_FUNCTION(); + s_instance = this; + m_drop_sdl_responsability = SDL_WasInit(SDL_INIT_VIDEO); if(m_drop_sdl_responsability) // is case the mlx is running in a sandbox like MacroUnitTester where SDL is already init return; @@ -224,12 +228,13 @@ namespace mlx return y; } - void SDLManager::Shutdown() noexcept + SDLManager::~SDLManager() { if(m_drop_sdl_responsability) return; SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS); SDL_Quit(); + s_instance = nullptr; DebugLog("SDL Manager uninitialized"); } }