summaryrefslogtreecommitdiff
path: root/libs/pbd
diff options
context:
space:
mode:
authorPaul Davis <paul@linuxaudiosystems.com>2006-08-25 01:07:15 +0000
committerPaul Davis <paul@linuxaudiosystems.com>2006-08-25 01:07:15 +0000
commitce234f363e95c38fc92728e520bf5ba240a89aa7 (patch)
tree96ce8c4734bdd564ec1f2ad0c36bc32f0b108204 /libs/pbd
parent7e95f29ce95edf01d6d451f96fae03f3d3451ff8 (diff)
use shared_ptr<> for all region handling
git-svn-id: svn://localhost/ardour2/trunk@852 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/pbd')
-rw-r--r--libs/pbd/pbd/command.h3
-rw-r--r--libs/pbd/pbd/memento_command.h39
-rw-r--r--libs/pbd/pbd/statefuldestructible.h13
-rw-r--r--libs/pbd/pbd/undo.h34
-rw-r--r--libs/pbd/undo.cc78
5 files changed, 128 insertions, 39 deletions
diff --git a/libs/pbd/pbd/command.h b/libs/pbd/pbd/command.h
index cd9bf0e08a..7c367e7462 100644
--- a/libs/pbd/pbd/command.h
+++ b/libs/pbd/pbd/command.h
@@ -22,8 +22,9 @@
#define __lib_pbd_command_h__
#include <pbd/stateful.h>
+#include <pbd/destructible.h>
-class Command : public Stateful
+class Command : public Stateful, public PBD::Destructible
{
public:
virtual ~Command() {}
diff --git a/libs/pbd/pbd/memento_command.h b/libs/pbd/pbd/memento_command.h
index 3a72fc9841..f257e63233 100644
--- a/libs/pbd/pbd/memento_command.h
+++ b/libs/pbd/pbd/memento_command.h
@@ -21,6 +21,10 @@
#ifndef __lib_pbd_memento_command_h__
#define __lib_pbd_memento_command_h__
+#include <iostream>
+using std::cerr;
+using std::endl;
+
#include <pbd/command.h>
#include <pbd/xml++.h>
#include <sigc++/slot.h>
@@ -34,12 +38,22 @@ template <class obj_T>
class MementoCommand : public Command
{
public:
- MementoCommand(XMLNode &state);
- MementoCommand(obj_T &obj,
+ MementoCommand(obj_T &object,
XMLNode *before,
XMLNode *after
)
- : obj(obj), before(before), after(after) {}
+ : obj(object), before(before), after(after) {
+ obj.GoingAway.connect (sigc::mem_fun (*this, &MementoCommand<obj_T>::object_death));
+ }
+ ~MementoCommand () {
+ GoingAway();
+ if (before) {
+ delete before;
+ }
+ if (after) {
+ delete after;
+ }
+ }
void operator() ()
{
if (after)
@@ -60,20 +74,27 @@ class MementoCommand : public Command
else
name = "MementoRedoCommand";
+
XMLNode *node = new XMLNode(name);
- node->add_property("obj_id", obj.id().to_s());
- node->add_property("type_name", typeid(obj).name());
- if (before)
- node->add_child_copy(*before);
- if (after)
- node->add_child_copy(*after);
+ node->add_property("obj_id", obj.id().to_s());
+ node->add_property("type_name", typeid(obj).name());
+
+ if (before)
+ node->add_child_copy(*before);
+ if (after)
+ node->add_child_copy(*after);
return *node;
}
+
protected:
obj_T &obj;
XMLNode *before, *after;
+
+ void object_death () {
+ delete this;
+ }
};
#endif // __lib_pbd_memento_h__
diff --git a/libs/pbd/pbd/statefuldestructible.h b/libs/pbd/pbd/statefuldestructible.h
new file mode 100644
index 0000000000..e78cc4bdaa
--- /dev/null
+++ b/libs/pbd/pbd/statefuldestructible.h
@@ -0,0 +1,13 @@
+#ifndef __pbd_stateful_destructible_h__
+#define __pbd_stateful_destructible_h__
+
+#include <pbd/stateful.h>
+#include <pbd/destructible.h>
+
+namespace PBD {
+class StatefulDestructible : public Stateful, public Destructible
+{
+};
+}
+
+#endif /* __pbd_stateful_destructible_h__ */
diff --git a/libs/pbd/pbd/undo.h b/libs/pbd/pbd/undo.h
index 724e86aaa0..eecd8ae49d 100644
--- a/libs/pbd/pbd/undo.h
+++ b/libs/pbd/pbd/undo.h
@@ -29,9 +29,6 @@
#include <sys/time.h>
#include <pbd/command.h>
-using std::string;
-using std::list;
-
typedef sigc::slot<void> UndoAction;
class UndoTransaction : public Command
@@ -40,10 +37,11 @@ class UndoTransaction : public Command
UndoTransaction ();
UndoTransaction (const UndoTransaction&);
UndoTransaction& operator= (const UndoTransaction&);
+ ~UndoTransaction ();
void clear ();
- void add_command (Command *const);
+ void add_command (Command* const);
void operator() ();
void undo();
@@ -51,10 +49,10 @@ class UndoTransaction : public Command
XMLNode &get_state();
- void set_name (const string& str) {
+ void set_name (const std::string& str) {
_name = str;
}
- const string& name() const { return _name; }
+ const std::string& name() const { return _name; }
void set_timestamp (struct timeval &t) {
_timestamp = t;
@@ -65,26 +63,28 @@ class UndoTransaction : public Command
}
private:
- list<Command*> actions;
- struct timeval _timestamp;
- string _name;
+ std::list<Command*> actions;
+ struct timeval _timestamp;
+ std::string _name;
+ bool clearing;
+ void remove_command (Command* const);
};
class UndoHistory
{
public:
- UndoHistory() {}
+ UndoHistory();
~UndoHistory() {}
- void add (UndoTransaction ut);
+ void add (UndoTransaction* ut);
void undo (unsigned int n);
void redo (unsigned int n);
unsigned long undo_depth() const { return UndoList.size(); }
unsigned long redo_depth() const { return RedoList.size(); }
- string next_undo() const { return (UndoList.empty() ? string("") : UndoList.back().name()); }
- string next_redo() const { return (RedoList.empty() ? string("") : RedoList.back().name()); }
+ std::string next_undo() const { return (UndoList.empty() ? std::string("") : UndoList.back()->name()); }
+ std::string next_redo() const { return (RedoList.empty() ? std::string("") : RedoList.back()->name()); }
void clear ();
void clear_undo ();
@@ -92,9 +92,13 @@ class UndoHistory
XMLNode &get_state();
void save_state();
+
private:
- list<UndoTransaction> UndoList;
- list<UndoTransaction> RedoList;
+ bool clearing;
+ std::list<UndoTransaction*> UndoList;
+ std::list<UndoTransaction*> RedoList;
+
+ void remove (UndoTransaction*);
};
diff --git a/libs/pbd/undo.cc b/libs/pbd/undo.cc
index 6f421de84e..717c355bae 100644
--- a/libs/pbd/undo.cc
+++ b/libs/pbd/undo.cc
@@ -19,26 +19,36 @@
*/
#include <iostream>
+#include <string>
+#include <sstream>
#include <pbd/undo.h>
#include <pbd/xml++.h>
-#include <string>
-#include <sstream>
+
+#include <sigc++/bind.h>
using namespace std;
using namespace sigc;
UndoTransaction::UndoTransaction ()
{
+ clearing = false;
}
UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
{
_name = rhs._name;
+ clearing = false;
clear ();
actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
}
+UndoTransaction::~UndoTransaction ()
+{
+ GoingAway ();
+ clear ();
+}
+
UndoTransaction&
UndoTransaction::operator= (const UndoTransaction& rhs)
{
@@ -52,13 +62,31 @@ UndoTransaction::operator= (const UndoTransaction& rhs)
void
UndoTransaction::add_command (Command *const action)
{
+ action->GoingAway.connect (bind (mem_fun (*this, &UndoTransaction::remove_command), action));
actions.push_back (action);
}
void
+UndoTransaction::remove_command (Command* const action)
+{
+ if (clearing) {
+ return;
+ }
+ actions.remove (action);
+ if (actions.empty()) {
+ delete this;
+ }
+}
+
+void
UndoTransaction::clear ()
{
+ clearing = true;
+ for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
+ delete *i;
+ }
actions.clear ();
+ clearing = false;
}
void
@@ -72,7 +100,6 @@ UndoTransaction::operator() ()
void
UndoTransaction::undo ()
{
- cerr << "Undo " << _name << endl;
for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
(*i)->undo();
}
@@ -81,7 +108,6 @@ UndoTransaction::undo ()
void
UndoTransaction::redo ()
{
- cerr << "Redo " << _name << endl;
(*this)();
}
@@ -103,10 +129,29 @@ XMLNode &UndoTransaction::get_state()
return *node;
}
+UndoHistory::UndoHistory ()
+{
+ clearing = false;
+}
+
void
-UndoHistory::add (UndoTransaction ut)
+UndoHistory::add (UndoTransaction* const ut)
{
+ ut->GoingAway.connect (bind (mem_fun (*this, &UndoHistory::remove), ut));
UndoList.push_back (ut);
+
+ /* we are now owners of the transaction */
+}
+
+void
+UndoHistory::remove (UndoTransaction* const ut)
+{
+ if (clearing) {
+ return;
+ }
+
+ UndoList.remove (ut);
+ RedoList.remove (ut);
}
void
@@ -116,9 +161,9 @@ UndoHistory::undo (unsigned int n)
if (UndoList.size() == 0) {
return;
}
- UndoTransaction ut = UndoList.back ();
+ UndoTransaction* ut = UndoList.back ();
UndoList.pop_back ();
- ut.undo ();
+ ut->undo ();
RedoList.push_back (ut);
}
}
@@ -130,9 +175,9 @@ UndoHistory::redo (unsigned int n)
if (RedoList.size() == 0) {
return;
}
- UndoTransaction ut = RedoList.back ();
+ UndoTransaction* ut = RedoList.back ();
RedoList.pop_back ();
- ut.redo ();
+ ut->redo ();
UndoList.push_back (ut);
}
}
@@ -140,29 +185,34 @@ UndoHistory::redo (unsigned int n)
void
UndoHistory::clear_redo ()
{
+ clearing = true;
RedoList.clear ();
+ clearing = false;
}
void
UndoHistory::clear_undo ()
{
+ clearing = true;
UndoList.clear ();
+ clearing = false;
}
void
UndoHistory::clear ()
{
- RedoList.clear ();
- UndoList.clear ();
+ clear_undo ();
+ clear_redo ();
}
XMLNode & UndoHistory::get_state()
{
XMLNode *node = new XMLNode ("UndoHistory");
- list<UndoTransaction>::iterator it;
- for (it=UndoList.begin(); it != UndoList.end(); it++)
- node->add_child_nocopy(it->get_state());
+ list<UndoTransaction*>::iterator it;
+ for (it = UndoList.begin(); it != UndoList.end(); it++) {
+ node->add_child_nocopy((*it)->get_state());
+ }
return *node;
}