From 60eee6e8098cbe3b4d7013e8da0f1967ddb3b0ec Mon Sep 17 00:00:00 2001 From: David Capello Date: Sat, 7 Dec 2024 12:34:23 -0300 Subject: [PATCH] Move Strings initialization to App instance creation This removes Strings::createInstance() and we can check with ASSERT() the singleton in the Strings class ctor/dtor. --- src/app/app.cpp | 13 ++----------- src/app/i18n/strings.cpp | 17 +++++++++-------- src/app/i18n/strings.h | 9 ++++----- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 97c2d8d2e..5706285dc 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -129,21 +129,12 @@ public: app::UIContext m_context; }; -class App::LoadLanguage { -public: - LoadLanguage(Preferences& pref, - Extensions& exts) { - Strings::createInstance(pref, exts); - } -}; - class App::Modules { public: LoggerModule m_loggerModule; FileSystemModule m_file_system_module; Extensions m_extensions; - // Load main language (after loading the extensions) - LoadLanguage m_loadLanguage; + Strings m_strings; // Load main language (after loading the extensions) tools::ToolBox m_toolbox; tools::ActiveToolManager m_activeToolManager; Commands m_commands; @@ -160,7 +151,7 @@ public: Modules(const bool createLogInDesktop, Preferences& pref) : m_loggerModule(createLogInDesktop) - , m_loadLanguage(pref, m_extensions) + , m_strings(pref, m_extensions) , m_activeToolManager(&m_toolbox) , m_recent_files(pref.general.recentItems()) #ifdef ENABLE_DATA_RECOVERY diff --git a/src/app/i18n/strings.cpp b/src/app/i18n/strings.cpp index 3f22ca916..e90f82b23 100644 --- a/src/app/i18n/strings.cpp +++ b/src/app/i18n/strings.cpp @@ -29,14 +29,6 @@ static Strings* singleton = nullptr; const char* Strings::kDefLanguage = "en"; -// static -void Strings::createInstance(Preferences& pref, - Extensions& exts) -{ - ASSERT(!singleton); - singleton = new Strings(pref, exts); -} - // static Strings* Strings::instance() { @@ -48,9 +40,18 @@ Strings::Strings(Preferences& pref, : m_pref(pref) , m_exts(exts) { + ASSERT(!singleton); + singleton = this; + loadLanguage(currentLanguage()); } +Strings::~Strings() +{ + ASSERT(singleton == this); + singleton = nullptr; +} + std::set Strings::availableLanguages() const { std::set result; diff --git a/src/app/i18n/strings.h b/src/app/i18n/strings.h index a5cef7b33..609ae3e3e 100644 --- a/src/app/i18n/strings.h +++ b/src/app/i18n/strings.h @@ -28,10 +28,12 @@ namespace app { public: static const char* kDefLanguage; - static void createInstance(Preferences& pref, - Extensions& exts); static Strings* instance(); + Strings(Preferences& pref, + Extensions& exts); + ~Strings(); + const std::string& translate(const char* id) const; const std::string& defaultString(const char* id) const; @@ -60,9 +62,6 @@ namespace app { obs::signal LanguageChange; private: - Strings(Preferences& pref, - Extensions& exts); - void loadLanguage(const std::string& langId); void loadStringsFromDataDir(const std::string& langId); void loadStringsFromExtension(const std::string& langId);