From 34710b16716fd76350e3492d8c3f7b762cb3e054 Mon Sep 17 00:00:00 2001 From: Jack Bennett Date: Mon, 10 Jun 2024 20:41:59 +0100 Subject: [PATCH] Switch to using C++ smart pointers in most cases --- texedit/gui/layout/panes/preview_pane.cpp | 12 ++---------- texedit/gui/layout/panes/preview_pane.hpp | 9 +++++---- texedit/gui/main_frame.cpp | 5 ++--- texedit/gui/main_frame.hpp | 3 ++- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/texedit/gui/layout/panes/preview_pane.cpp b/texedit/gui/layout/panes/preview_pane.cpp index 2540aee..9d0744d 100644 --- a/texedit/gui/layout/panes/preview_pane.cpp +++ b/texedit/gui/layout/panes/preview_pane.cpp @@ -21,22 +21,14 @@ namespace te::gui { } PreviewPane::~PreviewPane() { - // delete last loaded document if applicable - if (_document) { - delete _document; - } } void PreviewPane::SetPDFLocation(const wxString &path) { try { - // document is heap-allocated, so replace it here - if (_document) { - delete _document; - } - _document = new pdfr::PDFDocument(path); + _document = std::make_unique(path); // RenderDocument() stores the new document's rendered images into the canvas object to be drawn later - _canvas->RenderDocument(_document); + _canvas->RenderDocument(_document.get()); } catch (except::PDFException *e) { // TODO: show error message in the preview pane. wxLogError("%s", e->what()); diff --git a/texedit/gui/layout/panes/preview_pane.hpp b/texedit/gui/layout/panes/preview_pane.hpp index e6a8976..a962610 100644 --- a/texedit/gui/layout/panes/preview_pane.hpp +++ b/texedit/gui/layout/panes/preview_pane.hpp @@ -9,12 +9,13 @@ #ifndef __texedit__preview_pane_hpp__ #define __texedit__preview_pane_hpp__ -#include -#include - #include "pane_base.hpp" #include "gui/pdf_viewer/pdf_canvas.hpp" +#include +#include +#include + namespace te::pdfr { class PDFDocument; } @@ -30,7 +31,7 @@ namespace te::gui { private: wxBoxSizer *_sizer; - pdfr::PDFDocument *_document{nullptr}; + std::unique_ptr _document{nullptr}; PDFCanvas *_canvas; }; } diff --git a/texedit/gui/main_frame.cpp b/texedit/gui/main_frame.cpp index a96be2e..a953ac9 100644 --- a/texedit/gui/main_frame.cpp +++ b/texedit/gui/main_frame.cpp @@ -24,8 +24,8 @@ namespace te::gui { BuildMenuBar(); // use logger for message redirects - _logger = new util::GlobalLogger(_layout.GetOutputPane()->GetListBox()); - wxLog::SetActiveTarget(_logger); + _logger = std::make_unique(_layout.GetOutputPane()->GetListBox()); + wxLog::SetActiveTarget(_logger.get()); // TODO: temp -- _layout.GetPreviewPane()->SetPDFLocation("HelloWorld.pdf"); @@ -37,7 +37,6 @@ namespace te::gui { MainFrame::~MainFrame() { // reset logging to default behaviour before deleting the logger object wxLog::SetActiveTarget(nullptr); - delete _logger; } void MainFrame::BuildMenuBar() { diff --git a/texedit/gui/main_frame.hpp b/texedit/gui/main_frame.hpp index 015c588..5bf2327 100644 --- a/texedit/gui/main_frame.hpp +++ b/texedit/gui/main_frame.hpp @@ -16,6 +16,7 @@ #include #include +#include namespace te::gui { class MainFrame : public wxFrame { @@ -25,7 +26,7 @@ namespace te::gui { private: LayoutManager _layout; - util::GlobalLogger *_logger; + std::unique_ptr _logger; proc::ProcessManager _proc_mgr; proc::CompilerProcess *_compiler_proc;