SingleOngoingCommandDispatcher.hpp
1 #pragma once
2 #include <score/command/CommandStackFacade.hpp>
3 #include <score/command/Dispatchers/ICommandDispatcher.hpp>
4 #include <score/command/Dispatchers/SendStrategy.hpp>
5 
6 #include <memory>
7 
15 template <typename TheCommand>
17 {
18 public:
20  : ICommandDispatcher{stack}
21  {
22  }
23 
24  template <typename... Args>
25  void submit(Args&&... args)
26  {
27  if(!m_cmd)
28  {
29  stack().disableActions();
30  m_cmd = std::make_unique<TheCommand>(std::forward<Args>(args)...);
31  m_cmd->redo(stack().context());
32  }
33  else
34  {
35  m_cmd->update(std::forward<Args>(args)...);
36  m_cmd->redo(stack().context());
37  }
38  }
39 
40  void commit()
41  {
42  if(m_cmd)
43  {
44  SendStrategy::Quiet::send(stack(), m_cmd.release());
45  stack().enableActions();
46  }
47  }
48 
49  void rollback()
50  {
51  if(m_cmd)
52  {
53  m_cmd->undo(stack().context());
54  stack().enableActions();
55  }
56  m_cmd.reset();
57  }
58 
59 private:
60  std::unique_ptr<TheCommand> m_cmd;
61 };
The ICommandDispatcher class.
Definition: ICommandDispatcher.hpp:21
The SingleOngoingCommandDispatcher class.
Definition: SingleOngoingCommandDispatcher.hpp:17
A small abstraction layer over the score::CommandStack.
Definition: CommandStackFacade.hpp:20