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