Pioneer
Loading...
Searching...
No Matches
UndoStepType.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#pragma once
5
6#include "editor/UndoSystem.h"
7
8namespace Editor {
9
10 // ========================================================================
11 // UndoClosure Helper
12 // ========================================================================
13
14 // UndoClosure wraps an UndoStep with a closure to be executed after an
15 // Undo() or Redo() operation.
16
17 template<typename ClosureType, typename T>
18 class UndoClosure : public T {
19 public:
20 template<typename ...Args>
21 UndoClosure(ClosureType &&closure, Args ...args) :
22 T(args...),
23 m_onUpdate(closure)
24 {}
25
26 void Swap() override {
27 T::Swap();
28 m_onUpdate();
29 }
30
31 private:
32 ClosureType m_onUpdate;
33 };
34
35 // ========================================================================
36 // UndoSingleValue Helper
37 // ========================================================================
38
39 // UndoSingleValueStep implements an UndoStep that allows tracking mutation
40 // of a single public member variable in any class.
41 //
42 // The only requirements are that the value type be Move-Constructible or
43 // Copy-Constructible, and that the value's memory location will not change
44 // between creation of the UndoStep and when it is undone/redone.
45
46 template<typename ValueType>
48 {
49 public:
50 // Simple copy-state constructor to clone the given value
51 UndoSingleValueStep(ValueType *data) :
52 m_dataRef(data),
53 m_state(*m_dataRef)
54 {
55 }
56
57 // Convenience constructor overload to allow updating a data value in a single call
58 // (allows UndoSingleValueStep to work for move-only values like std::unique_ptr)
59 UndoSingleValueStep(ValueType *data, const ValueType &newValue) :
60 m_dataRef(data),
61 m_state(newValue)
62 {
63 }
64
65 void Swap() override { std::swap(*m_dataRef, m_state); }
66
67 // Implement HasChanged as !(a == b) to reduce the number of operator overloads required
68 bool HasChanged() const override { return !(*m_dataRef == m_state); }
69
70 private:
71 ValueType *m_dataRef;
72 ValueType m_state;
73 };
74
75 // Helper functions to construct the above UndoStep helpers
76
77 template<typename T>
78 inline void AddUndoSingleValue(UndoSystem *s, T *dataRef)
79 {
81 }
82
83 template<typename T>
84 inline void AddUndoSingleValue(UndoSystem *s, T *dataRef, const T &newValue)
85 {
86 s->AddUndoStep<UndoSingleValueStep<T>>(dataRef, newValue)->Swap();
87 }
88
89 template<typename T, typename UpdateClosure>
90 inline void AddUndoSingleValueClosure(UndoSystem *s, T *dataRef, UpdateClosure closure)
91 {
92 s->AddUndoStep<UndoClosure<UpdateClosure, UndoSingleValueStep<T>>>(std::move(closure), dataRef);
93 }
94
95 template<typename T, typename UpdateClosure>
96 inline void AddUndoSingleValueClosure(UndoSystem *s, T *dataRef, const T &newValue, UpdateClosure closure)
97 {
98 s->AddUndoStep<UndoClosure<UpdateClosure, UndoSingleValueStep<T>>>(std::move(closure), dataRef, newValue)->Swap();
99 }
100
101 // ========================================================================
102 // UndoGetSetValue Helper
103 // ========================================================================
104
105 // UndoGetSetValueStep implements an UndoStep that allows tracking mutation
106 // of a single private member variable exposed by a GetX() / SetX() pair.
107 //
108 // The value type must be Copy-Constructible, and the object containing the
109 // variable must not be reallocated or destroyed during the lifetime of the
110 // UndoStep.
111
112 template<auto GetterFn, auto SetterFn, typename ValueType, typename Obj>
114 {
115 public:
117 m_dataRef(data),
118 m_state((m_dataRef->*GetterFn)())
119 {
120 }
121
122 // Convenience constructor to allow updating the value in a single call
123 UndoGetSetValueStep(Obj *data, const ValueType &newValue) :
124 m_dataRef(data),
125 m_state(newValue)
126 {
127 }
128
129 // two-way swap with opaque setter/getter functions
130 void Swap() override {
131 ValueType t = (m_dataRef->*GetterFn)();
132 std::swap(t, m_state);
133 (m_dataRef->*SetterFn)(std::move(t));
134 }
135
136 bool HasChanged() const override { return !((m_dataRef->*GetterFn)() == m_state); }
137
138 private:
139 Obj *m_dataRef;
140 ValueType m_state;
141 };
142
143 // Helper functions to construct the above UndoStep helpers
144
145 template<auto GetterFn, auto SetterFn, typename Obj>
146 inline void AddUndoGetSetValue(UndoSystem *s, Obj *dataRef)
147 {
148 using ValueType = decltype((dataRef->*GetterFn)());
150 }
151
152 template<auto GetterFn, auto SetterFn, typename Obj, typename T>
153 inline void AddUndoGetSetValue(UndoSystem *s, Obj *dataRef, const T &newValue)
154 {
155 using ValueType = decltype((dataRef->*GetterFn)());
157 }
158
159 template<auto GetterFn, auto SetterFn, typename Obj, typename UpdateClosure>
160 inline void AddUndoGetSetValueClosure(UndoSystem *s, Obj *dataRef, UpdateClosure &&closure)
161 {
162 using ValueType = decltype((dataRef->*GetterFn)());
164 }
165
166 template<auto GetterFn, auto SetterFn, typename Obj, typename T, typename UpdateClosure>
167 inline void AddUndoGetSetValueClosure(UndoSystem *s, Obj *dataRef, const T &newValue, UpdateClosure &&closure)
168 {
169 using ValueType = decltype((dataRef->*GetterFn)());
171 }
172
173 // ========================================================================
174 // UndoVectorStep Helper
175 // ========================================================================
176
177 // UndoVectorStep implements an UndoStep that expresses mutation of a
178 // std::vector or similar container variable providing insert(), erase(),
179 // begin(), and size().
180 //
181 // The only requirements are that the value type be Move-Constructible or
182 // Copy-Constructible, and that the container's memory location will not
183 // change between creation of the UndoStep and when it is undone/redone.
184
185 template<typename Container>
187 {
188 public:
189 using ValueType = typename Container::value_type;
190
191 UndoVectorStep(Container *container, const ValueType &newValue, size_t insertIdx) :
192 m_container(*container),
193 m_value {},
194 m_idx(insertIdx),
195 m_insert(true)
196 {
197 // NOTE: the transaction is not yet committed until Swap() is called (for compatibility with UndoClosure)
198 }
199
200 UndoVectorStep(Container *container, size_t removeIdx) :
201 m_container(*container),
202 m_value {},
203 m_idx(removeIdx),
204 m_insert(false)
205 {
206 // NOTE: the transaction is not yet committed until Swap() is called (for compatibility with UndoClosure)
207 }
208
209 void Swap() override
210 {
211 if (m_insert) {
212 m_container.insert(m_container.begin() + m_idx, std::move(m_value));
213 } else {
214 m_value = std::move(m_container[m_idx]);
215 m_container.erase(m_container.begin() + m_idx);
216 }
217
218 m_insert = !m_insert;
219 }
220
221 private:
222 Container &m_container;
223 ValueType m_value;
224 size_t m_idx;
225 bool m_insert;
226 };
227
228 // ========================================================================
229 // UndoVectorSingleValue Helper
230 // ========================================================================
231
232 // UndoVectorSingleValue implements an UndoStep that expresses mutation of
233 // a value contained in a std::vector or similar container variable
234 // providing operator[](size_t) and size()
235 //
236 // The only requirements are that the value type be Move-Constructible or
237 // Copy-Constructible, and that the container's memory location will not
238 // change between creation of the UndoStep and when it is undone/redone.
239 //
240 // For containers that reallocate memory on mutation (e.g. std::vector)
241 // this UndoStep is a replacement for UndoSingleValueStep.
242
243 template<typename Container>
245 {
246 public:
247 using ValueType = typename Container::value_type;
248
249 UndoVectorSingleValueStep(Container *container, size_t idx) :
250 m_container(*container),
251 m_index(idx),
252 m_state(m_container[idx])
253 {
254 }
255
256 void Swap() override { std::swap(m_container[m_index], m_state); }
257
258 bool HasChanged() const override { return !(m_container[m_index] == m_state); }
259
260 private:
261 Container &m_container;
262 size_t m_index;
263 ValueType m_state;
264 };
265
266 // Helper functions to construct the above UndoStep helpers
267
268 template<typename T, typename Value = typename T::value_type>
269 inline void AddUndoVectorInsert(UndoSystem *s, T *containerRef, const Value &value, size_t idx = -1)
270 {
271 if (idx == size_t(-1))
272 idx = containerRef->size();
273
274 s->AddUndoStep<UndoVectorStep<T>>(containerRef, value, idx)->Swap();
275 }
276
277 template<typename T>
278 inline void AddUndoVectorErase(UndoSystem *s, T *containerRef, size_t idx = -1)
279 {
280 if (idx == size_t(-1))
281 idx = containerRef->size() - 1;
282
283 s->AddUndoStep<UndoVectorStep<T>>(containerRef, idx)->Swap();
284 }
285
286 template<typename T>
287 inline void AddUndoVectorSingleValue(UndoSystem *s, T *containerRef, size_t idx = -1)
288 {
289 if (idx == size_t(-1))
290 idx = containerRef->size() - 1;
291
292 s->AddUndoStep<UndoVectorSingleValueStep<T>>(containerRef, idx);
293 }
294
295 template<typename T, typename UpdateClosure, typename Value = typename T::value_type>
296 inline void AddUndoVectorInsertClosure(UndoSystem *s, T *containerRef, const Value &value, size_t idx, UpdateClosure closure)
297 {
298 s->AddUndoStep<UndoClosure<UpdateClosure, UndoVectorStep<T>>>(std::move(closure), containerRef, value, idx)->Swap();
299 }
300
301 template<typename T, typename UpdateClosure>
302 inline void AddUndoVectorEraseClosure(UndoSystem *s, T *containerRef, size_t idx, UpdateClosure closure)
303 {
304 s->AddUndoStep<UndoClosure<UpdateClosure, UndoVectorStep<T>>>(std::move(closure), containerRef, idx)->Swap();
305 }
306
307 template<typename T, typename UpdateClosure>
308 inline void AddUndoVectorSingleValue(UndoSystem *s, T *containerRef, size_t idx, UpdateClosure closure)
309 {
310 if (idx == size_t(-1))
311 idx = containerRef->size() - 1;
312
313 s->AddUndoStep<UndoClosure<UpdateClosure, UndoVectorSingleValueStep<T>>>(std::move(closure), containerRef, idx);
314 }
315
316} // namespace Editor
Definition UndoStepType.h:18
void Swap() override
Definition UndoStepType.h:26
UndoClosure(ClosureType &&closure, Args ...args)
Definition UndoStepType.h:21
Definition UndoStepType.h:114
void Swap() override
Definition UndoStepType.h:130
UndoGetSetValueStep(Obj *data)
Definition UndoStepType.h:116
UndoGetSetValueStep(Obj *data, const ValueType &newValue)
Definition UndoStepType.h:123
bool HasChanged() const override
Definition UndoStepType.h:136
Definition UndoStepType.h:48
UndoSingleValueStep(ValueType *data)
Definition UndoStepType.h:51
bool HasChanged() const override
Definition UndoStepType.h:68
UndoSingleValueStep(ValueType *data, const ValueType &newValue)
Definition UndoStepType.h:59
void Swap() override
Definition UndoStepType.h:65
Definition UndoSystem.h:31
Definition UndoSystem.h:112
T * AddUndoStep(Args &&...args)
Definition UndoSystem.h:153
Definition UndoStepType.h:245
bool HasChanged() const override
Definition UndoStepType.h:258
void Swap() override
Definition UndoStepType.h:256
UndoVectorSingleValueStep(Container *container, size_t idx)
Definition UndoStepType.h:249
typename Container::value_type ValueType
Definition UndoStepType.h:247
Definition UndoStepType.h:187
typename Container::value_type ValueType
Definition UndoStepType.h:189
UndoVectorStep(Container *container, size_t removeIdx)
Definition UndoStepType.h:200
UndoVectorStep(Container *container, const ValueType &newValue, size_t insertIdx)
Definition UndoStepType.h:191
void Swap() override
Definition UndoStepType.h:209
Definition ActionBinder.h:14
void AddUndoVectorInsertClosure(UndoSystem *s, T *containerRef, const Value &value, size_t idx, UpdateClosure closure)
Definition UndoStepType.h:296
void AddUndoSingleValueClosure(UndoSystem *s, T *dataRef, UpdateClosure closure)
Definition UndoStepType.h:90
void AddUndoGetSetValue(UndoSystem *s, Obj *dataRef)
Definition UndoStepType.h:146
void AddUndoSingleValue(UndoSystem *s, T *dataRef)
Definition UndoStepType.h:78
void AddUndoVectorErase(UndoSystem *s, T *containerRef, size_t idx=-1)
Definition UndoStepType.h:278
void AddUndoVectorSingleValue(UndoSystem *s, T *containerRef, size_t idx=-1)
Definition UndoStepType.h:287
void AddUndoVectorInsert(UndoSystem *s, T *containerRef, const Value &value, size_t idx=-1)
Definition UndoStepType.h:269
void AddUndoVectorEraseClosure(UndoSystem *s, T *containerRef, size_t idx, UpdateClosure closure)
Definition UndoStepType.h:302
void AddUndoGetSetValueClosure(UndoSystem *s, Obj *dataRef, UpdateClosure &&closure)
Definition UndoStepType.h:160