7 #ifndef MT4CPP_COMMAND_HPP
8 #define MT4CPP_COMMAND_HPP
10 #include <boost/smart_ptr.hpp>
11 #include <boost/thread.hpp>
13 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
17 #pragma warning(disable:4355)
24 typedef long CommandID;
37 enum State { NONE, QUEUED, PENDING, INTERRUPTED, EXCEPTION, DONE };
67 typedef boost::shared_ptr<CommandObserver> PCommandObserver;
79 Progress(
const Progress& p ) : command_(p.command_), observers_(p.observers_), start_(p.start_), finish_(p.finish_) {}
83 Progress(
const Progress& parent,
double st,
double fi) : command_(parent.command_), observers_(parent.observers_) {
84 double wsp = parent.finish_ - parent.start_;
85 start_ = parent.start_ + st * wsp;
86 finish_ = parent.start_ + fi * wsp;
93 void setProgress(
double current );
101 std::for_each( observers_.begin(), observers_.end(),
106 void attach(PCommandObserver observer) { observers_.push_back(observer); }
112 std::vector<PCommandObserver> observers_;
122 Command() : counter_(0), haltFlag_(
false), progress_(*
this) {}
130 operator()(progress_);
132 }
catch(boost::thread_interrupted&) {
141 boost::mutex::scoped_lock lock(m_);
147 boost::mutex::scoped_lock lock(m_);
154 boost::mutex::scoped_lock lock(m_);
155 descriptor_.
state_ = new_state;
157 progress_.changeState( new_state );
163 boost::mutex::scoped_lock lock(m_);
164 descriptor_.
id_ = new_id;
174 if(haltFlag_)
throw boost::thread_interrupted();
185 friend void intrusive_ptr_release(
Command* ptr);
187 mutable boost::mutex m_;
191 virtual void operator()(
Progress& p) = 0;
196 volatile bool haltFlag_;
203 return cmd.getDescriptor().state_;
206 inline void intrusive_ptr_add_ref(
Command* cmd) {
207 boost::mutex::scoped_lock lock(cmd->m_);
210 inline void intrusive_ptr_release(
Command* cmd) {
213 boost::mutex::scoped_lock lock(cmd->m_);
214 del = ! --(cmd->counter_);
224 typedef boost::intrusive_ptr<Command> PCommand;
237 T* getObsCmd() {
return obsCmd_; }
238 PCommand
get() {
return cmd_; }
255 inline Progress::Progress( Command& cmd ) : command_(cmd), start_(0), finish_(1)
263 inline void Progress::setProgress(
double current ) {
265 command_.
setProgress( start_ + (finish_ - start_)*current );
266 std::for_each( observers_.begin(), observers_.end(),
273 std::for_each( observers_.begin(), observers_.end(),
279 #endif //MT4CPP_COMMAND_HPP
void attach(PCommandObserver observer)
add observer
Definition: Command.hpp:106
void setId(long new_id)
Definition: Command.hpp:162
CommandID id_
command unique ID
Definition: Command.hpp:39
void attach(PCommandObserver observer)
Definition: Command.hpp:181
void setState(CommandDesc::State new_state)
Definition: Command.hpp:152
State
available states of Command NONE - command created, but not put in activation queue QUEUED - command ...
Definition: Command.hpp:37
CommandDesc()
Definition: Command.hpp:44
void execute()
Definition: Command.hpp:127
Definition: Command.hpp:21
T CommandType
the command type
Definition: Command.hpp:233
double progress_
command progress
Definition: Command.hpp:41
Definition: Command.hpp:120
holder with the type and pointer to command as well as the base class of command (smart pointer) ...
Definition: Command.hpp:230
friend void intrusive_ptr_add_ref(Command *ptr)
Definition: Command.hpp:206
CommandDesc getDescriptor() const
Definition: Command.hpp:140
void setProgress(double new_progress)
Definition: Command.hpp:146
void halt()
Definition: Command.hpp:168
void step()
notify all observers, increase the progress
Definition: Command.hpp:271
class to set the progress of given command. Subject in Obverver design pattern. It also check the 'ha...
Definition: Command.hpp:74
Definition: Command.hpp:51
State state_
command state
Definition: Command.hpp:40
virtual void notifyProgress(const Command &, double progress)=0
virtual void notifyState(const Command &, CommandDesc::State state)=0
Progress(const Progress &parent, double st, double fi)
Definition: Command.hpp:83
int getCounter() const
Definition: Command.hpp:178
virtual void notifyStep(const Command &)=0
virtual ~Command()
Definition: Command.hpp:124
void checkHaltFlag() const
Definition: Command.hpp:173
the descriptor of command
Definition: Command.hpp:27