forked from dacap/undo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathundo_history.cpp
220 lines (178 loc) · 4.25 KB
/
undo_history.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Undo Library
// Copyright (C) 2015-2022 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "undo_history.h"
#include "undo_command.h"
#include "undo_state.h"
#include <cassert>
#include <stack>
#define UNDO_TRACE(...)
namespace undo {
UndoHistory::UndoHistory(UndoHistoryDelegate* delegate)
: m_delegate(delegate)
, m_first(nullptr)
, m_last(nullptr)
, m_cur(nullptr)
{
}
UndoHistory::~UndoHistory()
{
m_cur = nullptr;
clearRedo();
}
bool UndoHistory::canUndo() const
{
return m_cur != nullptr;
}
bool UndoHistory::canRedo() const
{
return m_cur != m_last;
}
void UndoHistory::undo()
{
assert(m_cur);
if (!m_cur)
return;
assert(
(m_cur != m_first && m_cur->m_prev) ||
(m_cur == m_first && !m_cur->m_prev));
moveTo(m_cur->m_prev);
}
void UndoHistory::redo()
{
if (!m_cur)
moveTo(m_first);
else
moveTo(m_cur->m_next);
}
void UndoHistory::clearRedo()
{
UndoState* state = m_last;
// First we break the chain of undo states because deleteState() can
// generate an onDeleteUndoState() notification which might try to
// iterate over the whole UndoHistory.
if (m_cur) {
m_cur->m_next = nullptr;
m_last = m_cur;
}
else {
m_first = m_last = nullptr;
}
for (UndoState* prev = nullptr;
state && state != m_cur;
state = prev) {
prev = state->m_prev;
deleteState(state);
}
}
bool UndoHistory::deleteFirstState()
{
UNDO_TRACE("UndoHistory::deleteFirstState()\n");
// We cannot delete the first state if we are in the first state.
if (m_cur == m_first) {
UNDO_TRACE(" - Cannot delete first state if it's the current state\n");
return false;
}
UndoState* i = m_last;
while (i) {
// If this state depends on the delete one, this "i" is the new
// m_first undo state.
if (i->m_parent == m_first) {
// First we check if the current undo state is one of the states
// that we're going to delete.
UndoState* j = m_first;
while (j != i) {
// Cannot delete this "j" state because is the current one.
if (m_cur == j) {
UNDO_TRACE(" - Cannot delete first state because current state depends on it to go to the last state\n");
return false;
}
j = j->next();
}
j = m_first;
while (j != i) {
UNDO_TRACE(" - Delete undo state\n");
UndoState* k = j;
j = j->next();
deleteState(k);
}
i->m_prev = nullptr;
i->m_parent = nullptr;
m_first = i;
return true;
}
i = i->prev();
}
UndoState* state = m_first;
assert(m_last == m_first);
assert(m_first->next() == nullptr);
m_first = m_last = nullptr;
UNDO_TRACE(" - Delete first state only\n");
deleteState(state);
return true;
}
void UndoHistory::add(UndoCommand* cmd)
{
UndoState* state = new UndoState(cmd);
state->m_prev = m_last;
state->m_next = nullptr;
state->m_parent = m_cur;
if (!m_first)
m_first = state;
m_cur = m_last = state;
if (state->m_prev) {
assert(!state->m_prev->m_next);
state->m_prev->m_next = state;
}
}
const UndoState* UndoHistory::findCommonParent(const UndoState* a,
const UndoState* b)
{
const UndoState* pA = a;
const UndoState* pB = b;
if (pA == nullptr || pB == nullptr)
return nullptr;
while (pA != pB) {
pA = pA->m_parent;
if (!pA) {
pA = a;
pB = pB->m_parent;
if (!pB)
return nullptr;
}
}
return pA;
}
void UndoHistory::moveTo(const UndoState* new_state)
{
const UndoState* common = findCommonParent(m_cur, new_state);
if (m_cur) {
while (m_cur != common) {
m_cur->m_cmd->undo();
m_cur = m_cur->m_parent;
}
}
if (new_state) {
std::stack<const UndoState*> redo_parents;
const UndoState* p = new_state;
while (p != common) {
redo_parents.push(p);
p = p->m_parent;
}
while (!redo_parents.empty()) {
p = redo_parents.top();
redo_parents.pop();
p->m_cmd->redo();
}
}
m_cur = const_cast<UndoState*>(new_state);
}
void UndoHistory::deleteState(UndoState* state)
{
if (m_delegate)
m_delegate->onDeleteUndoState(state);
delete state;
}
} // namespace undo