From 2f362efa3859dad2c2e66be1767bd7b9945a6846 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 14 May 2023 06:50:27 +0200 Subject: [PATCH] code: Migrate configuration to new dynamic loader --- source/configuration.cpp | 35 +++++++++++++++++++++-------------- source/configuration.hpp | 4 ++-- source/plugin.cpp | 6 ------ 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/source/configuration.cpp b/source/configuration.cpp index 35b54105..37b58eec 100644 --- a/source/configuration.cpp +++ b/source/configuration.cpp @@ -92,20 +92,27 @@ bool streamfx::configuration::is_different_version() return (version() & STREAMFX_MASK_COMPAT) != (STREAMFX_VERSION & STREAMFX_MASK_COMPAT); } -static std::shared_ptr _instance = nullptr; - -void streamfx::configuration::initialize() -{ - if (!_instance) - _instance = std::make_shared(); -} - -void streamfx::configuration::finalize() -{ - _instance.reset(); -} - std::shared_ptr streamfx::configuration::instance() { - return _instance; + static std::weak_ptr winst; + static std::mutex mtx; + + std::unique_lock lock(mtx); + auto instance = winst.lock(); + if (!instance) { + instance = std::shared_ptr(new streamfx::configuration()); + winst = instance; + } + return instance; } + +static std::shared_ptr loader_instance; + +static auto loader = streamfx::loader( + []() { // Initalizer + loader_instance = streamfx::configuration::instance(); + }, + []() { // Finalizer + loader_instance.reset(); + }, + streamfx::loader_priority::HIGHER); // Attempt to load after critical functionality. diff --git a/source/configuration.hpp b/source/configuration.hpp index f1f37b3b..25743019 100644 --- a/source/configuration.hpp +++ b/source/configuration.hpp @@ -26,6 +26,8 @@ namespace streamfx { public: ~configuration(); + + private: configuration(); public: @@ -39,8 +41,6 @@ namespace streamfx { bool is_different_version(); public /* Singleton */: - static void initialize(); - static void finalize(); static std::shared_ptr instance(); }; } // namespace streamfx diff --git a/source/plugin.cpp b/source/plugin.cpp index a47d49cd..cc1d2997 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -138,9 +138,6 @@ MODULE_EXPORT bool obs_module_load(void) } } - // Initialize global configuration. - streamfx::configuration::initialize(); - // Initialize Source Tracker _source_tracker = streamfx::obs::source_tracker::get(); @@ -331,9 +328,6 @@ MODULE_EXPORT void obs_module_unload(void) // _updater.reset(); //#endif - // Finalize Configuration - streamfx::configuration::finalize(); - // Run all finalizers. for (auto kv : streamfx::get_finalizers()) { for (auto init : kv.second) {