Loading...
Searching...
No Matches
CommandStackSerialization.hpp
1#pragma once
2#include <score/application/ApplicationComponents.hpp>
3#include <score/command/CommandData.hpp>
4#include <score/plugins/StringFactoryKeySerialization.hpp>
6
7#include <core/command/CommandStack.hpp>
8namespace score
9{
10template <typename RedoFun>
11void loadCommandStack(
12 const score::ApplicationComponents& components, DataStreamWriter& writer,
13 score::CommandStack& stack, RedoFun redo_fun)
14{
15 std::vector<score::CommandData> undoStack, redoStack;
16 writer.writeTo(undoStack);
17 writer.writeTo(redoStack);
18
19 writer.checkDelimiter();
20
21 // The stack owns its commands (see ~CommandStack).
22 qDeleteAll(stack.undoable());
23 stack.undoable().clear();
24 qDeleteAll(stack.redoable());
25 stack.redoable().clear();
26
27 stack.updateStack([&]() {
28 stack.setSavedIndex(-1);
29
30 // The replay stops at the first command unknown to this build
31 auto make = [&](const score::CommandData& elt) -> score::Command* {
32 try
33 {
34 return components.instantiateUndoCommand(elt);
35 }
36 catch(...)
37 {
38 return nullptr;
39 }
40 };
41
42 bool ok = true;
43 for(const auto& elt : undoStack)
44 {
45 auto cmd = make(elt);
46 if(cmd && redo_fun(cmd))
47 {
48 stack.undoable().push(cmd);
49 }
50 else
51 {
52 delete cmd;
53 ok = false;
54 break;
55 }
56 }
57
58 if(ok)
59 {
60 for(const auto& elt : redoStack)
61 {
62 auto cmd = make(elt);
63 if(!cmd)
64 break;
65 stack.redoable().push(cmd);
66 }
67 }
68 });
69}
70}
Definition DataStreamVisitor.hpp:202
void checkDelimiter()
checkDelimiter
Definition DataStreamVisitor.cpp:47
Definition ApplicationComponents.hpp:68
The Command class.
Definition Command.hpp:34
Definition CommandStack.hpp:27
void updateStack(Callable &&c)
updateStack Updates the undo / redo stack
Definition CommandStack.hpp:190
Base toolkit upon which the software is built.
Definition Application.cpp:83
Utility class for the serialization and deserialization of commands.
Definition CommandData.hpp:14