/usr/include/tse3/cmd/Command.h is in libtse3-dev 0.3.1-4.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | /*
* @(#)cmd/Command.h 3.00 10 June 1999
*
* Copyright (c) 2000 Pete Goodliffe (pete@cthree.org)
*
* This file is part of TSE3 - the Trax Sequencer Engine version 3.00.
*
* This library is modifiable/redistributable under the terms of the GNU
* General Public License.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING. If not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#ifndef TSE3_CMD_COMMAND_H
#define TSE3_CMD_COMMAND_H
#include <string>
namespace TSE3
{
/**
* The Cmd namespace contains classes that implement the Command
* pattern (GoF book). They use the @ref TSE3 API to provide undo/redoable
* commands objects for all commonly performed operations.
*
* There are a large number of commands that can be used for different
* purposes. The base class for all @ref TSE3 commands is the
* @ref Command. There exists one command class for every useful
* operation on the @ref TSE3::Song data structure and it's subcomponents.
*
* There is one proviso with using commands: if you manipulate some
* of a @ref TSE3::Song with commands, for the undo/redo to work you
* must (logically) manipulate the @ref TSE3::Song entirely through
* @ref Command objects. Otherwise the undo/redo system will be
* faced with complicated inconsistencies which cannot be resolved.
*
* You can store executed @ref Command objects on the
* @ref CommandHistory buffer to automate the undo/redo process.
*
* @ref Command subclasses are easy to write, so it's not difficult
* to add your own commands and use them with the @ref CommandHistory.
* See the notes in the main TSE3 library documentation for more
* information.
*
* @short @ref TSE3 library undo/redoable commands
* @author Pete Goodliffe
* @version 3.00
* @see TSE3
*/
namespace Cmd
{
/**
* A base class for implementing the 'Command' design pattern [Gof]
*
* If you are implementing a new Command then inherit publicly
* from this class, implement the @ref executeImpl() and
* @ref undoImpl() methods and ensure that your constructor calls the
* Command protected constructor.
*
* @short Base command class
* @author Pete Goodliffe
* @version 3.00
*/
class Command
{
public:
virtual ~Command() {}
/**
* Call this to execute the Command.
*
* If the Command has already been executed, nothing will
* happen.
*
* @see undo
*/
void execute()
{
if (!_done)
{
executeImpl();
_done = true;
}
}
/**
* Call this to undo the Command.
*
* If the Command can't be undone, or it has already been
* undone, then nothing will happen.
*/
void undo()
{
if (_done && _undoable)
{
undoImpl();
_done = false;
}
}
/**
* Returns a descriptive string for the command.
*
* The string begins with a lower case letter and is capable
* of following "Undo " or "Redo ".
*/
const std::string &title() const { return _title; }
/**
* Returns whether this Command is capable of being undone.
*/
bool undoable() const { return _undoable; }
/**
* Returns whether this command has been executed.
*
* @see execute
* @see undo
*/
bool done() const { return _done; }
protected:
/**
* Create a Command with a descriptive string. This must begin
* with a lower case letter and be capable of following "Undo "
* or "Redo ".
*
* @param title Name for Command
* @param undoable Whether the Command is undoable
*/
Command(const std::string &title, bool undoable = true)
: _title(title), _undoable(undoable), _done(false)
{}
/**
* Implementations of Command override this method to implement
* the execute action.
*/
virtual void executeImpl() = 0;
/**
* Implementations of Command override this method to implement
* to undo action.
*/
virtual void undoImpl() = 0;
/**
* Sets the Command title. Normally you will do this in
* the ctor and never use this.
*/
void setTitle(const std::string &title) { _title = title; }
private:
std::string _title;
bool _undoable;
bool _done;
};
/**
* A template class for implementing @ref Command objects that set a
* particular class' variable state.
*
* The template takes five parameters:
* @li cls_type - the type of the class to be altered
* @li arg_type - the type of variable being set
* @li param_type - the type of variable that is accepted as a parameter
* @li getmfn - cls_type's method to read the variable,
* returns param_type
* @li setmfn - cls_type's method to set the variable, takes
* param_type
*
* @short Variable setting command template class
* @author Pete Goodliffe
* @version 3.00
*/
template <class cls_type, class arg_type, class param_type,
param_type (cls_type::*getmfn)() const,
void (cls_type::*setmfn)(param_type)>
class VariableSetCommand : public Command
{
public:
VariableSetCommand(cls_type *cls, param_type arg,
const std::string &title)
: Command(title), cls(cls), arg(arg) {}
virtual ~VariableSetCommand() {}
protected:
/**
* @reimplemented
*/
virtual void executeImpl()
{
old = (cls->*getmfn)();
(cls->*setmfn)(arg);
}
/**
* @reimplemented
*/
virtual void undoImpl()
{
(cls->*setmfn)(old);
}
private:
cls_type *cls;
arg_type arg;
arg_type old;
};
}
}
#endif
|