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