Merged with trunk R1141
[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/stacktrace.h>
30 #include <pbd/xml++.h>
31 #include <pbd/shiva.h>
32
33 #include <sigc++/slot.h>
34 #include <typeinfo>
35
36 /** This command class is initialized with before and after mementos 
37  * (from Stateful::get_state()), so undo becomes restoring the before
38  * memento, and redo is restoring the after memento.
39  */
40
41 template <class obj_T>
42 class MementoCommand : public Command
43 {
44     public:
45         MementoCommand(obj_T &object, 
46                        XMLNode *before,
47                        XMLNode *after
48                        ) 
49             : obj(object), before(before), after(after) {
50                 /* catch destruction of the object */
51                 new PBD::Shiva<obj_T,MementoCommand<obj_T> > (object, *this);
52         }
53
54         ~MementoCommand () {
55                 GoingAway();
56                 if (before) {
57                         delete before;
58                 }
59                 if (after) {
60                         delete after;
61                 }
62         }
63         void operator() () 
64         {
65             if (after)
66                 obj.set_state(*after); 
67         }
68         void undo() 
69         { 
70             if (before)
71                 obj.set_state(*before); 
72         }
73         virtual XMLNode &get_state() 
74         {
75             string name;
76             if (before && after)
77                 name = "MementoCommand";
78             else if (before)
79                 name = "MementoUndoCommand";
80             else
81                 name = "MementoRedoCommand";
82
83             
84             XMLNode *node = new XMLNode(name);
85
86             node->add_property("obj_id", obj.id().to_s());
87             node->add_property("type_name", typeid(obj).name());
88             
89             if (before)
90                     node->add_child_copy(*before);
91             if (after)
92                     node->add_child_copy(*after);
93
94             return *node;
95         }
96
97     protected:
98         obj_T &obj;
99         XMLNode *before, *after;
100 };
101
102 #endif // __lib_pbd_memento_h__