00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef TSE3_CMD_COMMAND_H
00018 #define TSE3_CMD_COMMAND_H
00019
00020 #include <string>
00021
00022 namespace TSE3
00023 {
00053 namespace Cmd
00054 {
00067 class Command
00068 {
00069 public:
00070
00071 virtual ~Command() {}
00072
00081 void execute()
00082 {
00083 if (!_done)
00084 {
00085 executeImpl();
00086 _done = true;
00087 }
00088 }
00089
00096 void undo()
00097 {
00098 if (_done && _undoable)
00099 {
00100 undoImpl();
00101 _done = false;
00102 }
00103 }
00104
00111 const std::string &title() const { return _title; }
00112
00116 bool undoable() const { return _undoable; }
00117
00124 bool done() const { return _done; }
00125
00126 protected:
00127
00136 Command(const std::string &title, bool undoable = true)
00137 : _title(title), _undoable(undoable), _done(false)
00138 {}
00139
00144 virtual void executeImpl() = 0;
00145
00150 virtual void undoImpl() = 0;
00151
00156 void setTitle(const std::string &title) { _title = title; }
00157
00158 private:
00159
00160 std::string _title;
00161 bool _undoable;
00162 bool _done;
00163 };
00164
00182 template <class cls_type, class arg_type, class param_type,
00183 param_type (cls_type::*getmfn)() const,
00184 void (cls_type::*setmfn)(param_type)>
00185 class VariableSetCommand : public Command
00186 {
00187 public:
00188 VariableSetCommand(cls_type *cls, param_type arg,
00189 const std::string &title)
00190 : Command(title), cls(cls), arg(arg) {}
00191 virtual ~VariableSetCommand() {}
00192 protected:
00196 virtual void executeImpl()
00197 {
00198 old = (cls->*getmfn)();
00199 (cls->*setmfn)(arg);
00200 }
00204 virtual void undoImpl()
00205 {
00206 (cls->*setmfn)(old);
00207 }
00208 private:
00209 cls_type *cls;
00210 arg_type arg;
00211 arg_type old;
00212 };
00213 }
00214 }
00215
00216 #endif