Re: undo/redo algo
call_me_anything wrote:
I mean the text on "command pattern" was just too idealistic and I am
unable to relate it closely to my problem.
Any light shedding would be helpful. :)
Basically you're going to want an interface that every action uses, e.g:
class Command {
public:
virtual void apply() = 0;
virtual void undo() = 0;
};
You'll also need two stacks - one stack to store things that can have
been done, and can be undone in the future, and another one to store
things that have been undone, and can be re-done in the future.
Then you'll want to implement the Command interface we defined
previously for each action, e.g. for re-naming a node we want to store
the old name so that our undo() method can set the name back to what it
was previously. We also need to save the new name in the command so that
apply() can set the new name when it gets called.
class RenameNodeCommand : public Command {
public:
virtual void apply() {
n->name = new_name;
}
virtual void undo() {
n->name = old_name;
}
private:
Node *n;
const std::string new_name;
const std::string old_name;
};
Then you just need to orchestrate it such that every time the user
clicks/presses/types a new name an instance of RenameNodeCommand is
created to perform this action and is saved on the undo stack.
Obviously this is a slightly simplified example, but hopefully it shows
the basic principle...
Alan