Merge branch 'master' into cairocanvas
[ardour.git] / libs / pbd / pbd / memento_command.h
1 /* 
2     Copyright (C) 2006 Paul Davis
3     Author: Hans Fugal
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __lib_pbd_memento_command_h__
22 #define __lib_pbd_memento_command_h__
23
24 #include <iostream>
25
26 #include "pbd/command.h"
27 #include "pbd/stacktrace.h"
28 #include "pbd/xml++.h"
29 #include "pbd/demangle.h"
30
31 #include <sigc++/slot.h>
32 #include <typeinfo>
33
34 /** A class that can return a Stateful object which is the subject of a MementoCommand.
35  *
36  *  The existence of this class means that the undo record can refer to objects which
37  *  don't exist in the session file.  Currently this is used for
38  *
39  *  1.  MIDI automation; when MIDI automation is edited, undo records are
40  *  written for the AutomationList being changed.  However this AutomationList
41  *  is a temporary structure, built by a MidiModel, which doesn't get written
42  *  to the session file.  Hence we need to be able to go from a MidiSource and
43  *  Parameter to an AutomationList.  This Binder mechanism allows this through
44  *  MidiAutomationListBinder; the undo record stores the source and parameter,
45  *  and these are bound to an AutomationList by the Binder.
46  *
47  *  2.  Crossfades; unlike regions, these are completely removed from a session
48  *  when they are deleted.  This means that the undo record can contain
49  *  references to non-existant crossfades.  To get around this, CrossfadeBinder
50  *  can do `just-in-time' binding from the crossfade ID.
51  */
52 template <class obj_T>
53 class MementoCommandBinder : public PBD::Destructible
54 {
55 public:
56         /** @return Stateful object to operate on */
57         virtual obj_T* get () const = 0;
58
59         /** @return Name of our type */
60         virtual std::string type_name () const {
61                 return PBD::demangled_name (*get ());
62         }
63
64         /** Add our own state to an XMLNode */
65         virtual void add_state (XMLNode *) = 0;
66 };
67
68 /** A simple MementoCommandBinder which binds directly to an object */
69 template <class obj_T>
70 class SimpleMementoCommandBinder : public MementoCommandBinder<obj_T>
71 {
72 public:
73         SimpleMementoCommandBinder (obj_T& o)
74                 : _object (o)
75         {
76                 _object.Destroyed.connect_same_thread (_object_death_connection, boost::bind (&SimpleMementoCommandBinder::object_died, this));
77         }
78
79         obj_T* get () const {
80                 return &_object;
81         }
82
83         void add_state (XMLNode* node) {
84                 node->add_property ("obj_id", _object.id().to_s());
85         }
86         
87         void object_died () {
88                 /* The object we are binding died, so drop references to ourselves */
89                 this->drop_references ();
90         }
91
92 private:
93         obj_T& _object;
94         PBD::ScopedConnection _object_death_connection;
95 };
96
97 /** This command class is initialized with before and after mementos 
98  * (from Stateful::get_state()), so undo becomes restoring the before
99  * memento, and redo is restoring the after memento.
100  */
101 template <class obj_T>
102 class MementoCommand : public Command
103 {
104 public:
105         MementoCommand (obj_T& a_object, XMLNode* a_before, XMLNode* a_after) 
106                 : _binder (new SimpleMementoCommandBinder<obj_T> (a_object)), before (a_before), after (a_after)
107         {
108                 /* The binder's object died, so we must die */
109                 _binder->DropReferences.connect_same_thread (_binder_death_connection, boost::bind (&MementoCommand::binder_dying, this));
110         }
111
112         MementoCommand (MementoCommandBinder<obj_T>* b, XMLNode* a_before, XMLNode* a_after) 
113                 : _binder (b), before (a_before), after (a_after)
114         {
115                 /* The binder's object died, so we must die */
116                 _binder->DropReferences.connect_same_thread (_binder_death_connection, boost::bind (&MementoCommand::binder_dying, this));
117         }
118         
119         ~MementoCommand () {
120                 drop_references ();
121                 delete before;
122                 delete after;
123                 delete _binder;
124         }
125
126         void binder_dying () {
127                 delete this;
128         }
129
130         void operator() () {
131                 if (after) {
132                         _binder->get()->set_state(*after, Stateful::current_state_version); 
133                 }
134         }
135
136         void undo() { 
137                 if (before) {
138                         _binder->get()->set_state(*before, Stateful::current_state_version); 
139                 }
140         }
141
142         virtual XMLNode &get_state() {
143                 std::string name;
144                 if (before && after) {
145                         name = "MementoCommand";
146                 } else if (before) {
147                         name = "MementoUndoCommand";
148                 } else {
149                         name = "MementoRedoCommand";
150                 }
151
152                 XMLNode* node = new XMLNode(name);
153                 _binder->add_state (node);
154                 
155                 node->add_property ("type_name", _binder->type_name ());
156
157                 if (before) {
158                         node->add_child_copy(*before);
159                 }
160                 
161                 if (after) {
162                         node->add_child_copy(*after);
163                 }
164
165                 return *node;
166         }
167
168 protected:
169         MementoCommandBinder<obj_T>* _binder;
170         XMLNode* before;
171         XMLNode* after;
172         PBD::ScopedConnection _binder_death_connection;
173 };
174
175 #endif // __lib_pbd_memento_h__