mt4cpp
CommandHistory.hpp
Go to the documentation of this file.
1 /**
2  * \file CommandHistory.hpp
3  * \brief The collection of commands connected with scheduler.
4  */
5 
6 
7 #ifndef MT4CPP_COMMAND_HISTORY_HPP
8 #define MT4CPP_COMMAND_HISTORY_HPP
9 
10 #include <map>
11 
12 #include <boost/noncopyable.hpp>
13 
14 #include "Scheduler.hpp"
15 
16 namespace mt4cpp {
17 
18  /** the collection of commands, it can find the command by Id to give the command status
19  or progress etc. It is synchronized associative memory (std::map)
20  */
21  class CommandHistory : boost::noncopyable {
22  public:
23  CommandHistory() {}
24  ~CommandHistory() {}
25 
26  /** add the command to collection with given ID */
27  void insert(CommandID id, PCommand cmd) {
28  boost::mutex::scoped_lock lock(m_);
29  history_.insert( std::make_pair( id, cmd ) );
30  }
31 
32  /** try to find the Command of CommandId. If there is no command it returns the null pointer */
33  PCommand find(CommandID id) const {
34  boost::mutex::scoped_lock lock(m_);
35  std::map<CommandID, PCommand>::const_iterator i = history_.find(id);
36  if(i != history_.end() )
37  return i->second;
38  else
39  return PCommand(0L);
40  }
41 
42  /** CommandIDs stored in collection */
43  std::vector<CommandID> keys() const {
44  std::vector<CommandID> keys;
45  keys.reserve(history_.size());
46  typedef std::pair<CommandID, PCommand> Pair; //helping
47  std::transform(history_.begin(), history_.end(), std::back_inserter(keys), [](const Pair& p) -> CommandID { return p.first; });
48  return keys;
49  }
50 
51  /** clear the CommandHistory */
52  void clear() {
53  history_.clear();
54  }
55  private:
56  mutable boost::mutex m_;
57  std::map<CommandID, PCommand> history_;
58 
59  };
60 
61  /** \brief find the command descriptor of given ID.
62  If the command is not found in history returns default Descriptor (id=0, status=NONE)
63  */
64  inline CommandDesc findCommandDescriptor(const CommandHistory& history, CommandID id) {
65  PCommand cmd = history.find(id);
66  if(cmd.get() != 0L)
67  return cmd->getDescriptor();
68  else
69  return CommandDesc();
70  }
71 
72  /** helpers, first executes in Scheduler and insterts cmd to CommandHistory */
73  inline CommandID executeAsynchronouslyAndRemember(Scheduler& scheduler, CommandHistory& history, PCommand cmd) {
74  CommandID id = scheduler.executeAsynchronously(cmd);
75  history.insert(id, cmd);
76  return id;
77  }
78 
79  /** helpers, first executes in Scheduler and insterts cmd to CommandHistory */
80  inline CommandID executeSynchronouslyAndRemember(Scheduler& scheduler, CommandHistory& history, PCommand cmd) {
81  CommandID id = scheduler.executeSynchronously(cmd);
82  history.insert(id, cmd);
83  return id;
84  }
85 
86 } //namespace mt4cpp
87 
88 #endif //MT4CPP_COMMAND_HISTORY_HPP
std::vector< CommandID > keys() const
Definition: CommandHistory.hpp:43
void clear()
Definition: CommandHistory.hpp:52
Definition: CommandHistory.hpp:21
The active object scheduler module (Scheduler, CommandQueue and working thread main function) ...
void insert(CommandID id, PCommand cmd)
Definition: CommandHistory.hpp:27
Definition: Command.hpp:21
PCommand find(CommandID id) const
Definition: CommandHistory.hpp:33