Loading...
Searching...
No Matches
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#include <score/selection/SelectionStack.hpp>
6
7#include <memory>
8
17template <typename Command_T, typename RedoStrategy_T, typename SendStrategy_T>
19{
20public:
21 template <typename... Args>
22 GenericMacroCommandDispatcher(Args&&... args)
23 : ICommandDispatcher{std::forward<Args&&>(args)...}
24 , m_aggregateCommand{std::make_unique<Command_T>()}
25 {
26 static_assert(
27 std::is_base_of<score::AggregateCommand, Command_T>::value,
28 "MacroCommandDispatcher: Command_T must be AggregateCommand-derived");
29 }
30
31 template <typename... Args>
33 std::unique_ptr<score::AggregateCommand> cmd, Args&&... args)
34 : ICommandDispatcher{std::forward<Args&&>(args)...}
35 , m_aggregateCommand{std::move(cmd)}
36 {
37 SCORE_ASSERT(m_aggregateCommand);
38 }
39
40 void submit(score::Command* cmd)
41 {
42 {
43 score::SelectionStack::Batch b{stack().selectionStack()};
44 RedoStrategy_T::redo(stack().context(), *cmd);
45 }
46 m_aggregateCommand->addCommand(cmd);
47 }
48
49 void commit()
50 {
51 if(m_aggregateCommand)
52 {
53 if(m_aggregateCommand->count() != 0)
54 {
55 SendStrategy_T::send(stack(), m_aggregateCommand.release());
56 }
57
58 m_aggregateCommand.reset();
59 }
60 }
61
62 void rollback()
63 {
64 if(m_aggregateCommand)
65 {
66 score::SelectionStack::Batch b{stack().selectionStack()};
67 m_aggregateCommand->undo(stack().context());
68 m_aggregateCommand.reset();
69 }
70 }
71
72 void release() { m_aggregateCommand.release(); }
73
74 auto command() const { return m_aggregateCommand.get(); }
75
76protected:
77 std::unique_ptr<Command_T> m_aggregateCommand;
78};
79
80// Don't redo the individual commands, and redo() the aggregate command.
81template <typename Command_T>
84
85// Redo the individual commands, don't redo the aggregate command
86template <typename Command_T>
89
90// Don't redo anything, just push
91template <typename Command_T>
The MacroCommandDispatcher class.
Definition MacroCommandDispatcher.hpp:19
The ICommandDispatcher class.
Definition ICommandDispatcher.hpp:21
The Command class.
Definition Command.hpp:34
Definition SelectionStack.hpp:72
Definition SendStrategy.hpp:39
Definition SendStrategy.hpp:7