-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMementoContextObjectPtr.h
264 lines (220 loc) · 6.53 KB
/
MementoContextObjectPtr.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#pragma once
#include"RxConcurrent/CommonDefines.h"
#include"RxConcurrent/Memento.h"
#include"RxConcurrent/MementoSharedType.h"
namespace BaseLib { namespace Templates
{
/**
* ContextObject is designed after the software design pattern "Context Object".
*
* Config = configuration, policies, qos, strategies, etc
* State = object state that develops in its lifetime, but not critical to persist
* CriticalState = object and system state that is critical for the entire system
*
* TODO:
* data = objects internal data vital to its operation, i.e., not state machine stuff.
*
* sm = Access to state machine objects
* sm().config = Access to state policies to be applied in state machine
* sm().status = Object's status, i.e., count, time, etc, to be used in a state machine
* sm().state = Object's state, i.e., current state in the state machine.
* Can be an implementation of an interface for all states possible in a state machine
* sm().subject = All subject events originating from this
*
* sm().function = All std::function or strategy objects that are configured for the API
*
* TODO: Introduce Factory/Builder for construction as a static
* - Factory factory();
* - Builder builder();
*
* TODO: Introduce Status which is the "object's Status object, i.e., state in a state machine"
* TODO: Instead of state() use data() for object's internals that are to be made accessible
*
* TODO: Policy for "Copy on write", "Copy on read", No copy,
* TODO: Possible to have policy on Write access, read access?
* TODO: Use memento to create a snapshot, and support setting a memento
* TODO: Introduce subject()?
*/
template <typename Config, typename Data, typename Status = NullState, typename State = NullState>
class MementoContextObjectShared
: public UndoRedoBase
, public InitializeMethods
{
public:
MementoContextObjectShared(Config *config, Data *data, Status *status, State *state)
: data_(new MementoSharedTypePtr<Data>(data))
, config_(new MementoSharedTypePtr<Config>(config))
, status_(new MementoSharedTypePtr<Status>(status))
, state_(new MementoSharedTypePtr<State>(state))
{
Initialize();
}
MementoContextObjectShared(Config *config, Data *data, Status *status)
: data_(new MementoSharedTypePtr<Data>(data))
, config_(new MementoSharedTypePtr<Config>(config))
, status_(new MementoSharedTypePtr<Status>(status))
, state_(new MementoSharedTypePtr<State>(new State()))
{
Initialize();
}
MementoContextObjectShared(Config *config, Data *data)
: data_(new MementoSharedTypePtr<Data>(data))
, config_(new MementoSharedTypePtr<Config>(config))
, status_(new MementoSharedTypePtr<Status>(new Status()))
, state_(new MementoSharedTypePtr<State>(new State()))
{
Initialize();
}
virtual ~MementoContextObjectShared()
{}
const MementoSharedTypePtr<Data>& data() const
{
return data_.operator *();
}
MementoSharedTypePtr<Data>& data()
{
return data_.operator *();
}
const MementoSharedTypePtr<Config>& config() const
{
return config_.operator *();
}
MementoSharedTypePtr<Config>& config()
{
return config_.operator *();
}
const MementoSharedTypePtr<Status>& status() const
{
return status_.operator *();
}
MementoSharedTypePtr<Status>& status()
{
return status_.operator *();
}
const MementoSharedTypePtr<State>& state() const
{
return state_.operator *();
}
MementoSharedTypePtr<State>& state()
{
return state_.operator *();
}
// -------------------------------------
// UndoRedoBase interface
// -------------------------------------
virtual bool CreateMemento() const
{
caretaker_.CreateMemento();
}
virtual bool Undo()
{
caretaker_.Undo();
}
virtual bool Redo()
{
caretaker_.Redo();
}
virtual void ClearUndo()
{
caretaker_.ClearUndo();
}
virtual void ClearRedo()
{
caretaker_.ClearRedo();
}
// -------------------------------------
// InitializeMethods
// -------------------------------------
virtual bool Initalize()
{
caretaker_.push_back(data_);
caretaker_.push_back(config_);
caretaker_.push_back(status_);
caretaker_.push_back(state_);
return true;
}
virtual bool IsInitialized() const
{
return !caretaker_.empty();
}
private:
std::shared_ptr<MementoSharedTypePtr<Data>> data_;
std::shared_ptr<MementoSharedTypePtr<Config>> config_;
std::shared_ptr<MementoSharedTypePtr<Status>> status_;
std::shared_ptr<MementoSharedTypePtr<State>> state_;
Templates::Caretakers caretaker_;
};
/**
* @brief The MementoContextDataShared class
*/
template <typename T>
class MementoContextDataShared
: public UndoRedoBase
, public InitializeMethods
{
public:
MementoContextDataShared()
: value_(new MementoSharedTypePtr<T>(new T()))
{
Initialize();
}
MementoContextDataShared(const T *value)
: value_(new MementoSharedTypePtr<T>(value))
{
Initialize();
}
MementoContextDataShared(const std::shared_ptr<T> value)
: value_(new MementoSharedTypePtr<T>(value))
{
Initialize();
}
virtual ~MementoContextDataShared()
{ }
MementoSharedTypePtr<T>& context()
{
return value_.operator *();
}
const MementoSharedTypePtr<T>& context() const
{
return value_.operator *();
}
// -------------------------------------
// UndoRedoBase interface
// -------------------------------------
virtual bool CreateMemento() const
{
caretaker_.CreateMemento();
}
virtual bool Undo()
{
caretaker_.Undo();
}
virtual bool Redo()
{
caretaker_.Redo();
}
virtual void ClearUndo()
{
caretaker_.ClearUndo();
}
virtual void ClearRedo()
{
caretaker_.ClearRedo();
}
// -------------------------------------
// InitializeMethods
// -------------------------------------
virtual bool Initalize()
{
caretaker_.push_back(value_);
return true;
}
virtual bool IsInitialized() const
{
return !caretaker_.empty();
}
private:
std::shared_ptr<MementoSharedTypePtr<T>> value_;
Templates::Caretakers caretaker_;
};
}}