OngoingCommandDispatcher.hpp
1 #pragma once
2 #include <score/command/Dispatchers/ICommandDispatcher.hpp>
3 #include <score/command/Dispatchers/SendStrategy.hpp>
4 #include <score/plugins/StringFactoryKey.hpp>
5 #include <score/tools/Debug.hpp>
6 #include <score/tools/SafeCast.hpp>
7 
8 #include <memory>
9 
27 {
28 public:
30  : ICommandDispatcher{stack}
31  {
32  }
33 
36  template <typename TheCommand, typename... Args>
37  void submit(Args&&... args)
38  {
39  if(!m_cmd)
40  {
41  stack().disableActions();
42  m_cmd = std::make_unique<TheCommand>(std::forward<Args>(args)...);
43  m_cmd->redo(stack().context());
44  }
45  else
46  {
47  if(m_cmd->key() != TheCommand::static_key())
48  {
49  throw std::runtime_error(
50  "Ongoing command mismatch: current command " + m_cmd->key().toString()
51  + " does not match new command " + TheCommand{}.key().toString());
52  }
53 
54  safe_cast<TheCommand*>(m_cmd.get())->update(std::forward<Args>(args)...);
55  m_cmd->redo(stack().context());
56  }
57  }
58 
61  void commit()
62  {
63  if(m_cmd)
64  {
65  SendStrategy::Quiet::send(stack(), m_cmd.release());
66  stack().enableActions();
67  }
68  }
69 
71  void rollback()
72  {
73  if(m_cmd)
74  {
75  m_cmd->undo(stack().context());
76  stack().enableActions();
77  }
78  m_cmd.reset();
79  }
80 
81 private:
82  std::unique_ptr<score::Command> m_cmd;
83 };
The ICommandDispatcher class.
Definition: ICommandDispatcher.hpp:21
The OngoingCommandDispatcher class.
Definition: OngoingCommandDispatcher.hpp:27
void submit(Args &&... args)
Definition: OngoingCommandDispatcher.hpp:37
void commit()
Definition: OngoingCommandDispatcher.hpp:61
void rollback()
If the command has to be reverted, for instance when pressing escape.
Definition: OngoingCommandDispatcher.hpp:71
A small abstraction layer over the score::CommandStack.
Definition: CommandStackFacade.hpp:20