[niepce] undo: use shared_ptr<> of undo instead of raw pointers



commit 94a9c25a603bc9144605577b73d02ad621a1d40e
Author: Hubert Figuière <hub figuiere net>
Date:   Sat May 13 15:39:52 2017 -0400

    undo: use shared_ptr<> of undo instead of raw pointers

 src/fwk/toolkit/application.cpp       |    4 ++--
 src/fwk/toolkit/application.hpp       |    2 +-
 src/fwk/toolkit/undo.cpp              |   26 +++++++-------------------
 src/fwk/toolkit/undo.hpp              |   27 +++++++++++++--------------
 src/niepce/ui/dialogs/editlabels.cpp  |    2 +-
 src/niepce/ui/selectioncontroller.cpp |    5 +++--
 6 files changed, 27 insertions(+), 39 deletions(-)
---
diff --git a/src/fwk/toolkit/application.cpp b/src/fwk/toolkit/application.cpp
index c57e6aa..270fac7 100644
--- a/src/fwk/toolkit/application.cpp
+++ b/src/fwk/toolkit/application.cpp
@@ -215,9 +215,9 @@ void Application::on_about()
     dlg.run();
 }
 
-UndoTransaction * Application::begin_undo(const std::string & label)
+std::shared_ptr<UndoTransaction> Application::begin_undo(const std::string & label)
 {
-    auto undo = new fwk::UndoTransaction(label);
+    auto undo = std::make_shared<fwk::UndoTransaction>(label);
     undo_history().add(undo);
     return undo;
 }
diff --git a/src/fwk/toolkit/application.hpp b/src/fwk/toolkit/application.hpp
index d49a023..78242ae 100644
--- a/src/fwk/toolkit/application.hpp
+++ b/src/fwk/toolkit/application.hpp
@@ -71,7 +71,7 @@ public:
 
     UndoHistory & undo_history()
         { return m_undo; }
-    UndoTransaction * begin_undo(const std::string & label);
+    std::shared_ptr<UndoTransaction> begin_undo(const std::string & label);
 
     // Module management
     /** @return the module manager
diff --git a/src/fwk/toolkit/undo.cpp b/src/fwk/toolkit/undo.cpp
index 442131a..cac6d4e 100644
--- a/src/fwk/toolkit/undo.cpp
+++ b/src/fwk/toolkit/undo.cpp
@@ -35,12 +35,10 @@ UndoTransaction::UndoTransaction(const std::string & n)
 
 UndoTransaction::~UndoTransaction()
 {
-    std::for_each(m_operations.begin(), m_operations.end(),
-                  &boost::checked_delete<Command>);
 }
 
 
-void UndoTransaction::add(Command * cmd)
+void UndoTransaction::add(const std::shared_ptr<Command>& cmd)
 {
     m_operations.push_back(cmd);
 }
@@ -66,14 +64,12 @@ void UndoTransaction::redo()
 UndoHistory::~UndoHistory()
 {
     // DO NOT CALL UndoHistory::clear() !!!
-    clear(m_undos);
-    clear(m_redos);
 }
 
-void UndoHistory::add(UndoTransaction* t)
+void UndoHistory::add(const std::shared_ptr<UndoTransaction>& t)
 {
     m_undos.push_front(t);
-    clear(m_redos);
+    m_redos.clear();
 
     signal_changed();
 }
@@ -82,7 +78,7 @@ void UndoHistory::undo()
 {
     DBG_OUT("run undo history");
     if(!m_undos.empty()) {
-        UndoTransaction * t = m_undos.front();
+        std::shared_ptr<UndoTransaction> t = m_undos.front();
         if(t) {
             t->undo();
             m_undos.pop_front();
@@ -96,7 +92,7 @@ void UndoHistory::redo()
 {
     DBG_OUT("run redo history");
     if(!m_redos.empty()) {
-        UndoTransaction * t = m_redos.front();
+        std::shared_ptr<UndoTransaction> t = m_redos.front();
         if(t) {
             t->redo();
             m_redos.pop_front();
@@ -116,7 +112,7 @@ void UndoHistory::clear()
 std::string UndoHistory::next_undo() const
 {
     if(!m_undos.empty()) {
-        UndoTransaction * t = m_undos.front();
+        auto t = m_undos.front();
         if(t) {
             return t->name();
         }
@@ -127,7 +123,7 @@ std::string UndoHistory::next_undo() const
 std::string UndoHistory::next_redo() const
 {
     if(!m_redos.empty()) {
-        UndoTransaction * t = m_redos.front();
+        auto t = m_redos.front();
         if(t) {
             return t->name();
         }
@@ -135,14 +131,6 @@ std::string UndoHistory::next_redo() const
     return "";
 }
 
-       
-void UndoHistory::clear(std::list<UndoTransaction*> & l)
-{
-    std::for_each(l.begin(), l.end(),
-                  &boost::checked_delete<UndoTransaction>);
-    l.clear();
-}
-
 }
 
 /*
diff --git a/src/fwk/toolkit/undo.hpp b/src/fwk/toolkit/undo.hpp
index 3f96dd3..eae811d 100644
--- a/src/fwk/toolkit/undo.hpp
+++ b/src/fwk/toolkit/undo.hpp
@@ -24,6 +24,7 @@
 #include <list>
 #include <stack>
 #include <string>
+#include <memory>
 
 #include <sigc++/signal.h>
 #include <sigc++/trackable.h>
@@ -43,8 +44,9 @@ public:
     ~UndoTransaction();
 
     template <typename _RetType>
-    Command *new_command(const typename CommandWithArg<_RetType>::RedoFunction &,
-                         const typename CommandWithArg<_RetType>::UndoFunction &);
+    std::shared_ptr<Command> new_command(
+        const typename CommandWithArg<_RetType>::RedoFunction&,
+        const typename CommandWithArg<_RetType>::UndoFunction&);
     void undo();
     void redo();
     /** execute the transaction after adding it. (calls %undo) */
