Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

/home/pete/Work/TSE3.svn/tse3/src/tse3/cmd/Command.h

Go to the documentation of this file.
00001 /*
00002  * @(#)cmd/Command.h 3.00 10 June 1999
00003  *
00004  * Copyright (c) 2000 Pete Goodliffe (pete@cthree.org)
00005  *
00006  * This file is part of TSE3 - the Trax Sequencer Engine version 3.00.
00007  *
00008  * This library is modifiable/redistributable under the terms of the GNU
00009  * General Public License.
00010  *
00011  * You should have received a copy of the GNU General Public License along
00012  * with this program; see the file COPYING. If not, write to the Free Software
00013  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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

Generated on Wed May 25 14:45:05 2005 for TSE3 by doxygen 1.3.2