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