Merged with trunk R920.
[ardour.git] / libs / pbd / pbd / memento_command.h
1 /* 
2    Copyright (C) 2006 Hans Fugal & Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id: /local/undo/libs/pbd3/pbd/undo.h 132 2006-06-29T18:45:16.609763Z fugalh  $
19 */
20
21 #ifndef __lib_pbd_memento_command_h__
22 #define __lib_pbd_memento_command_h__
23
24 #include <iostream>
25 using std::cerr;
26 using std::endl;
27
28 #include <pbd/command.h>
29 #include <pbd/xml++.h>
30 #include <sigc++/slot.h>
31 #include <typeinfo>
32
33 /** This command class is initialized with before and after mementos 
34  * (from Stateful::get_state()), so undo becomes restoring the before
35  * memento, and redo is restoring the after memento.
36  */
37 template <class obj_T>
38 class MementoCommand : public Command
39 {
40     public:
41         MementoCommand(obj_T &object, 
42                        XMLNode *before,
43                        XMLNode *after
44                        ) 
45             : obj(object), before(before), after(after) {
46                 obj.GoingAway.connect (sigc::mem_fun (*this, &MementoCommand<obj_T>::object_death));
47         }
48         ~MementoCommand () {
49                 GoingAway();
50                 if (before) {
51                         delete before;
52                 }
53                 if (after) {
54                         delete after;
55                 }
56         }
57         void operator() () 
58         {
59             if (after)
60                 obj.set_state(*after); 
61         }
62         void undo() 
63         { 
64             if (before)
65                 obj.set_state(*before); 
66         }
67         virtual XMLNode &get_state() 
68         {
69             string name;
70             if (before && after)
71                 name = "MementoCommand";
72             else if (before)
73                 name = "MementoUndoCommand";
74             else
75                 name = "MementoRedoCommand";
76
77             
78             XMLNode *node = new XMLNode(name);
79
80             node->add_property("obj_id", obj.id().to_s());
81             node->add_property("type_name", typeid(obj).name());
82             
83             if (before)
84                     node->add_child_copy(*before);
85             if (after)
86                     node->add_child_copy(*after);
87
88             return *node;
89         }
90
91     protected:
92         obj_T &obj;
93         XMLNode *before, *after;
94
95         void object_death () {
96                 delete this;
97         }
98 };
99
100 #endif // __lib_pbd_memento_h__