| 1 | /*
 | 
|---|
| 2 |  * ActionQueue.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Aug 16, 2013
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef ACTIONQUEUE_HPP_
 | 
|---|
| 9 | #define ACTIONQUEUE_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | // include config.h
 | 
|---|
| 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 13 | #include <config.h>
 | 
|---|
| 14 | #endif
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "CodePatterns/Singleton.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <list>
 | 
|---|
| 19 | #include <vector>
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "Actions/Action.hpp"
 | 
|---|
| 22 | #include "Actions/ActionState.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | namespace MoleCuilder {
 | 
|---|
| 25 | 
 | 
|---|
| 26 | class ActionHistory;
 | 
|---|
| 27 | class ActionRegistry;
 | 
|---|
| 28 | class ActionTrait;
 | 
|---|
| 29 | 
 | 
|---|
| 30 | /** This class combines the whole handling of Actions into a single class.
 | 
|---|
| 31 |  *
 | 
|---|
| 32 |  * It spawns new Actions by its internal ActionRegistry. Spawned Actions are
 | 
|---|
| 33 |  * automatically queued and placed into a History after execution.
 | 
|---|
| 34 |  */
 | 
|---|
| 35 | class ActionQueue : public Singleton<ActionQueue>
 | 
|---|
| 36 | {
 | 
|---|
| 37 |   friend class Singleton<ActionQueue>;
 | 
|---|
| 38 | public:
 | 
|---|
| 39 |   typedef std::vector<std::string> ActionTokens_t;
 | 
|---|
| 40 |   typedef std::list<Action *> ActionQueue_t;
 | 
|---|
| 41 | 
 | 
|---|
| 42 |   /** Queues the Action with \a name to be called.
 | 
|---|
| 43 |    *
 | 
|---|
| 44 |    * \param name token of Action to queue
 | 
|---|
| 45 |    * \param state whether Actions needs to be filled via a Dialog or not
 | 
|---|
| 46 |    */
 | 
|---|
| 47 |   void queueAction(const std::string &name, enum Action::QueryOptions state = Action::Interactive);
 | 
|---|
| 48 | 
 | 
|---|
| 49 |   /** Queues the Action with \a name to be called.
 | 
|---|
| 50 |    *
 | 
|---|
| 51 |    * \param _action action to add
 | 
|---|
| 52 |    * \param state whether Actions needs to be filled via a Dialog or not
 | 
|---|
| 53 |    */
 | 
|---|
| 54 |   void queueAction(Action *_action, enum Action::QueryOptions state = Action::Interactive);
 | 
|---|
| 55 | 
 | 
|---|
| 56 |   /** Returns the spawned action by token \a name.
 | 
|---|
| 57 |    *
 | 
|---|
| 58 |    * Action is checked into internal queue.
 | 
|---|
| 59 |    *
 | 
|---|
| 60 |    * \return pointer to newly spawned action
 | 
|---|
| 61 |    */
 | 
|---|
| 62 |   Action* getActionByName(const std::string &name);
 | 
|---|
| 63 | 
 | 
|---|
| 64 |   /** Checks whether the Action is known by the given \a name.
 | 
|---|
| 65 |    *
 | 
|---|
| 66 |    * \param name token of action to check
 | 
|---|
| 67 |    * \return true - token is known, false - else
 | 
|---|
| 68 |    */
 | 
|---|
| 69 |   bool isActionKnownByName(const std::string &name) const;
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   /** Register an Action with the ActionRegistry.
 | 
|---|
| 72 |    *
 | 
|---|
| 73 |    * \param _action action to add
 | 
|---|
| 74 |    */
 | 
|---|
| 75 |   void registerAction(Action *_action);
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   /** Returns the vector with the tokens of all currently known Actions.
 | 
|---|
| 78 |    *
 | 
|---|
| 79 |    * \return list of all tokens
 | 
|---|
| 80 |    */
 | 
|---|
| 81 |   const ActionTokens_t getListOfActions() const;
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   /** Returns the trait to an Action.
 | 
|---|
| 84 |    *
 | 
|---|
| 85 |    * \param name name of Action
 | 
|---|
| 86 |    * \return const ref to its default Trait.
 | 
|---|
| 87 |    */
 | 
|---|
| 88 |   const ActionTrait& getActionsTrait(const std::string &name) const;
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   /** Print the current contents of the queue as CLI instantiated list of Actions.
 | 
|---|
| 91 |    *
 | 
|---|
| 92 |    * This is useful for storing the current session.
 | 
|---|
| 93 |    *
 | 
|---|
| 94 |    * \param output output stream to print to
 | 
|---|
| 95 |    */
 | 
|---|
| 96 |   void outputAsCLI(std::ostream &output) const;
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   /** Print the current contents of the queue as Python instantiated list of Actions.
 | 
|---|
| 99 |    *
 | 
|---|
| 100 |    * This is useful for storing the current session.
 | 
|---|
| 101 |    *
 | 
|---|
| 102 |    * \param output output stream to print to
 | 
|---|
| 103 |    */
 | 
|---|
| 104 |   void outputAsPython(std::ostream &output) const;
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   /** Undoes last called Action.
 | 
|---|
| 107 |    *
 | 
|---|
| 108 |    */
 | 
|---|
| 109 |   void undoLast();
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   /** Redoes last undone Action.
 | 
|---|
| 112 |    *
 | 
|---|
| 113 |    */
 | 
|---|
| 114 |   void redoLast();
 | 
|---|
| 115 | 
 | 
|---|
| 116 | private:
 | 
|---|
| 117 |   //!> grant Action access to internal history functions.
 | 
|---|
| 118 |   friend class Action;
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   /** Wrapper function to add state to ActionHistory.
 | 
|---|
| 121 |    *
 | 
|---|
| 122 |    * \param _Action Action whose state to add
 | 
|---|
| 123 |    * \param _state state to add
 | 
|---|
| 124 |    */
 | 
|---|
| 125 |   void addElement(Action* _Action, ActionState::ptr _state);
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   /** Wrapper function to clear ActionHistory.
 | 
|---|
| 128 |    *
 | 
|---|
| 129 |    */
 | 
|---|
| 130 |   void clear();
 | 
|---|
| 131 | 
 | 
|---|
| 132 | private:
 | 
|---|
| 133 |   /** Private cstor for ActionQueue.
 | 
|---|
| 134 |    *
 | 
|---|
| 135 |    * Must be private as is singleton.
 | 
|---|
| 136 |    *
 | 
|---|
| 137 |    */
 | 
|---|
| 138 |   ActionQueue();
 | 
|---|
| 139 | 
 | 
|---|
| 140 |   /** Dstor for ActionQueue.
 | 
|---|
| 141 |    *
 | 
|---|
| 142 |    */
 | 
|---|
| 143 |   ~ActionQueue();
 | 
|---|
| 144 | 
 | 
|---|
| 145 |   //!> ActionRegistry to spawn new actions
 | 
|---|
| 146 |   ActionRegistry *AR;
 | 
|---|
| 147 | 
 | 
|---|
| 148 |   //!> ActionHistory is for undoing and redoing actions, requires ActionRegistry fully initialized
 | 
|---|
| 149 |   ActionHistory *history;
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   //!> internal queue of actions
 | 
|---|
| 152 |   ActionQueue_t queue;
 | 
|---|
| 153 | };
 | 
|---|
| 154 | 
 | 
|---|
| 155 | };
 | 
|---|
| 156 | 
 | 
|---|
| 157 | #endif /* ACTIONQUEUE_HPP_ */
 | 
|---|