From 21f38974a87f9b3c336017ea0dfb70b18a2c4eb3 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 8 Apr 2023 12:08:45 +0200 Subject: [PATCH] impr: Use smart pointers to allocate Views --- .../include/hex/api/content_registry.hpp | 8 +++---- lib/libimhex/include/hex/api/keybinding.hpp | 3 ++- lib/libimhex/source/api/content_registry.cpp | 12 +++++----- lib/libimhex/source/api/keybinding.cpp | 2 +- main/source/init/tasks.cpp | 22 +++++++------------ .../builtin/source/content/welcome_screen.cpp | 2 +- 6 files changed, 22 insertions(+), 27 deletions(-) diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index 23cb7fb3a..fca5242e8 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -311,8 +311,8 @@ namespace hex { namespace impl { - void add(View *view); - std::map &getEntries(); + void add(std::unique_ptr &&view); + std::map> &getEntries(); } @@ -324,7 +324,7 @@ namespace hex { */ template T, typename... Args> void add(Args &&...args) { - return impl::add(new T(std::forward(args)...)); + return impl::add(std::make_unique(std::forward(args)...)); } /** @@ -332,7 +332,7 @@ namespace hex { * @param unlocalizedName The unlocalized name of the view * @return The view if it exists, nullptr otherwise */ - View *getViewByName(const std::string &unlocalizedName); + View* getViewByName(const std::string &unlocalizedName); } /* Tools Registry. Allows adding new entries to the tools window */ diff --git a/lib/libimhex/include/hex/api/keybinding.hpp b/lib/libimhex/include/hex/api/keybinding.hpp index 09fbd6cbb..3fad177e5 100644 --- a/lib/libimhex/include/hex/api/keybinding.hpp +++ b/lib/libimhex/include/hex/api/keybinding.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -396,7 +397,7 @@ namespace hex { * @param focused Whether the current view is focused * @param keyCode The key code of the key that was pressed */ - static void process(View *currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode); + static void process(const std::unique_ptr ¤tView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode); /** * @brief Process a key event. This should be called from the main loop. diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 09de0fefa..ef0911ce6 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -354,25 +354,25 @@ namespace hex { namespace impl { - std::map &getEntries() { - static std::map views; + std::map> &getEntries() { + static std::map> views; return views; } } - void impl::add(View *view) { + void impl::add(std::unique_ptr &&view) { log::debug("Registered new view: {}", view->getUnlocalizedName()); - impl::getEntries().insert({ view->getUnlocalizedName(), view }); + impl::getEntries().insert({ view->getUnlocalizedName(), std::move(view) }); } - View *getViewByName(const std::string &unlocalizedName) { + View* getViewByName(const std::string &unlocalizedName) { auto &views = impl::getEntries(); if (views.contains(unlocalizedName)) - return views[unlocalizedName]; + return views[unlocalizedName].get(); else return nullptr; } diff --git a/lib/libimhex/source/api/keybinding.cpp b/lib/libimhex/source/api/keybinding.cpp index e101d2f73..3f16aaf6c 100644 --- a/lib/libimhex/source/api/keybinding.cpp +++ b/lib/libimhex/source/api/keybinding.cpp @@ -32,7 +32,7 @@ namespace hex { return pressedShortcut; } - void ShortcutManager::process(View *currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) { + void ShortcutManager::process(const std::unique_ptr ¤tView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) { Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, keyCode); if (focused && currentView->m_shortcuts.contains(pressedShortcut)) diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index a080bb1d5..eb93a3666 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -7,10 +7,11 @@ #include #include #include -#include #include #include #include +#include +#include #include #include @@ -123,8 +124,9 @@ namespace hex::init { bool migrateConfig(){ - // check if there is a new config in folder + // Check if there is a new config in folder auto configPaths = hex::fs::getDefaultPaths(hex::fs::ImHexPath::Config, false); + // There should always be exactly one config path on Linux std::fs::path newConfigPath = configPaths[0]; wolv::io::File newConfigFile(newConfigPath / "settings.json", wolv::io::File::Mode::Read); @@ -132,7 +134,7 @@ namespace hex::init { // find an old config std::fs::path oldConfigPath; - for(auto dir : hex::fs::appendPath(hex::fs::getDataPaths(), "config")) { + for (const auto &dir : hex::fs::appendPath(hex::fs::getDataPaths(), "config")) { wolv::io::File oldConfigFile(dir / "settings.json", wolv::io::File::Mode::Read); if (oldConfigFile.isValid()) { oldConfigPath = dir; @@ -141,11 +143,7 @@ namespace hex::init { } if (!oldConfigPath.empty()) { - // move it to new location - auto configPaths = hex::fs::getDefaultPaths(hex::fs::ImHexPath::Config, false); - // There should always be exactly one config path on Linux - std::fs::path newConfigPath = configPaths[0]; - log::info("Found config file in {} ! Migrating to {}", oldConfigPath.string(), newConfigPath.string()); + log::info("Found config file in {}! Migrating to {}", oldConfigPath.string(), newConfigPath.string()); std::fs::rename(oldConfigPath / "settings.json", newConfigPath / "settings.json"); wolv::io::File oldIniFile(oldConfigPath / "interface.ini", wolv::io::File::Mode::Read); @@ -294,12 +292,8 @@ namespace hex::init { ContentRegistry::PatternLanguage::impl::getPragmas().clear(); ContentRegistry::PatternLanguage::impl::getVisualizers().clear(); - { - auto &views = ContentRegistry::Views::impl::getEntries(); - for (auto &[name, view] : views) - delete view; - views.clear(); - } + ContentRegistry::Views::impl::getEntries().clear(); + impl::PopupBase::getOpenPopups().clear(); ContentRegistry::Tools::impl::getEntries().clear(); diff --git a/plugins/builtin/source/content/welcome_screen.cpp b/plugins/builtin/source/content/welcome_screen.cpp index 37c1c9e7b..75af335a9 100644 --- a/plugins/builtin/source/content/welcome_screen.cpp +++ b/plugins/builtin/source/content/welcome_screen.cpp @@ -211,7 +211,7 @@ namespace hex::plugin::builtin { static bool isAnyViewOpen() { const auto &views = ContentRegistry::Views::impl::getEntries(); return std::any_of(views.begin(), views.end(), - [](const std::pair &entry) { + [](const auto &entry) { return entry.second->getWindowOpenState(); }); }