[65b6e0] | 1 | /*
|
---|
| 2 | * Action.h
|
---|
| 3 | *
|
---|
| 4 | * Created on: Dec 8, 2009
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #ifndef ACTION_H_
|
---|
| 9 | #define ACTION_H_
|
---|
| 10 |
|
---|
[cc04b7] | 11 | #include <string>
|
---|
[5b0b98] | 12 | #include <boost/shared_ptr.hpp>
|
---|
[cc04b7] | 13 |
|
---|
[67e2b3] | 14 | // forward declaration
|
---|
| 15 |
|
---|
| 16 | class ActionState;
|
---|
| 17 | class ActionSequence;
|
---|
| 18 |
|
---|
[2efa90] | 19 | /**
|
---|
| 20 | * @file
|
---|
| 21 | * <H1> Action Howto </H1>
|
---|
| 22 | *
|
---|
| 23 | * <H2> Introduction </H2>
|
---|
| 24 | *
|
---|
| 25 | * Actions are used in object oriented design as a replacement for callback functions.
|
---|
| 26 | * In most ways Actions can be used in the same way that callbacks were used in non
|
---|
| 27 | * OO-Systems, but can contain support for several extra mechanism such as undo/redo
|
---|
| 28 | * or progress indicators.
|
---|
| 29 | *
|
---|
| 30 | * The main purpose of an action class is to contain small procedures, that can be repeatedly
|
---|
| 31 | * called. These procedures can also be stored, passed around, so that the execution of an
|
---|
| 32 | * action can happen quite far away from the place of creation. For a detailed description of
|
---|
| 33 | * the Action pattern see GOF:1996.
|
---|
| 34 | *
|
---|
| 35 | * <H3> How to use an action </H3>
|
---|
| 36 | *
|
---|
| 37 | * The process of using an action is as easy as calling the call() method of the action. The
|
---|
| 38 | * action will then do whatever it is supposed to do. If it is an action that can be undone, it
|
---|
| 39 | * will also register itself in the history to make itself available for undo. To undo the last
|
---|
| 40 | * action, you can either use the undoLast() method inside the ActionHistory class or call the
|
---|
| 41 | * UndoAction also provided by the ActionHistory. If an action was undone it will be available for
|
---|
| 42 | * redo, using the redoLast() method of the ActionHistory or the RedoAction also provided by this
|
---|
| 43 | * class. To check whether undo/redo is available at any moment you can use the hasUndo() or
|
---|
| 44 | * hasRedo() method respectively.
|
---|
| 45 | *
|
---|
| 46 | * Actions can be set to be active or inactive. If an action is set to inactive it is signaling, that
|
---|
| 47 | * some condition necessary for this action to be executed is not currently met. For example the
|
---|
| 48 | * UndoAction will set itself to inactive, when there is no action at that time that can be undone.
|
---|
| 49 | * Using call() on an inactive Action results in a no-op. You can query the state of an action using
|
---|
| 50 | * the isActive() method.
|
---|
| 51 | *
|
---|
| 52 | * The undo capabilities of actions come in three types as signaled by two boolean flags (one
|
---|
| 53 | * combination of these flags is left empty as can be seen later).
|
---|
| 54 | * <ul>
|
---|
| 55 | * <li/> The first flag indicates if the undo mechanism for this action should be considered at all, i.e.
|
---|
| 56 | * if the state of the application changes in a way that needs to be reverted. Actions that should
|
---|
| 57 | * consider the undo mechanism are for example adding a molecule, moving atoms, changing
|
---|
| 58 | * the name of a molecule etc. Changing the View-Area on the other hand should be an action that
|
---|
| 59 | * does not consider the undo mechanism. This flag can be queried using the shouldUndo() method.
|
---|
| 60 | *
|
---|
| 61 | * <li/> The second flag indicates whether the changes can be undo for this action. If this flag is true
|
---|
| 62 | * the action will be made available for undo using the ActionHistory class and the actions of this
|
---|
| 63 | * class. If this flag is false while the shoudlUndo() flag is true this means that this action
|
---|
| 64 | * changes the state of the application changes in a way that cannot be undone, but might cause
|
---|
| 65 | * the undo of previous actions to fail. In this case the whole History is cleared, as to keep
|
---|
| 66 | * the state of the application intact by avoiding dangerous undos. This flag can be queried
|
---|
| 67 | * using the canUndo() method.
|
---|
| 68 | *</ul>
|
---|
| 69 | *
|
---|
| 70 | * Each action has a name, that can be used to identify it throughout the run of the application.
|
---|
| 71 | * This name can be retrieved using the getName() method. Most actions also register themselves with
|
---|
| 72 | * a global structure, called the ActionRegistry. Actions that register themselves need to have a
|
---|
| 73 | * unique name for the whole application. If the name is known these actions can be retrieved from
|
---|
| 74 | * the registry by their name and then be used as normal.
|
---|
| 75 | *
|
---|
| 76 | * <H2> Building your own actions </H2>
|
---|
| 77 | *
|
---|
| 78 | * Building actions is fairly easy. Simply derive from the abstract Action base class and implement
|
---|
| 79 | * the virtual methods. The main code that needs to be executed upon call() should be implemented in
|
---|
| 80 | * the performCall() method. You should also indicate whether the action supports undo by implementing
|
---|
| 81 | * the shouldUndo() and canUndo() methods to return the appropriate flags.
|
---|
| 82 | *
|
---|
| 83 | * The constructor of your derived class also needs to call the Base constructor, passing it the
|
---|
| 84 | * name of the Action and a flag indicating whether this action should be made available in the
|
---|
| 85 | * registry. WARNING: Do not use the virtual getName() method of the derived action to provide the
|
---|
| 86 | * constructor with the name, even if you overloaded this method to return a constant. Doing this
|
---|
| 87 | * will most likely not do what you think it does (see: http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.5
|
---|
| 88 | * if you want to know why this wont work)
|
---|
| 89 | *
|
---|
| 90 | * <H3> Interfacing your Action with the Undo mechanism </H3>
|
---|
| 91 | *
|
---|
| 92 | * The performX() methods need to comply to a simple standard to allow for undo and redo. The first
|
---|
| 93 | * convention in this standard concerns the return type. All methods that handle calling, undoing
|
---|
| 94 | * or redoing return an object of Action::state_ptr. This is a smart pointer to a State object, that
|
---|
| 95 | * can be used to store state information that is needed by your action for later redo. A rename
|
---|
| 96 | * Action for example would need to store which object has been renamed and what the old name was.
|
---|
| 97 | * A move Action on the other hand would need to store the object that has been moved as well as the
|
---|
| 98 | * old position. If your Action does not need to store any kind of information for redo you can
|
---|
| 99 | * simply return Action::success and skip the rest of this paragraph. If your action has been
|
---|
| 100 | * abborted you can return Action::failure, which indicates to the history mechanism that this
|
---|
| 101 | * action should not be stored.
|
---|
| 102 | *
|
---|
| 103 | * If your Action needs any kind of information to undo its execution, you need to store this
|
---|
| 104 | * information in the state that is returned by the performCall() method. Since no assumptions
|
---|
| 105 | * can be made on the type or amount of information the ActionState base class is left empty.
|
---|
| 106 | * To use this class you need to derive a YourActionState class from the ActionState base class
|
---|
| 107 | * adding your data fields and accessor functions. Upon undo the ActionState object produced
|
---|
| 108 | * by the corresponding performCall() is then passed to the performUndo() method which should
|
---|
| 109 | * typecast the ActionState to the appropriate sub class, undo all the changes and produce
|
---|
| 110 | * a State object that can be used to redo the action if neccessary. This new state object is
|
---|
| 111 | * then used if the redo mechanism is invoked and passed to the performRedo() function, which
|
---|
| 112 | * again produces a State that can be used for performUndo().
|
---|
| 113 | *
|
---|
| 114 | * <H3> Outline of the implementation of Actions </H3>
|
---|
| 115 | *
|
---|
| 116 | * To sum up the actions necessary to build actions here is a brief outline of things methioned
|
---|
| 117 | * in the last paragraphs:
|
---|
| 118 | *
|
---|
| 119 | * <H4> Basics </H4>
|
---|
| 120 | *
|
---|
| 121 | * <ul>
|
---|
| 122 | * <li/> derive YourAction from Action
|
---|
| 123 | * <li/> pass name and flag for registry to the base constructor
|
---|
| 124 | * <li/> implement performCall(), performUndo(), performRedo()
|
---|
| 125 | * <li/> implement the functions that return the flags for the undo mechanism
|
---|
| 126 | * <li/> Derive YourActionState from ActionState as necessary
|
---|
| 127 | * </ul>
|
---|
| 128 | *
|
---|
| 129 | * <H4> Implementing performX() methods </H4>
|
---|
| 130 | *
|
---|
| 131 | * <ul>
|
---|
| 132 | * <li/> performCall():
|
---|
| 133 | * <ul>
|
---|
| 134 | * <li/> do whatever is needed to make the action work
|
---|
| 135 | * <li/> if the action was abborted return Action::failure
|
---|
| 136 | * <li/> if the action needs to save a state return a custom state object
|
---|
| 137 | * <li/> otherwise return Action::success
|
---|
| 138 | * </ul>
|
---|
| 139 | * <li/> performUndo():
|
---|
| 140 | * <ul>
|
---|
| 141 | * <li/> typecast the ActionState pointer to a Pointer to YourActionState if necessary
|
---|
| 142 | * <li/> undo the action using the information from the state
|
---|
| 143 | * <li/> produce a new state that can be used for redoing and return it
|
---|
| 144 | * </ul>
|
---|
| 145 | * <li/> performRedo():
|
---|
| 146 | * <ul>
|
---|
| 147 | * <li/> take the ActionState produced by performUndo and typecast it to a pointer to YourActionState if necessary
|
---|
| 148 | * <li/> redo the undone action using the information from the state
|
---|
| 149 | * <li/> produce a new state that can be used by performUndo() and return it
|
---|
| 150 | * </ul>
|
---|
| 151 | * </ul>
|
---|
| 152 | *
|
---|
| 153 | * <H2> Advanced techniques </H2>
|
---|
| 154 | *
|
---|
| 155 | * <H3> Predefined Actions </H3>
|
---|
| 156 | *
|
---|
| 157 | * To make construction of actions easy there are some predefined actions. Namely these are
|
---|
| 158 | * the MethodAction and the ErrorAction.
|
---|
| 159 | *
|
---|
| 160 | * The method action can be used to turn any function with empty arguments and return type void
|
---|
| 161 | * into an action (also works for functors with those types). Simply pass the constructor for the
|
---|
| 162 | * MethodAction a name to use for this action, the function to call inside the performCall()
|
---|
| 163 | * method and a flag indicating if this action should be made retrievable inside the registry
|
---|
| 164 | * (default is true). MethodActions always report themselves as changing the state of the
|
---|
| 165 | * application but cannot be undone. i.e. calling MethodActions will always cause the ActionHistory
|
---|
| 166 | * to be cleared.
|
---|
| 167 | *
|
---|
| 168 | * ErrorActions can be used to produce a short message using the Log() << Verbose() mechanism of
|
---|
| 169 | * the molecuilder. Simply pass the constructor a name for the action, the message to show upon
|
---|
| 170 | * calling this action and the flag for the registry (default is again true). Error action
|
---|
| 171 | * report that they do not change the state of the application and are therefore not considered
|
---|
| 172 | * for undo.
|
---|
| 173 | *
|
---|
| 174 | * <H3> Sequences of Actions and MakroActions </H3>
|
---|
| 175 | *
|
---|
| 176 | * <H4> Building sequences of Actions </H4>
|
---|
| 177 | *
|
---|
| 178 | * Actions can be chained to sequences using the ActionSequence class. Once an ActionSequence is
|
---|
| 179 | * constructed it will be initially empty. Any Actions can then be added to the sequence using the
|
---|
| 180 | * addAction() method of the ActionSequence class. The last added action can be removed using the
|
---|
| 181 | * removeLastAction() method. If the construction of the sequence is done, you can use the
|
---|
| 182 | * callAll() method. Each action called this way will register itself with the History to allow
|
---|
[a295d1] | 183 | * separate undo of all actions in the sequence.
|
---|
[2efa90] | 184 | *
|
---|
| 185 | * <H4> Building larger Actions from simple ones </H4>
|
---|
| 186 | *
|
---|
| 187 | * Using the pre-defined class MakroAction it is possible to construct bigger actions from a sequence
|
---|
| 188 | * of smaller ones. For this you first have to build a sequence of the actions using the ActionSequence
|
---|
| 189 | * as described above. Then you can construct a MakroAction passing it a name, the sequence to use
|
---|
| 190 | * and as usual a flag for the registry. You can then simply call the complete action-sequence through
|
---|
| 191 | * this makro action using the normal interface. Other than with the direct use of the action sequence
|
---|
| 192 | * only the complete MakroAction is registered inside the history, i.e. the complete sequence can be
|
---|
| 193 | * undone at once. Also there are a few caveats you have to take care of when using the MakroAction:
|
---|
| 194 | * <ul>
|
---|
| 195 | * <li/> All Actions as well as the sequence should exclusively belong to the MakroAction. This
|
---|
| 196 | * especially means, that the destruction of these objects should be handled by the MakroAction.
|
---|
| 197 | * <li/> none of the Actions inside the MakroAction should be registered with the registry, since the
|
---|
| 198 | * registry also assumes sole ownership of the actions.
|
---|
| 199 | * <li/> Do not remove or add actions from the sequence once the MakroAction has been constructed, since this
|
---|
| 200 | * might brake important assumptions for the undo/redo mechanism
|
---|
| 201 | * </ul>
|
---|
[a295d1] | 202 | *
|
---|
| 203 | * <H3> Special kinds of Actions </H3>
|
---|
| 204 | *
|
---|
| 205 | * To make the usage of Actions more versatile there are two special kinds of actions defined,
|
---|
| 206 | * that contain special mechanisms. These are defined inside the class Process, for actions that
|
---|
| 207 | * take some time and indicate their own progress, and in the class Calculations for actions that
|
---|
| 208 | * have a retrievable result.
|
---|
| 209 | *
|
---|
| 210 | * <H4> Processes </H4>
|
---|
| 211 | *
|
---|
| 212 | * Processes are Actions that might take some time and therefore contain special mechanisms
|
---|
| 213 | * to indicate their progress to the user. If you want to implement a process you can follow the
|
---|
| 214 | * guidelines for implementing actions. In addition to the normal Action constructor parameters,
|
---|
| 215 | * you also need to define the number of steps the process takes to finish (use 0 if that number is
|
---|
| 216 | * not known upon construction). At the beginning of your process you then simply call start() to
|
---|
| 217 | * indicate that the process is taking up its work. You might also want to set the number of steps it
|
---|
| 218 | * needs to finish, if it has changed since the last invocation/construction. You can use the
|
---|
| 219 | * setMaxSteps() method for this. Then after each finished step of calulation simply call step(),
|
---|
| 220 | * to let the indicators know that it should update itself. If the number of steps is not known
|
---|
| 221 | * at the time of calculation, you should make sure the maxSteps field is set to 0, either through
|
---|
| 222 | * the constructor or by using setMaxSteps(0). Indicators are required to handle both processes that
|
---|
| 223 | * know the number of steps needed as well as processes that cannot predict when they will be finished.
|
---|
| 224 | * Once your calculation is done call stop() to let every indicator know that the process is done with
|
---|
| 225 | * the work and to let the user know.
|
---|
| 226 | *
|
---|
| 227 | * Indicators that want to know about processes need to implement the Observer class with all the
|
---|
| 228 | * methods defined there. They can then globally sign on to all processes using the static
|
---|
| 229 | * Process::AddObserver() method and remove themselves using the Process::RemoveObserver()
|
---|
| 230 | * methods. When a process starts it will take care that the notification for this process
|
---|
| 231 | * is invoked at the right time. Indicators should not try to observe a single process, but rather
|
---|
| 232 | * be ready to observe the status of any kind of process using the methods described here.
|
---|
| 233 | *
|
---|
| 234 | * <H4> Calculations </H4>
|
---|
| 235 | *
|
---|
| 236 | * Calculations are special Actions that also return a result when called. Calculations are
|
---|
| 237 | * always derived from Process, so that the progress of a calculation can be shown. Also
|
---|
| 238 | * Calculations should not contain side-effects and not consider the undo mechanism.
|
---|
| 239 | * When a Calculation is called using the Action mechanism this will cause it to calculate
|
---|
| 240 | * the result and make it available using the getResult() method. Another way to have a Calculation
|
---|
| 241 | * produce a result is by using the function-call operator. When this operator is used, the Calculation
|
---|
| 242 | * will try to return a previously calculated and cached result and only do any actuall calculations
|
---|
| 243 | * when no such result is available. You can delete the cached result using the reset() method.
|
---|
[2efa90] | 244 | */
|
---|
| 245 |
|
---|
[ef81b0] | 246 | /**
|
---|
| 247 | * Base class for all actions.
|
---|
| 248 | *
|
---|
| 249 | * Actions describe something that has to be done.
|
---|
| 250 | * Actions can be passed around, stored, performed and undone (Command-Pattern).
|
---|
| 251 | */
|
---|
[65b6e0] | 252 | class Action
|
---|
| 253 | {
|
---|
[67e2b3] | 254 | friend class ActionSequence;
|
---|
[2efa90] | 255 | friend class ActionHistory;
|
---|
[1fa107] | 256 | public:
|
---|
[5b0b98] | 257 |
|
---|
| 258 | typedef boost::shared_ptr<ActionState> state_ptr;
|
---|
| 259 |
|
---|
[cc04b7] | 260 | Action(std::string _name,bool _doRegister=true);
|
---|
[65b6e0] | 261 | virtual ~Action();
|
---|
| 262 |
|
---|
[67e2b3] | 263 | // this method only handles the infrastructure
|
---|
| 264 | // actuall action is passed on to a private virtual
|
---|
| 265 | void call();
|
---|
| 266 |
|
---|
[65b6e0] | 267 | virtual bool canUndo()=0;
|
---|
[67e2b3] | 268 | virtual bool shouldUndo()=0;
|
---|
[65b6e0] | 269 |
|
---|
[f9352d] | 270 | virtual bool isActive();
|
---|
| 271 |
|
---|
[cc04b7] | 272 | virtual const std::string getName();
|
---|
| 273 |
|
---|
[67e2b3] | 274 | protected:
|
---|
[2efa90] | 275 | state_ptr undo(state_ptr);
|
---|
| 276 | state_ptr redo(state_ptr);
|
---|
| 277 |
|
---|
[5b0b98] | 278 | static state_ptr success;
|
---|
| 279 | static state_ptr failure;
|
---|
[67e2b3] | 280 |
|
---|
[cc04b7] | 281 | private:
|
---|
[5b0b98] | 282 | virtual state_ptr performCall()=0;
|
---|
| 283 | virtual state_ptr performUndo(state_ptr)=0;
|
---|
| 284 | virtual state_ptr performRedo(state_ptr)=0;
|
---|
[67e2b3] | 285 |
|
---|
[cc04b7] | 286 | std::string name;
|
---|
[65b6e0] | 287 | };
|
---|
| 288 |
|
---|
[67e2b3] | 289 | /**
|
---|
| 290 | * This class can be used by actions to save the state.
|
---|
| 291 | *
|
---|
| 292 | * It is implementing a memento pattern. The base class is completely empty,
|
---|
| 293 | * since no general state internals can be given. The Action performing
|
---|
| 294 | * the Undo should downcast to the apropriate type.
|
---|
| 295 | */
|
---|
| 296 | class ActionState{
|
---|
| 297 | public:
|
---|
| 298 | ActionState(){}
|
---|
| 299 | virtual ~ActionState(){}
|
---|
| 300 | };
|
---|
| 301 |
|
---|
[65b6e0] | 302 | #endif /* ACTION_H_ */
|
---|