(Hopefully) clarify operator= and copy construction behaviour of the Property hierarc...
[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 just used for MIDI automation;
38  *  when MIDI automation is edited, undo records are written for the AutomationList being
39  *  changed.  However this AutomationList is a temporary structure, built by a MidiModel,
40  *  which doesn't get written to the session file.  Hence we need to be able to go from
41  *  a MidiSource and Parameter to an AutomationList.  This Binder mechanism allows this
42  *  through MidiAutomationListBinder; the undo record stores the source and parameter,
43  *  and these are bound to an AutomationList by the Binder.
44  */
45 template <class obj_T>
46 class MementoCommandBinder : public PBD::Destructible
47 {
48 public:
49         /** @return Stateful object to operate on */
50         virtual obj_T* get () = 0;
51
52         /** Add our own state to an XMLNode */
53         virtual void add_state (XMLNode *) = 0;
54 };
55
56 /** A simple MementoCommandBinder which binds directly to an object */
57 template <class obj_T>
58 class SimpleMementoCommandBinder : public MementoCommandBinder<obj_T>
59 {
60 public:
61         SimpleMementoCommandBinder (obj_T& o)
62                 : _object (o)
63         {
64                 _object.Destroyed.connect_same_thread (_object_death_connection, boost::bind (&SimpleMementoCommandBinder::object_died, this));
65         }
66
67         obj_T* get () {
68                 return &_object;
69         }
70
71         void add_state (XMLNode* node) {
72                 node->add_property ("obj_id", _object.id().to_s());
73         }
74         
75         void object_died () {
76                 /* The object we are binding died, so drop references to ourselves */
77                 this->drop_references ();
78         }
79
80 private:
81         obj_T& _object;
82         PBD::ScopedConnection _object_death_connection;
83 };
84
85 /** This command class is initialized with before and after mementos 
86  * (from Stateful::get_state()), so undo becomes restoring the before
87  * memento, and redo is restoring the after memento.
88  */
89 template <class obj_T>
90 class MementoCommand : public Command
91 {
92 public:
93         MementoCommand (obj_T& a_object, XMLNode* a_before, XMLNode* a_after) 
94                 : _binder (new SimpleMementoCommandBinder<obj_T> (a_object)), before (a_before), after (a_after)
95         {
96                 /* The binder's object died, so we must die */
97                 _binder->DropReferences.connect_same_thread (_binder_death_connection, boost::bind (&MementoCommand::binder_dying, this));
98         }
99
100         MementoCommand (MementoCommandBinder<obj_T>* b, XMLNode* a_before, XMLNode* a_after) 
101                 : _binder (b), before (a_before), after (a_after)
102         {
103                 /* The binder's object died, so we must die */
104                 _binder->DropReferences.connect_same_thread (_binder_death_connection, boost::bind (&MementoCommand::binder_dying, this));
105         }
106         
107         ~MementoCommand () {
108                 drop_references ();
109                 delete before;
110                 delete after;
111                 delete _binder;
112         }
113
114         void binder_dying () {
115                 delete this;
116         }
117
118         void operator() () {
119                 if (after) {
120                         _binder->get()->set_state(*after, Stateful::current_state_version); 
121                 }
122         }
123
124         void undo() { 
125                 if (before) {
126                         _binder->get()->set_state(*before, Stateful::current_state_version); 
127                 }
128         }
129
130         virtual XMLNode &get_state() {
131                 std::string name;
132                 if (before && after) {
133                         name = "MementoCommand";
134                 } else if (before) {
135                         name = "MementoUndoCommand";
136                 } else {
137                         name = "MementoRedoCommand";
138                 }
139
140                 XMLNode* node = new XMLNode(name);
141                 _binder->add_state (node);
142                 
143                 node->add_property("type_name", demangled_name (*_binder->get()));
144
145                 if (before) {
146                         node->add_child_copy(*before);
147                 }
148                 
149                 if (after) {
150                         node->add_child_copy(*after);
151                 }
152
153                 return *node;
154         }
155
156 protected:
157         MementoCommandBinder<obj_T>* _binder;
158         XMLNode* before;
159         XMLNode* after;
160         PBD::ScopedConnection _binder_death_connection;
161 };
162
163 #endif // __lib_pbd_memento_h__