r162@gandalf: fugalh | 2006-06-30 19:30:58 -0600
[ardour.git] / libs / pbd3 / 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 <sigc++/slot.h>
26
27 #define MEMENTO_COMMAND(obj_T, mem_T, obj, meth) \
28     (MementoCommand<(obj_T),(mem_T)>((obj),sigc::mem_fun((obj),&(meth)),#meth))
29
30 #define MEMENTO_COMMAND_1(obj_T, mem_T, obj, meth, arg1) \
31     (MementoCommand<(obj_T),(mem_T)>((obj),\
32                                      sigc::bind(sigc::mem_fun((obj),\
33                                                               &(meth)),\
34                                                 arg1),#meth))
35
36 #define MEMENTO_COMMAND_2(obj_T, mem_T, obj, meth, arg1, arg2) \
37     (MementoCommand<(obj_T),(mem_T)>((obj),\
38                                      sigc::bind(sigc::mem_fun((obj),\
39                                                               &(meth)),\
40                                                 arg1, arg2),#meth))
41
42 #define MEMENTO_COMMAND_3(obj_T, mem_T, obj, meth, arg1, arg2, arg3) \
43     (MementoCommand<(obj_T),(mem_T)>((obj),\
44                                      sigc::bind(sigc::mem_fun((obj),\
45                                                               &(meth)),\
46                                                 arg1, arg2, arg3),#meth))
47
48
49 template <class obj_T, class mem_T>
50 class MementoCommand : public Command
51 {
52     public:
53         MementoCommand(obj_T obj, 
54                        sigc::slot<void> action,
55                        std::string key
56                        std::list<Serializable *> args
57                        ) 
58             : obj(obj), action(action), key(key), args(args), memento(obj.get_memento()) {}
59         MementoCommand(obj_T obj, 
60                        sigc::slot<void> action,
61                        std::string key
62                        Serializable *arg1 = 0,
63                        Serializable *arg2 = 0,
64                        Serializable *arg3 = 0
65                        ) 
66             : obj(obj), action(action), key(key), memento(obj.get_memento())
67         {
68             if (arg1 == 0)
69                 return;
70             args.push_back(arg1);
71
72             if (arg2 == 0)
73                 return;
74             args.push_back(arg2);
75
76             if (arg3 == 0)
77                 return;
78             args.push_back(arg3);
79         }
80         void operator() () { action(); }
81         void undo() { obj.set_memento(memento); }
82         virtual XMLNode &serialize() 
83         {
84             // obj.id
85             // key
86             // args
87         }
88     protected:
89         obj_T &obj;
90         mem_T memento;
91         sigc::slot<void> action;
92         std::string key;
93         std::list<Serializable*> args;
94 };
95
96 #endif // __lib_pbd_memento_h__