MacroCommandDispatcher.hpp
1 #pragma once
2 #include <score/command/AggregateCommand.hpp>
3 #include <score/command/Dispatchers/ICommandDispatcher.hpp>
4 #include <score/command/Dispatchers/SendStrategy.hpp>
5 
6 #include <memory>
7 
16 template <typename Command_T, typename RedoStrategy_T, typename SendStrategy_T>
18 {
19 public:
20  template <typename... Args>
21  GenericMacroCommandDispatcher(Args&&... args)
22  : ICommandDispatcher{std::forward<Args&&>(args)...}
23  , m_aggregateCommand{std::make_unique<Command_T>()}
24  {
25  static_assert(
26  std::is_base_of<score::AggregateCommand, Command_T>::value,
27  "MacroCommandDispatcher: Command_T must be AggregateCommand-derived");
28  }
29 
30  template <typename... Args>
32  std::unique_ptr<score::AggregateCommand> cmd, Args&&... args)
33  : ICommandDispatcher{std::forward<Args&&>(args)...}
34  , m_aggregateCommand{std::move(cmd)}
35  {
36  SCORE_ASSERT(m_aggregateCommand);
37  }
38 
39  void submit(score::Command* cmd)
40  {
41  RedoStrategy_T::redo(stack().context(), *cmd);
42  m_aggregateCommand->addCommand(cmd);
43  }
44 
45  void commit()
46  {
47  if(m_aggregateCommand)
48  {
49  if(m_aggregateCommand->count() != 0)
50  {
51  SendStrategy_T::send(stack(), m_aggregateCommand.release());
52  }
53 
54  m_aggregateCommand.reset();
55  }
56  }
57 
58  void rollback()
59  {
60  if(m_aggregateCommand)
61  {
62  m_aggregateCommand->undo(stack().context());
63  m_aggregateCommand.reset();
64  }
65  }
66 
67  auto command() const { return m_aggregateCommand.get(); }
68 
69 protected:
70  std::unique_ptr<Command_T> m_aggregateCommand;
71 };
72 
73 // Don't redo the individual commands, and redo() the aggregate command.
74 template <typename Command_T>
77 
78 // Redo the individual commands, don't redo the aggregate command
79 template <typename Command_T>
82 
83 // Don't redo anything, just push
84 template <typename Command_T>
The MacroCommandDispatcher class.
Definition: MacroCommandDispatcher.hpp:18
The ICommandDispatcher class.
Definition: ICommandDispatcher.hpp:21
The Command class.
Definition: Command.hpp:34
Definition: SendStrategy.hpp:42
Definition: SendStrategy.hpp:7