@@ -54,17 +56,18 @@ public:
         { return m_name; }
 protected:
     /** add the command. Use %new_command instead */
-    void add(Command *);
+    void add(const std::shared_ptr<Command>&);
 private:
-    std::list<Command *> m_operations;
+    std::list<std::shared_ptr<Command>> m_operations;
     std::string m_name;
 };
 
 template <typename _ArgType>
-Command *UndoTransaction::new_command(const typename CommandWithArg<_ArgType>::RedoFunction & _redo,
-                                      const typename CommandWithArg<_ArgType>::UndoFunction & _undo)
+std::shared_ptr<Command> UndoTransaction::new_command(
+    const typename CommandWithArg<_ArgType>::RedoFunction & _redo,
+    const typename CommandWithArg<_ArgType>::UndoFunction & _undo)
 {
-    Command *cmd = new CommandWithArg<_ArgType>(_redo, _undo);
+    auto cmd = std::make_shared<CommandWithArg<_ArgType>>(_redo, _undo);
     add(cmd);
     return cmd;
 }
@@ -80,7 +83,7 @@ public:
     ~UndoHistory();
 
     /** the history becomes owner */
-    void add(UndoTransaction*);
+    void add(const std::shared_ptr<UndoTransaction>&);
     void undo();
     void redo();
     void clear();
@@ -94,14 +97,10 @@ public:
     // called when the undo history change.
     sigc::signal<void> signal_changed;
 private:
-    void clear(std::list<UndoTransaction*> & l);
-
-    std::list<UndoTransaction*> m_undos;
-    std::list<UndoTransaction*> m_redos;
+    std::list<std::shared_ptr<UndoTransaction>> m_undos;
+    std::list<std::shared_ptr<UndoTransaction>> m_redos;
 };
 
-       
-
 }
 
 #endif
diff --git a/src/niepce/ui/dialogs/editlabels.cpp b/src/niepce/ui/dialogs/editlabels.cpp
index bf93c21..1634d44 100644
--- a/src/niepce/ui/dialogs/editlabels.cpp
+++ b/src/niepce/ui/dialogs/editlabels.cpp
@@ -95,7 +95,7 @@ void EditLabels::label_colour_changed(size_t idx)
 
 void EditLabels::update_labels(int /*response*/)
 {
-    fwk::UndoTransaction *undo = nullptr;
+    std::shared_ptr<fwk::UndoTransaction> undo;
     for(size_t i = 0; i < 5; i++) {
         if(m_status[i]) {
             bool has_label = m_labels.size() > i;
diff --git a/src/niepce/ui/selectioncontroller.cpp b/src/niepce/ui/selectioncontroller.cpp
index 81729bd..2220d46 100644
--- a/src/niepce/ui/selectioncontroller.cpp
+++ b/src/niepce/ui/selectioncontroller.cpp
@@ -196,7 +196,7 @@ bool SelectionController::_set_metadata(const std::string & undo_label,
                                         fwk::PropertyIndex meta,
                                         int old_value, int new_value)
 {
-    fwk::UndoTransaction *undo = fwk::Application::app()->begin_undo(undo_label);
+    std::shared_ptr<fwk::UndoTransaction> undo = fwk::Application::app()->begin_undo(undo_label);
     undo->new_command<void>(
         std::bind(&libraryclient::LibraryClient::setMetadata,
                     getLibraryClient(), file->id(), meta, fwk::PropertyValue(new_value)),
@@ -316,7 +316,8 @@ void SelectionController::move_to_trash()
         if(iter) {
             eng::LibFile::Ptr file = (*iter)[m_imageliststore->columns().m_libfile];
             eng::library_id_t from_folder = file->folderId();
-            fwk::UndoTransaction *undo = fwk::Application::app()->begin_undo(_("Move to Trash"));
+            std::shared_ptr<fwk::UndoTransaction> undo =
+                fwk::Application::app()->begin_undo(_("Move to Trash"));
             undo->new_command<void>(
                 std::bind(&libraryclient::LibraryClient::moveFileToFolder,
                             getLibraryClient(), selection, from_folder, trash_folder),


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]