[628577] | 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 |
|
---|
[74459a] | 18 |
|
---|
| 19 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 20 | #include <boost/thread.hpp>
|
---|
[74459a] | 21 | #endif
|
---|
[690741] | 22 | #include <vector>
|
---|
[1d3563] | 23 |
|
---|
[f54cda] | 24 | #include "Actions/Action.hpp"
|
---|
[b5b01e] | 25 | #include "Actions/ActionState.hpp"
|
---|
[26b4eb4] | 26 | #include "Actions/ActionStatusList.hpp"
|
---|
[628577] | 27 |
|
---|
[74459a] | 28 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 29 | void stopQueue();
|
---|
| 30 | void waitQueue();
|
---|
[74459a] | 31 | #endif
|
---|
[415ddd] | 32 |
|
---|
[11d433] | 33 | class CommandLineParser;
|
---|
| 34 |
|
---|
[628577] | 35 | namespace MoleCuilder {
|
---|
| 36 |
|
---|
[6367dd] | 37 | class ActionHistory;
|
---|
[ed3944] | 38 | class ActionRegistry;
|
---|
| 39 | class ActionTrait;
|
---|
[628577] | 40 |
|
---|
| 41 | /** This class combines the whole handling of Actions into a single class.
|
---|
| 42 | *
|
---|
[1d3563] | 43 | * It spawns new Actions by its internal ActionRegistry. Spawned Actions are
|
---|
| 44 | * automatically queued and placed into a History after execution.
|
---|
[628577] | 45 | */
|
---|
| 46 | class ActionQueue : public Singleton<ActionQueue>
|
---|
| 47 | {
|
---|
| 48 | friend class Singleton<ActionQueue>;
|
---|
| 49 | public:
|
---|
[690741] | 50 | typedef std::vector<std::string> ActionTokens_t;
|
---|
[af5384] | 51 | typedef std::vector< Action * > ActionQueue_t;
|
---|
[1d3563] | 52 |
|
---|
[05c989] | 53 | /** Queues the Action with \a name to be called.
|
---|
| 54 | *
|
---|
[7fc447] | 55 | * \param name token of Action to actionqueue
|
---|
[f54cda] | 56 | * \param state whether Actions needs to be filled via a Dialog or not
|
---|
[05c989] | 57 | */
|
---|
[f54cda] | 58 | void queueAction(const std::string &name, enum Action::QueryOptions state = Action::Interactive);
|
---|
| 59 |
|
---|
| 60 | /** Queues the Action with \a name to be called.
|
---|
| 61 | *
|
---|
| 62 | * \param _action action to add
|
---|
| 63 | * \param state whether Actions needs to be filled via a Dialog or not
|
---|
| 64 | */
|
---|
[7f1a1a] | 65 | void queueAction(const Action * const _action, enum Action::QueryOptions state = Action::Interactive);
|
---|
[05c989] | 66 |
|
---|
[1d3563] | 67 | /** Returns the spawned action by token \a name.
|
---|
| 68 | *
|
---|
[7fc447] | 69 | * Action is checked into internal actionqueue.
|
---|
[1d3563] | 70 | *
|
---|
| 71 | * \return pointer to newly spawned action
|
---|
| 72 | */
|
---|
[a6ceab] | 73 | Action* getActionByName(const std::string &name);
|
---|
[1d3563] | 74 |
|
---|
| 75 | /** Checks whether the Action is known by the given \a name.
|
---|
| 76 | *
|
---|
| 77 | * \param name token of action to check
|
---|
| 78 | * \return true - token is known, false - else
|
---|
| 79 | */
|
---|
[a6ceab] | 80 | bool isActionKnownByName(const std::string &name) const;
|
---|
[1d3563] | 81 |
|
---|
[126867] | 82 | /** Register an Action with the ActionRegistry.
|
---|
| 83 | *
|
---|
| 84 | * \param _action action to add
|
---|
| 85 | */
|
---|
| 86 | void registerAction(Action *_action);
|
---|
| 87 |
|
---|
[690741] | 88 | /** Returns the vector with the tokens of all currently known Actions.
|
---|
| 89 | *
|
---|
| 90 | * \return list of all tokens
|
---|
| 91 | */
|
---|
| 92 | const ActionTokens_t getListOfActions() const;
|
---|
| 93 |
|
---|
| 94 | /** Returns the trait to an Action.
|
---|
| 95 | *
|
---|
| 96 | * \param name name of Action
|
---|
| 97 | * \return const ref to its default Trait.
|
---|
| 98 | */
|
---|
[a6ceab] | 99 | const ActionTrait& getActionsTrait(const std::string &name) const;
|
---|
[690741] | 100 |
|
---|
[7fc447] | 101 | /** Print the current contents of the actionqueue as CLI instantiated list of Actions.
|
---|
[46b181] | 102 | *
|
---|
| 103 | * This is useful for storing the current session.
|
---|
| 104 | *
|
---|
| 105 | * \param output output stream to print to
|
---|
| 106 | */
|
---|
| 107 | void outputAsCLI(std::ostream &output) const;
|
---|
| 108 |
|
---|
[7fc447] | 109 | /** Print the current contents of the actionqueue as Python instantiated list of Actions.
|
---|
[477012] | 110 | *
|
---|
| 111 | * This is useful for storing the current session.
|
---|
| 112 | *
|
---|
| 113 | * \param output output stream to print to
|
---|
| 114 | */
|
---|
| 115 | void outputAsPython(std::ostream &output) const;
|
---|
| 116 |
|
---|
[af5384] | 117 | /** Undoes last called Acfriend void ::cleanUp();tion.
|
---|
[6367dd] | 118 | *
|
---|
| 119 | */
|
---|
| 120 | void undoLast();
|
---|
| 121 |
|
---|
| 122 | /** Redoes last undone Action.
|
---|
| 123 | *
|
---|
| 124 | */
|
---|
| 125 | void redoLast();
|
---|
| 126 |
|
---|
[a61dbb] | 127 | /** Return status of last executed action.
|
---|
| 128 | *
|
---|
| 129 | * \return true - action executed correctly, false - else
|
---|
| 130 | */
|
---|
| 131 | bool getLastActionOk() const
|
---|
| 132 | { return lastActionOk; }
|
---|
| 133 |
|
---|
[74459a] | 134 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 135 | boost::thread &getRunThread()
|
---|
| 136 | { return run_thread; }
|
---|
[74459a] | 137 | #endif
|
---|
[af5384] | 138 |
|
---|
[26b4eb4] | 139 | /** Getter to ref to list of status messages.
|
---|
| 140 | *
|
---|
| 141 | * This is meant for UIs to registers as Observables.
|
---|
| 142 | *
|
---|
| 143 | * \return ref to StatusList variable
|
---|
| 144 | */
|
---|
| 145 | ActionStatusList& getStatusList()
|
---|
| 146 | { return StatusList; }
|
---|
| 147 |
|
---|
[6367dd] | 148 | private:
|
---|
| 149 | //!> grant Action access to internal history functions.
|
---|
| 150 | friend class Action;
|
---|
[11d433] | 151 | //!> grant CommandLineParser access to stop and clearQueue()
|
---|
| 152 | friend class ::CommandLineParser;
|
---|
[6367dd] | 153 |
|
---|
| 154 | /** Wrapper function to add state to ActionHistory.
|
---|
| 155 | *
|
---|
| 156 | * \param _Action Action whose state to add
|
---|
| 157 | * \param _state state to add
|
---|
| 158 | */
|
---|
| 159 | void addElement(Action* _Action, ActionState::ptr _state);
|
---|
| 160 |
|
---|
[26b4eb4] | 161 | /** Advocate function to add status message to the list.
|
---|
| 162 | *
|
---|
| 163 | */
|
---|
| 164 | void pushStatus(const std::string &_msg)
|
---|
| 165 | { StatusList.pushMessage(_msg); }
|
---|
| 166 |
|
---|
[6367dd] | 167 | /** Wrapper function to clear ActionHistory.
|
---|
| 168 | *
|
---|
| 169 | */
|
---|
| 170 | void clear();
|
---|
| 171 |
|
---|
[7f1a1a] | 172 | /** Clears all actions currently present in the actionqueues.
|
---|
| 173 | *
|
---|
| 174 | */
|
---|
| 175 | void clearQueue();
|
---|
| 176 |
|
---|
[74459a] | 177 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 178 | /** Runs the ActionQueue.
|
---|
| 179 | *
|
---|
| 180 | */
|
---|
| 181 | void run();
|
---|
| 182 |
|
---|
| 183 | friend void ::stopQueue();
|
---|
| 184 |
|
---|
| 185 | /** Stops the internal thread.
|
---|
| 186 | *
|
---|
| 187 | */
|
---|
| 188 | void stop();
|
---|
| 189 |
|
---|
| 190 | friend void ::waitQueue();
|
---|
| 191 |
|
---|
| 192 | /** Wait till all currently queued actions are processed.
|
---|
| 193 | *
|
---|
| 194 | */
|
---|
| 195 | void wait();
|
---|
[74459a] | 196 | #endif
|
---|
[415ddd] | 197 |
|
---|
[975b83] | 198 | /** Insert an action after CurrentAction.
|
---|
| 199 | *
|
---|
| 200 | * This is implemented only to allow action's COMMAND to work. If we
|
---|
| 201 | * were to use queueAction, actions would come after all other already
|
---|
| 202 | * present actions.
|
---|
| 203 | */
|
---|
| 204 | void insertAction(Action *_action, enum Action::QueryOptions state);
|
---|
| 205 |
|
---|
[415ddd] | 206 | /** Moves all action from tempqueue into real queue.
|
---|
| 207 | *
|
---|
| 208 | */
|
---|
| 209 | void insertTempQueue();
|
---|
| 210 |
|
---|
[628577] | 211 | private:
|
---|
[1d3563] | 212 | /** Private cstor for ActionQueue.
|
---|
| 213 | *
|
---|
| 214 | * Must be private as is singleton.
|
---|
| 215 | *
|
---|
| 216 | */
|
---|
[628577] | 217 | ActionQueue();
|
---|
[1d3563] | 218 |
|
---|
| 219 | /** Dstor for ActionQueue.
|
---|
| 220 | *
|
---|
| 221 | */
|
---|
[628577] | 222 | ~ActionQueue();
|
---|
| 223 |
|
---|
| 224 | //!> ActionRegistry to spawn new actions
|
---|
[ed3944] | 225 | ActionRegistry *AR;
|
---|
[1d3563] | 226 |
|
---|
[6367dd] | 227 | //!> ActionHistory is for undoing and redoing actions, requires ActionRegistry fully initialized
|
---|
| 228 | ActionHistory *history;
|
---|
| 229 |
|
---|
[7fc447] | 230 | //!> internal actionqueue of actions
|
---|
| 231 | ActionQueue_t actionqueue;
|
---|
[af5384] | 232 |
|
---|
[7fc447] | 233 | //!> point to current action in actionqueue
|
---|
[af5384] | 234 | size_t CurrentAction;
|
---|
[415ddd] | 235 |
|
---|
| 236 | //!> internal temporary actionqueue of actions used by insertAction()
|
---|
| 237 | ActionQueue_t tempqueue;
|
---|
| 238 |
|
---|
[a61dbb] | 239 | //!> indicates that the last action has failed
|
---|
| 240 | bool lastActionOk;
|
---|
| 241 |
|
---|
[74459a] | 242 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 243 | //!> internal thread to call Actions
|
---|
| 244 | boost::thread run_thread;
|
---|
| 245 |
|
---|
| 246 | //!> internal mutex to synchronize access to queue
|
---|
| 247 | boost::mutex mtx_queue;
|
---|
| 248 |
|
---|
| 249 | //!> conditional variable notifying when run_thread is idling
|
---|
| 250 | boost::condition_variable cond_idle;
|
---|
| 251 |
|
---|
| 252 | //!> flag indicating whether run_thread is idle or not
|
---|
| 253 | bool run_thread_isIdle;
|
---|
| 254 |
|
---|
| 255 | //!> internal mutex to synchronize access to run_thread_isIdle
|
---|
| 256 | boost::mutex mtx_idle;
|
---|
[74459a] | 257 | #endif
|
---|
[26b4eb4] | 258 |
|
---|
| 259 | //!> internal list of status messages from Actions for UIs to display
|
---|
| 260 | ActionStatusList StatusList;
|
---|
[628577] | 261 | };
|
---|
| 262 |
|
---|
| 263 | };
|
---|
| 264 |
|
---|
| 265 | #endif /* ACTIONQUEUE_HPP_ */
|
---|