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