-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemento.h
244 lines (197 loc) · 5.55 KB
/
Memento.h
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#pragma once
#include"RxConcurrent/CommonDefines.h"
#include"RxConcurrent/StateMachineCollections.h"
#include"RxConcurrent/Export.h"
namespace BaseLib { namespace Templates
{
template <typename T>
class Memento : protected ComparableMutableType<T>
{
public:
Memento() : ComparableMutableType<T>()
{}
Memento(const T &t) : ComparableMutableType<T>(t)
{}
virtual ~Memento()
{}
T GetState() const
{
return this->Clone();
}
void SetState(const T &t)
{
this->Set(t);
}
};
/**
-> Identify a class that needs to be able to take a snapshot of its state.(the originator role.)
-> Design a class that does nothing more than accept and return this snapshot.(The memento role).
-> Caretaker Role asks the Originator to return a Memento and cause the Originator's previous state to be restored of desired.
-> Notion of undo or rollback has now been objectified(i.e promoted to full object status).
*/
template <typename T>
class Originator : public ComparableMutableType< Memento<T> >
{
public:
virtual ~Originator()
{}
CLASS_TRAITS(Originator)
virtual bool SetMemento(Memento<T> t)
{
this->Set(t);
return true;
}
virtual Memento<T> CreateMemento() const
{
return this->Clone();
}
virtual void SetOriginatorState(const T &t)
{
this->Set(Memento<T>(t));
}
virtual T GetOriginatorState() const
{
return this->delegate().GetState();
}
};
// ------------------------------------------------
// Action base classes to be implemented
// ------------------------------------------------
template <typename T, typename Return>
class MementoAction0
: public Action0<Return>
, public Originator<T>
{
public:
virtual ~MementoAction0() {}
CLASS_TRAITS(MementoAction0)
};
template <typename T, typename Return, typename Arg1>
class MementoAction1
: public Action1<Return, Arg1>
, public Originator<T>
{
public:
virtual ~MementoAction1() {}
CLASS_TRAITS(MementoAction1)
};
template <typename T, typename Return, typename Arg1, typename Arg2>
class MementoAction2
: public Action2<Return, Arg1, Arg2>
, public Originator<T>
{
public:
virtual ~MementoAction2() {}
CLASS_TRAITS(MementoAction2)
};
template <typename T, typename Return, typename Arg1, typename Arg2, typename Arg3>
class MementoAction3
: public Action3<Return, Arg1, Arg2, Arg3>
, public Originator<T>
{
public:
virtual ~MementoAction3() {}
CLASS_TRAITS(MementoAction3)
};
template <typename T, typename Return, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
class MementoAction4
: public Action4<Return, Arg1, Arg2, Arg3, Arg4>
, public Originator<T>
{
public:
virtual ~MementoAction4() {}
CLASS_TRAITS(MementoAction4)
};
template <typename T, typename Return, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
class MementoAction5
: public Action5<Return, Arg1, Arg2, Arg3, Arg4, Arg5>
, public Originator<T>
{
public:
virtual ~MementoAction5() {}
CLASS_TRAITS(MementoAction5)
};
// ------------------------------------------------
// Undo redo base
// ------------------------------------------------
class DLL_STATE UndoRedoBase
{
public:
virtual ~UndoRedoBase();
virtual bool CreateMemento() const = 0;
virtual bool Undo() = 0;
virtual bool Redo() = 0;
virtual void ClearUndo() = 0;
virtual void ClearRedo() = 0;
};
/**
* Perhaps inherit from map and implement specialized iterator functionality for undo, redo, etc.
*
* TODO: Use collections for states: UNDO, REDO, CURRENT
*
* CURRENT -> UNDO
*
* Use Caretaker in SharedTypePtr
*/
template <typename T>
class Caretaker : public UndoRedoBase
, public Lockable<Mutex>
{
private:
typedef Concurrent::StateMachineCollections<std::string, Originator<T>> StateMachineCollection;
typedef Concurrent::StateCollection<std::string, Originator<T>> MementoCollection;
public:
Caretaker(typename Originator<T>::Ptr originator)
: stateCollections_(new StateMachineCollection())
, originator_(originator)
{}
virtual ~Caretaker()
{}
CLASS_TRAITS(Caretaker)
virtual bool CreateMemento() const
{
Memento<T> memento = originator_->CreateMemento();
typename MementoCollection::Ptr collection = stateCollections_->GetCollection("UNDO");
collection->template AddAction<bool, Memento<T>>(originator_, &Originator<T>::SetMemento, memento);
stateCollections_->ClearCollection("REDO");
return true;
}
virtual bool Undo()
{
return stateCollections_->ExecuteNObjectsAndMove(1, "UNDO", "REDO");
}
virtual bool Redo()
{
return stateCollections_->ExecuteNObjectsAndMove(1, "REDO", "UNDO");
}
virtual void ClearUndo()
{
stateCollections_->ClearCollection("UNDO");
}
virtual void ClearRedo()
{
stateCollections_->ClearCollection("REDO");
}
private:
mutable typename StateMachineCollection::Ptr stateCollections_;
mutable typename Originator<T>::Ptr originator_;
};
/**
* @brief The Caretakers class
*
* TODO: Make synchronized
*/
class DLL_STATE Caretakers
: public Collection::IList<std::shared_ptr<UndoRedoBase>>
, public UndoRedoBase
{
public:
Caretakers();
virtual ~Caretakers();
virtual bool CreateMemento() const;
virtual bool Undo();
virtual bool Redo();
virtual void ClearUndo();
virtual void ClearRedo();
};
}}