[65b6e0] | 1 | /*
|
---|
[56f73b] | 2 | * Action.hpp
|
---|
[65b6e0] | 3 | *
|
---|
| 4 | * Created on: Dec 8, 2009
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[56f73b] | 8 | #ifndef ACTION_HPP_
|
---|
| 9 | #define ACTION_HPP_
|
---|
| 10 |
|
---|
| 11 | // include config.h
|
---|
| 12 | #ifdef HAVE_CONFIG_H
|
---|
| 13 | #include <config.h>
|
---|
| 14 | #endif
|
---|
[65b6e0] | 15 |
|
---|
[cc04b7] | 16 | #include <string>
|
---|
| 17 |
|
---|
[6ba9ba] | 18 | #include <boost/preprocessor/list/adt.hpp>
|
---|
| 19 |
|
---|
[e4afb4] | 20 | /** Used in .def files in paramdefaults define to set that no default value exists.
|
---|
[6ba9ba] | 21 | * We define NOPARAM_DEFAULT here, as it is used in .def files and needs to be present
|
---|
[e4afb4] | 22 | * before these are included.
|
---|
| 23 | */
|
---|
[6ba9ba] | 24 | #define NOPARAM_DEFAULT BOOST_PP_NIL
|
---|
[e4afb4] | 25 |
|
---|
[67e2b3] | 26 | // forward declaration
|
---|
[126867] | 27 | template <typename T> class Registry;
|
---|
[67e2b3] | 28 |
|
---|
[ce7fdc] | 29 | namespace MoleCuilder {
|
---|
[126867] | 30 | class ActionHistory;
|
---|
[f54cda] | 31 | class ActionQueue;
|
---|
[0c8056] | 32 | class ActionRegistry;
|
---|
[ce7fdc] | 33 | class ActionSequence;
|
---|
| 34 | }
|
---|
[126867] | 35 | class ActionSequenceTest;
|
---|
[ba7418] | 36 | class Dialog;
|
---|
[67e2b3] | 37 |
|
---|
[f92ef3] | 38 | #include "Actions/ActionParameters.hpp"
|
---|
[b5b01e] | 39 | #include "Actions/ActionState.hpp"
|
---|
[3139b2] | 40 | #include "Actions/ActionTrait.hpp"
|
---|
[df32ee] | 41 |
|
---|
[2efa90] | 42 |
|
---|
[ce7fdc] | 43 | namespace MoleCuilder {
|
---|
[df32ee] | 44 |
|
---|
[b2c302] | 45 | /** Actions are Command patterns to allow for undoing and redoing.
|
---|
| 46 | *
|
---|
| 47 | * Each specific Action derives from this class to implement a certain functionality.
|
---|
[ef81b0] | 48 | *
|
---|
| 49 | * Actions describe something that has to be done.
|
---|
[b2c302] | 50 | * Actions can be passed around, stored, performed and undone (Command-Pattern:
|
---|
| 51 | * http://en.wikipedia.org/wiki/Command_pattern).
|
---|
| 52 | *
|
---|
| 53 | * Unique to each Action is its ActionTrait, i.e. the options it requires
|
---|
| 54 | * to perform a certain function. E.g. in order to execute a "add atom" Action
|
---|
| 55 | * we need to know a position and an element. These options have certain
|
---|
| 56 | * properties, see \ref OptionTrait and \ref ActionTrait wherein these are stored.
|
---|
| 57 | * Essentially, each option is stored as an \ref OptionTrait instance and
|
---|
| 58 | * contains the token, default value, a description, the type, ...
|
---|
| 59 | *
|
---|
| 60 | * ActionTrait itself is also an OptionTrait because the command token may actually
|
---|
| 61 | * coincide with an option-token. E.g. this allows "...--add-atom 3" to mean
|
---|
| 62 | * both execute the action under token "add-atom" and that the option "add-atom"
|
---|
| 63 | * (the new atom's \ref element number) should contain 3.
|
---|
| 64 | *
|
---|
| 65 | * \ref ActionTrait contains a map of all associated options. With this in the cstor
|
---|
| 66 | * we register not only the Action with the \ref ActionRegistry but also each of
|
---|
| 67 | * its \link options OptionTrait \endlink with the \ref OptionRegistry.
|
---|
| 68 | *
|
---|
| 69 | * The token of the option is unique, but two Action's may share the same token if:
|
---|
| 70 | * -# they have the same default value
|
---|
| 71 | * -# they have the same type
|
---|
| 72 | *
|
---|
| 73 | * This requirement is easy because if you need to store some option of another
|
---|
| 74 | * type, simply think of a new suitable name for it.
|
---|
| 75 | *
|
---|
| 76 | * The actual value, i.e. "3" in the "add-atom" example, is taken from the
|
---|
| 77 | * ValueStorage, see \ref Dialog for how this is possible.
|
---|
| 78 | *
|
---|
| 79 | * \note There is a unit test that checks on the consistency of all registered
|
---|
| 80 | * options, also in "--enable-debug"-mode assertions will check that an option
|
---|
| 81 | * has not been registered before under another type.
|
---|
| 82 | *
|
---|
[ef81b0] | 83 | */
|
---|
[65b6e0] | 84 | class Action
|
---|
| 85 | {
|
---|
[f54cda] | 86 | //!> grant ActionQueue access to undo() and redo()
|
---|
[126867] | 87 | friend class ActionHistory;
|
---|
[f54cda] | 88 | //!> grant ActionQueue access to call()
|
---|
| 89 | friend class ActionQueue;
|
---|
| 90 | //!> grant ActionRegistry access to cstors (for ActionRegistry::fillRegistry())
|
---|
[126867] | 91 | friend class ActionRegistry;
|
---|
[f54cda] | 92 | //!> grant ActionSequence access to Action's private stuff
|
---|
[126867] | 93 | friend class ActionSequence;
|
---|
[f54cda] | 94 | //!> grant all Registry templates access to cstor and dstor (for Registry<T>::cleanup())
|
---|
[126867] | 95 | template <typename T> friend class ::Registry;
|
---|
| 96 | //!> TextMenu needs to instantiate some specific Actions, grant access to cstor
|
---|
| 97 | friend class TextMenu;
|
---|
| 98 |
|
---|
| 99 | // some unit tests on Actions
|
---|
| 100 | friend class ::ActionSequenceTest;
|
---|
[1fa107] | 101 | public:
|
---|
[5b0b98] | 102 |
|
---|
[4e145c] | 103 | enum QueryOptions {Interactive, NonInteractive};
|
---|
| 104 |
|
---|
[126867] | 105 | protected:
|
---|
[8a34392] | 106 | /**
|
---|
| 107 | * Standard constructor of Action Base class
|
---|
| 108 | *
|
---|
| 109 | * All Actions need to have a name. The second flag indicates, whether the action should
|
---|
| 110 | * be registered with the ActionRegistry. If the Action is registered the name of the
|
---|
| 111 | * Action needs to be unique for all Actions that are registered.
|
---|
[e4afb4] | 112 | *
|
---|
| 113 | * \note NO reference for \a _Traits as we do have to copy it, otherwise _Traits would have
|
---|
| 114 | * to be present throughout the program's run.
|
---|
| 115 | *
|
---|
| 116 | * \param Traits information class to this action
|
---|
[8a34392] | 117 | */
|
---|
[126867] | 118 | Action(const ActionTrait &_Traits);
|
---|
[65b6e0] | 119 | virtual ~Action();
|
---|
| 120 |
|
---|
[8a34392] | 121 | /**
|
---|
| 122 | * This method is used to call an action. The basic operations for the Action
|
---|
| 123 | * are carried out and if necessary/possible the Action is added to the History
|
---|
| 124 | * to allow for undo of this action.
|
---|
| 125 | *
|
---|
| 126 | * If the call needs to undone you have to use the History, to avoid destroying
|
---|
| 127 | * invariants used by the History.
|
---|
[4e145c] | 128 | *
|
---|
| 129 | * Note that this call can be Interactive (i.e. a dialog will ask the user for
|
---|
| 130 | * necessary information) and NonInteractive (i.e. the information will have to
|
---|
| 131 | * be present already within the ValueStorage class or else a MissingArgumentException
|
---|
| 132 | * is thrown)
|
---|
[8a34392] | 133 | */
|
---|
[6bf52f] | 134 | void call(enum QueryOptions state = Interactive);
|
---|
[67e2b3] | 135 |
|
---|
[f54cda] | 136 | public:
|
---|
[8a34392] | 137 | /**
|
---|
| 138 | * This method provides a flag that indicates if an undo mechanism is implemented
|
---|
| 139 | * for this Action. If this is true, and this action was called last, you can
|
---|
| 140 | * use the History to undo this action.
|
---|
| 141 | */
|
---|
[65b6e0] | 142 | virtual bool canUndo()=0;
|
---|
[8a34392] | 143 |
|
---|
| 144 | /**
|
---|
| 145 | * This method provides a flag, that indicates if the Action changes the state of
|
---|
| 146 | * the application in a way that needs to be undone for the History to work.
|
---|
| 147 | *
|
---|
| 148 | * If this is false the Action will not be added to the History upon calling. However
|
---|
| 149 | * Actions called before this one will still be available for undo.
|
---|
| 150 | */
|
---|
[67e2b3] | 151 | virtual bool shouldUndo()=0;
|
---|
[65b6e0] | 152 |
|
---|
[8a34392] | 153 | /**
|
---|
| 154 | * Indicates whether the Action can do it's work at the moment. If this
|
---|
| 155 | * is false calling the action will result in a no-op.
|
---|
| 156 | */
|
---|
[f9352d] | 157 | virtual bool isActive();
|
---|
| 158 |
|
---|
[8a34392] | 159 | /**
|
---|
| 160 | * Returns the name of the Action.
|
---|
| 161 | */
|
---|
[13799e] | 162 | const std::string getName() const;
|
---|
[cc04b7] | 163 |
|
---|
[e4b2f6] | 164 | /**
|
---|
| 165 | * returns a detailed help message.
|
---|
| 166 | */
|
---|
| 167 | const std::string help() const;
|
---|
| 168 |
|
---|
[e4afb4] | 169 | /**
|
---|
| 170 | * Traits resemble all necessary information that "surrounds" an action, such as
|
---|
| 171 | * its name (for ActionRegistry and as ref from string to instance and vice versa),
|
---|
| 172 | * which menu, which position, what parameters, their types, if it is itself a
|
---|
| 173 | * parameter and so on ...
|
---|
| 174 | *
|
---|
| 175 | * Note that is important that we do not use a reference here. We want to copy the
|
---|
| 176 | * information in the Action's constructor and have it contained herein. Hence, we
|
---|
[3139b2] | 177 | * also have our own copy constructor for ActionTrait. Information should be
|
---|
[e4afb4] | 178 | * encapsulated in the Action, no more references to the outside than absolutely
|
---|
| 179 | * necessary.
|
---|
| 180 | */
|
---|
[3139b2] | 181 | const ActionTrait Traits;
|
---|
[df32ee] | 182 |
|
---|
[dfef3f] | 183 | protected:
|
---|
[41449c] | 184 | /** Removes the static entities Action::success and Action::failure.
|
---|
| 185 | * This is only to be called on the program's exit, i.e. in cleanUp(),
|
---|
| 186 | * as these static entities are used throughout all Actions.
|
---|
| 187 | */
|
---|
| 188 | static void removeStaticStateEntities();
|
---|
| 189 |
|
---|
[dfef3f] | 190 | /** Creates the static entities Action::success and Action::failure.
|
---|
| 191 | * This is only to be called by ActionHistory.
|
---|
| 192 | */
|
---|
| 193 | static void createStaticStateEntities();
|
---|
| 194 |
|
---|
[8a34392] | 195 | /**
|
---|
| 196 | * This method is called by the History, when an undo is performed. It is
|
---|
| 197 | * provided with the corresponding state produced by the performCall or
|
---|
| 198 | * performRedo method and needs to provide a state that can be used for redo.
|
---|
| 199 | */
|
---|
[b5b01e] | 200 | ActionState::ptr undo(ActionState::ptr);
|
---|
[8a34392] | 201 |
|
---|
| 202 | /**
|
---|
[992bd5] | 203 | * This method is called by the History, when a redo is performed. It is
|
---|
[8a34392] | 204 | * provided with the corresponding state produced by the undo method and
|
---|
| 205 | * needs to produce a State that can then be used for another undo.
|
---|
| 206 | */
|
---|
[b5b01e] | 207 | ActionState::ptr redo(ActionState::ptr);
|
---|
[2efa90] | 208 |
|
---|
[8a34392] | 209 | /**
|
---|
[992bd5] | 210 | * This special state can be used to indicate that the Action was successful
|
---|
[8a34392] | 211 | * without providing a special state. Use this if your Action does not need
|
---|
[992bd5] | 212 | * a specialized state.
|
---|
[8a34392] | 213 | */
|
---|
[b5b01e] | 214 | static ActionState::ptr success;
|
---|
[8a34392] | 215 |
|
---|
| 216 | /**
|
---|
| 217 | * This special state can be returned, to indicate that the action could not do it's
|
---|
[992bd5] | 218 | * work, was aborted by the user etc. If you return this state make sure to transactionize
|
---|
[8a34392] | 219 | * your Actions and unroll the complete transaction before this is returned.
|
---|
| 220 | */
|
---|
[b5b01e] | 221 | static ActionState::ptr failure;
|
---|
[67e2b3] | 222 |
|
---|
[8a34392] | 223 | /**
|
---|
[ba7418] | 224 | * This creates the dialog requesting the information needed for this action from the user
|
---|
| 225 | * via means of the user interface.
|
---|
| 226 | */
|
---|
[047878] | 227 | Dialog * createDialog();
|
---|
| 228 |
|
---|
[862b6a] | 229 | /** Virtual function that starts the timer.
|
---|
| 230 | *
|
---|
| 231 | */
|
---|
| 232 | virtual void startTimer() const {};
|
---|
| 233 |
|
---|
| 234 | /** Virtual function that ends the timer.
|
---|
| 235 | *
|
---|
| 236 | */
|
---|
| 237 | virtual void endTimer() const {};
|
---|
| 238 |
|
---|
[047878] | 239 | private:
|
---|
| 240 |
|
---|
[0b2ce9] | 241 | /**
|
---|
| 242 | * This is called internally before the action is processed. This adds necessary queries
|
---|
| 243 | * to a given dialog to obtain parameters for the user for processing the action accordingly.
|
---|
| 244 | * The dialog will be given to the user before Action::performCall() is initiated, values
|
---|
| 245 | * are transfered via ValueStorage.
|
---|
| 246 | */
|
---|
[047878] | 247 | virtual Dialog * fillDialog(Dialog*)=0;
|
---|
[ba7418] | 248 |
|
---|
| 249 | /**
|
---|
| 250 | * This is called internally when the call is being done. Implement this method to do the actual
|
---|
[8a34392] | 251 | * work of the Action. Implement this in your Derived classes. Needs to return a state that can be
|
---|
| 252 | * used to undo the action.
|
---|
| 253 | */
|
---|
[b5b01e] | 254 | virtual ActionState::ptr performCall()=0;
|
---|
[8a34392] | 255 |
|
---|
| 256 | /**
|
---|
| 257 | * This is called internally when the undo process is chosen. This Method should use the state
|
---|
| 258 | * produced by the performCall method to return the state of the application to the state
|
---|
| 259 | * it had before the Action.
|
---|
| 260 | */
|
---|
[b5b01e] | 261 | virtual ActionState::ptr performUndo(ActionState::ptr)=0;
|
---|
[8a34392] | 262 |
|
---|
| 263 | /**
|
---|
| 264 | * This is called internally when the redo process is chosen. This method shoudl use the state
|
---|
| 265 | * produced by the performUndo method to return the application to the state it should have after
|
---|
| 266 | * the action.
|
---|
| 267 | *
|
---|
| 268 | * Often this method can be implement to re-use the performCall method. However if user interaction
|
---|
| 269 | * or further parameters are needed, those should be taken from the state and not query the user
|
---|
| 270 | * again.
|
---|
| 271 | */
|
---|
[b5b01e] | 272 | virtual ActionState::ptr performRedo(ActionState::ptr)=0;
|
---|
[67e2b3] | 273 | };
|
---|
| 274 |
|
---|
[ce7fdc] | 275 | }
|
---|
| 276 |
|
---|
[56f73b] | 277 | #endif /* ACTION_HPP_ */
|
---|