Re: undo/redo algo
call_me_anything wrote:
One more doubt here :
We plan to support multiple changes here like
"change the name of all pets starting with abc to badpets_<original-
name>"
And the application expects all such commands by means of a string
input.
It will parse the string and find out what to do.
So if the user says
change all "abc" to "ABC"
then says
change all "def" to "ABC"
If you want to make multiple changes seem atomic I think I'd do
something like making another implementation of the Command interface
that just stores a collection of commands, and then calls their
apply()/undo() methods from its own apply()/undo() methods:
class CommandCollection : public Command {
std::vector<Command*> cmds;
public:
virtual void apply() {
//for each Command* in cmds call apply()
}
virtual void undo() {
//for each Command* in cmds call undo()
}
}
And then asks to undo, how does we keep track of undoing only those
ABCs which came from def.
I guess all the editing applications do such kind of things (for eg.
notepad and Vi)
How do they handle it efficiently without consuming huge memory or
disk space ?
Then when you get the command "change all def to abc" you do the
following pseudo code:
1) find all the nodes with def you want to change to abc.
2) create a new CommandColletion.
3) for each node you decided you want to change create a
RenameNodeCommand instance, and add the RenameNodeCommand instance to
the CommandCollection.
4) push the CommandCollection you created onto your undo stack and call
apply() on the CommandCollection to actually make the changes.
Then when you get your next command e.g. "change all ABC to abc" you
repeat the above process. After doing that you will now have two
CommandCollection objects on your undo stack that when you pop them off
and call undo() on them will return the state to how it was before those
commands were executed...
Alan