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