1
0
mirror of synced 2025-01-18 00:56:49 +01:00

Make views get auto deleted

This commit is contained in:
WerWolv 2021-02-03 00:56:33 +01:00
parent 785ecb8a78
commit 8c306a5d3d
7 changed files with 18 additions and 15 deletions

View File

@ -104,14 +104,14 @@ namespace hex {
Views() = delete;
template<hex::derived_from<View> T, typename ... Args>
static T* add(Args&& ... args) {
return static_cast<T*>(add(new T(std::forward<Args>(args)...)));
static void add(Args&& ... args) {
return add(std::make_unique<T>(std::forward<Args>(args)...));
}
static std::vector<View*>& getEntries();
static std::vector<std::unique_ptr<View>>& getEntries();
private:
static View* add(View *view);
static void add(std::unique_ptr<View> &&view);
};

View File

@ -4,11 +4,13 @@
#include <functional>
#include <list>
#include <map>
#include <memory>
#include <vector>
#include <hex/api/content_registry.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/api/event.hpp>
#include <hex/views/view.hpp>
#include <imgui.h>
#include <ImGuiFileBrowser.h>
@ -55,7 +57,7 @@ namespace hex {
static u32 customEventsLastId;
static std::vector<ContentRegistry::CommandPaletteCommands::Entry> commandPaletteCommands;
static std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function> patternLanguageFunctions;
static std::vector<View*> views;
static std::vector<std::unique_ptr<View>> views;
static std::vector<ContentRegistry::Tools::Entry> toolsEntries;
static std::vector<ContentRegistry::DataInspector::Entry> dataInspectorEntries;
static u32 patternPaletteOffset;

View File

@ -6,7 +6,6 @@
#include <ImGuiFileBrowser.h>
#include <hex/api/event.hpp>
#include <hex/helpers/shared_data.hpp>
#include <hex/providers/provider.hpp>
#include <functional>
@ -24,7 +23,7 @@ namespace hex {
virtual void drawContent() = 0;
virtual void drawMenu();
virtual bool handleShortcut(int key, int mods);
virtual bool isAvailable() { return SharedData::currentProvider != nullptr && SharedData::currentProvider->isAvailable(); }
virtual bool isAvailable();
static void openFileBrowser(std::string title, imgui_addons::ImGuiFileBrowser::DialogMode mode, std::string validExtensions, const std::function<void(std::string)> &callback);
static void doLater(std::function<void()> &&function);

View File

@ -149,11 +149,11 @@ namespace hex {
/* Views */
View* ContentRegistry::Views::add(View *view) {
return getEntries().emplace_back(view);
void ContentRegistry::Views::add(std::unique_ptr<View> &&view) {
getEntries().emplace_back(std::move(view));
}
std::vector<View*>& ContentRegistry::Views::getEntries() {
std::vector<std::unique_ptr<View>>& ContentRegistry::Views::getEntries() {
return SharedData::views;
}

View File

@ -11,7 +11,7 @@ namespace hex {
u32 SharedData::customEventsLastId;
std::vector<ContentRegistry::CommandPaletteCommands::Entry> SharedData::commandPaletteCommands;
std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function> SharedData::patternLanguageFunctions;
std::vector<View*> SharedData::views;
std::vector<std::unique_ptr<View>> SharedData::views;
std::vector<ContentRegistry::Tools::Entry> SharedData::toolsEntries;
std::vector<ContentRegistry::DataInspector::Entry> SharedData::dataInspectorEntries;
u32 SharedData::patternPaletteOffset;

View File

@ -16,6 +16,10 @@ namespace hex {
void View::drawMenu() { }
bool View::handleShortcut(int key, int mods) { return false; }
bool View::isAvailable() {
return SharedData::currentProvider != nullptr && SharedData::currentProvider->isAvailable();
}
std::vector<std::function<void()>>& View::getDeferedCalls() {
return SharedData::deferredCalls;
}

View File

@ -134,10 +134,6 @@ namespace hex {
this->deinitGLFW();
ContentRegistry::Settings::store();
for (auto &view : ContentRegistry::Views::getEntries())
delete view;
ContentRegistry::Views::getEntries().clear();
this->deinitPlugins();
EventManager::unsubscribe(Events::SettingsChanged, this);
@ -473,9 +469,11 @@ namespace hex {
void Window::initImGui() {
IMGUI_CHECKVERSION();
auto *ctx = ImGui::CreateContext();
GImGui = ctx;
ImGuiIO& io = ImGui::GetIO();
ImGuiStyle& style = ImGui::GetStyle();