r168@gandalf: fugalh | 2006-07-11 16:29:22 -0600
I just had an epiphany. I tried so many ways to make saving function name and
args work, it never occured to me that you could just as easily save undo
information as a pair of mementos, even in the Command-based structure we
agreed on.
Since many (read: almost all) existing undo commands take this form:
begin_reversible_command (_("change fade in length"));
session->add_undo (arv->region.get_memento());
arv->region.set_fade_in_length (fade_length);
session->add_redo_no_execute (arv->region.get_memento());
commit_reversible_command ();
We are already doing the save a memento before and after work. All we need to
do is instantiate an appropriate instance of MementoCommand. So the above
becomes:
begin_reversible_command (_("change fade in length"));
MementoCommand<arv_region_t, arv_region_memento_t> before, after;
before = arv->region.get_memento();
arv->region.set_fade_in_length (fade_length);
after = arv->region.get_memento();
session->add_command(arv->region, before, after);
commit_reversible_command ();
(With apologies for being too lazy to go look up what arv_region_t and
arv_region_memento_t are)
Note that the true command approach is still possible, and encouraged (both by
dictate and design).
git-svn-id: svn://localhost/ardour2/branches/undo@680 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
@@ -24,73 +24,26 @@
|
||||
#include <pbd/command.h>
|
||||
#include <sigc++/slot.h>
|
||||
|
||||
#define MEMENTO_COMMAND(obj_T, mem_T, obj, meth) \
|
||||
(MementoCommand<(obj_T),(mem_T)>((obj),sigc::mem_fun((obj),&(meth)),#meth))
|
||||
|
||||
#define MEMENTO_COMMAND_1(obj_T, mem_T, obj, meth, arg1) \
|
||||
(MementoCommand<(obj_T),(mem_T)>((obj),\
|
||||
sigc::bind(sigc::mem_fun((obj),\
|
||||
&(meth)),\
|
||||
arg1),#meth))
|
||||
|
||||
#define MEMENTO_COMMAND_2(obj_T, mem_T, obj, meth, arg1, arg2) \
|
||||
(MementoCommand<(obj_T),(mem_T)>((obj),\
|
||||
sigc::bind(sigc::mem_fun((obj),\
|
||||
&(meth)),\
|
||||
arg1, arg2),#meth))
|
||||
|
||||
#define MEMENTO_COMMAND_3(obj_T, mem_T, obj, meth, arg1, arg2, arg3) \
|
||||
(MementoCommand<(obj_T),(mem_T)>((obj),\
|
||||
sigc::bind(sigc::mem_fun((obj),\
|
||||
&(meth)),\
|
||||
arg1, arg2, arg3),#meth))
|
||||
|
||||
|
||||
template <class obj_T, class mem_T>
|
||||
class MementoCommand : public Command
|
||||
{
|
||||
public:
|
||||
MementoCommand(obj_T obj,
|
||||
sigc::slot<void> action,
|
||||
std::string key
|
||||
std::list<Serializable *> args
|
||||
MementoCommand(obj_T &obj,
|
||||
mem_T before,
|
||||
mem_T after
|
||||
)
|
||||
: obj(obj), action(action), key(key), args(args), memento(obj.get_memento()) {}
|
||||
MementoCommand(obj_T obj,
|
||||
sigc::slot<void> action,
|
||||
std::string key
|
||||
Serializable *arg1 = 0,
|
||||
Serializable *arg2 = 0,
|
||||
Serializable *arg3 = 0
|
||||
)
|
||||
: obj(obj), action(action), key(key), memento(obj.get_memento())
|
||||
{
|
||||
if (arg1 == 0)
|
||||
return;
|
||||
args.push_back(arg1);
|
||||
|
||||
if (arg2 == 0)
|
||||
return;
|
||||
args.push_back(arg2);
|
||||
|
||||
if (arg3 == 0)
|
||||
return;
|
||||
args.push_back(arg3);
|
||||
}
|
||||
void operator() () { action(); }
|
||||
void undo() { obj.set_memento(memento); }
|
||||
: obj(obj), before(before), after(after) {}
|
||||
void operator() () { obj.set_memento(after); }
|
||||
void undo() { obj.set_memento(before); }
|
||||
virtual XMLNode &serialize()
|
||||
{
|
||||
// obj.id
|
||||
// key
|
||||
// args
|
||||
// key is "MementoCommand" or something
|
||||
// before and after mementos
|
||||
}
|
||||
protected:
|
||||
obj_T &obj;
|
||||
mem_T memento;
|
||||
sigc::slot<void> action;
|
||||
std::string key;
|
||||
std::list<Serializable*> args;
|
||||
mem_T before, after;
|
||||
};
|
||||
|
||||
#endif // __lib_pbd_memento_h__
|
||||
|
||||
Reference in New Issue
Block a user