forked from RetroAchievements/RAIntegration
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataModelBase.hh
More file actions
319 lines (270 loc) · 11.3 KB
/
Copy pathDataModelBase.hh
File metadata and controls
319 lines (270 loc) · 11.3 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#ifndef RA_DATA_DATAMODEL_BASE_H
#define RA_DATA_DATAMODEL_BASE_H
#pragma once
#include "ModelBase.hh"
#include "NotifyTargetSet.hh"
#include <set>
namespace ra {
namespace data {
class DataModelBase : public ModelBase
{
public:
class NotifyTarget
{
public:
NotifyTarget() noexcept = default;
virtual ~NotifyTarget() noexcept = default;
NotifyTarget(const NotifyTarget&) noexcept = delete;
NotifyTarget& operator=(const NotifyTarget&) noexcept = delete;
NotifyTarget(NotifyTarget&&) noexcept = default;
NotifyTarget& operator=(NotifyTarget&&) noexcept = default;
virtual void OnDataModelBoolValueChanged([[maybe_unused]] const BoolModelProperty::ChangeArgs& args) noexcept(false) {}
virtual void OnDataModelStringValueChanged([[maybe_unused]] const StringModelProperty::ChangeArgs& args) noexcept(false) {}
virtual void OnDataModelIntValueChanged([[maybe_unused]] const IntModelProperty::ChangeArgs& args) noexcept(false) {}
};
void AddNotifyTarget(NotifyTarget& pTarget) noexcept { m_vNotifyTargets.Add(pTarget); }
void RemoveNotifyTarget(NotifyTarget& pTarget) noexcept
{
#ifdef DEBUG
GSL_SUPPRESS_F6 Expects(!m_bDestructed);
#endif
m_vNotifyTargets.Remove(pTarget);
}
private:
NotifyTargetSet<NotifyTarget> m_vNotifyTargets;
public:
/// <summary>
/// Pushes a new transaction onto the stack.
/// </summary>
virtual void BeginTransaction();
/// <summary>
/// Completes the current transaction, keeping any changes.
/// </summary>
virtual void CommitTransaction();
/// <summary>
/// Completes the current transaction, resetting all tracked properties to the state
/// they were in when <see cref="BeginTransaction" /> was called.
/// </summary>
virtual void RevertTransaction();
/// <summary>
/// The <see cref="ModelProperty" /> for the whether the viewmodel has been modified.
/// </summary>
static const BoolModelProperty IsModifiedProperty;
/// <summary>
/// Determines if any tracked property has been modified in the current transaction.
/// </summary>
/// <returns><c>true</c> if modified, <c>false</c> if not.</returns>
bool IsModified() const
{
return GetValue(IsModifiedProperty);
}
/// <summary>
/// Determines if the specified property has been modified in the current transaction.
/// </summary>
/// <param name="pProperty">The property to check.</param>
/// <returns><c>true</c> if modified, <c>false</c> if not.</returns>
bool IsModified(const BoolModelProperty& pProperty) const
{
return m_pTransaction != nullptr && m_pTransaction->IsModified(pProperty);
}
/// <summary>
/// Determines if the specified property has been modified in the current transaction.
/// </summary>
/// <param name="pProperty">The property to check.</param>
/// <returns><c>true</c> if modified, <c>false</c> if not.</returns>
bool IsModified(const StringModelProperty& pProperty) const
{
return m_pTransaction != nullptr && m_pTransaction->IsModified(pProperty);
}
/// <summary>
/// Determines if the specified property has been modified in the current transaction.
/// </summary>
/// <param name="pProperty">The property to check.</param>
/// <returns><c>true</c> if modified, <c>false</c> if not.</returns>
bool IsModified(const IntModelProperty& pProperty) const
{
return m_pTransaction != nullptr && m_pTransaction->IsModified(pProperty);
}
protected:
GSL_SUPPRESS_F6 DataModelBase() = default;
/// <summary>
/// Indicates the specified property should be monitored in transactions.
/// </summary>
/// <param name="pProperty">The property to monitor.</param>
void SetTransactional(const BoolModelProperty& pProperty) noexcept
{
GSL_SUPPRESS_F6 m_vTransactionalProperties.insert(pProperty.GetKey());
}
/// <summary>
/// Indicates the specified property should be monitored in transactions.
/// </summary>
/// <param name="pProperty">The property to monitor.</param>
void SetTransactional(const StringModelProperty& pProperty) noexcept
{
GSL_SUPPRESS_F6 m_vTransactionalProperties.insert(pProperty.GetKey());
}
/// <summary>
/// Indicates the specified property should be monitored in transactions.
/// </summary>
/// <param name="pProperty">The property to monitor.</param>
void SetTransactional(const IntModelProperty& pProperty) noexcept
{
GSL_SUPPRESS_F6 m_vTransactionalProperties.insert(pProperty.GetKey());
}
class Transaction
{
public:
/// <summary>
/// Gets the value of the property prior to any changes made in the current transaction
/// </summary>
const bool* GetPreviousValue(const BoolModelProperty& pProperty) const
{
const ModelPropertyValue* pValue = FindValue(pProperty.GetKey());
GSL_SUPPRESS_TYPE1
return pValue ? reinterpret_cast<const bool*>(&pValue->nValue) : nullptr;
}
/// <summary>
/// Gets the previous value for the property if it has been modified, or the default value if
/// if has not been modified. Use <see cref="IsModified"/> to determined if if was modified.
/// </summary>
const std::wstring* GetPreviousValue(const StringModelProperty& pProperty) const
{
const ModelPropertyValue* pValue = FindValue(pProperty.GetKey());
return pValue ? &pValue->sValue : nullptr;
}
/// <summary>
/// Gets the previous value for the property if it has been modified, or the default value if
/// if has not been modified. Use <see cref="IsModified"/> to determined if if was modified.
/// </summary>
const int* GetPreviousValue(const IntModelProperty& pProperty) const
{
const ModelPropertyValue* pValue = FindValue(pProperty.GetKey());
return pValue ? &pValue->nValue : nullptr;
}
/// <summary>
/// Updates the transaction with the new value for a property.
/// </summary>
/// <param name="args">Information about the change.</param>
void ValueChanged(const BoolModelProperty::ChangeArgs& args);
/// <summary>
/// Updates the transaction with the new value for a property.
/// </summary>
/// <param name="args">Information about the change.</param>
void ValueChanged(const StringModelProperty::ChangeArgs& args);
/// <summary>
/// Updates the transaction with the new value for a property.
/// </summary>
/// <param name="args">Information about the change.</param>
void ValueChanged(const IntModelProperty::ChangeArgs& args);
/// <summary>
/// Determines if any property has been modified.
/// </summary>
bool IsModified() const noexcept
{
return !m_vOriginalValues.empty();
}
/// <summary>
/// Determines if the specified property has been modified.
/// </summary>
/// <param name="pProperty">The property to check.</param>
/// <returns><c>true</c> if modified, <c>false</c> if not.</returns>
bool IsModified(const BoolModelProperty& pProperty) const
{
return FindValue(pProperty.GetKey()) != nullptr;
}
/// <summary>
/// Determines if the specified property has been modified.
/// </summary>
/// <param name="pProperty">The property to check.</param>
/// <returns><c>true</c> if modified, <c>false</c> if not.</returns>
bool IsModified(const StringModelProperty& pProperty) const
{
return FindValue(pProperty.GetKey()) != nullptr;
}
/// <summary>
/// Determines if the specified property has been modified.
/// </summary>
/// <param name="pProperty">The property to check.</param>
/// <returns><c>true</c> if modified, <c>false</c> if not.</returns>
bool IsModified(const IntModelProperty& pProperty) const
{
return FindValue(pProperty.GetKey()) != nullptr;
}
void Revert(DataModelBase& vmViewModel);
void Commit(const DataModelBase& vmViewModel);
std::unique_ptr<Transaction> m_pNext;
// allow DataModelCollectionBase to call GetValue(Property) directly.
friend class DataModelCollectionBase;
private:
typedef struct ModelPropertyValue
{
#ifdef _DEBUG
const char* pPropertyName;
#endif
int nKey = 0;
int nValue = 0;
std::wstring sValue;
} ModelPropertyValue;
std::vector<ModelPropertyValue> m_vOriginalValues;
static int CompareModelPropertyKey(const ModelPropertyValue& left, int nKey) noexcept;
const ModelPropertyValue* FindValue(int nKey) const;
};
std::unique_ptr<Transaction> m_pTransaction;
void OnValueChanged(const BoolModelProperty::ChangeArgs& args) override;
void OnValueChanged(const StringModelProperty::ChangeArgs& args) override;
void OnValueChanged(const IntModelProperty::ChangeArgs& args) override;
/// <summary>
/// Disables change notifications.
/// </summary>
/// <remarks>
/// Should not be called unless making multiple changes to the same property.
/// OnValueChanged will be called once after EndUpdate is called instead of for each intermediate change.
/// </remarks>
void BeginUpdate();
/// <summary>
/// Re-enables change notifications.
/// </summary>
void EndUpdate();
/// <summary>
/// Determines if change notifications are disabled.
/// </summary>
/// <remarks>
/// Can be used in OnValueChanged overrides to determine if change notifications are disabled. If they are, OnValueChanged
/// will be called again once the notifications are re-enabled, so processing should be delayed if this returns true.
/// </remarks>
virtual bool IsUpdating() const noexcept(false) { return (m_pUpdateTransaction != nullptr); }
private:
void DiscardTransaction();
bool IsTransactional(const BoolModelProperty& pProperty)
{
return (m_vTransactionalProperties.find(pProperty.GetKey()) != m_vTransactionalProperties.end());
}
bool IsTransactional(const StringModelProperty& pProperty)
{
return (m_vTransactionalProperties.find(pProperty.GetKey()) != m_vTransactionalProperties.end());
}
bool IsTransactional(const IntModelProperty& pProperty)
{
return (m_vTransactionalProperties.find(pProperty.GetKey()) != m_vTransactionalProperties.end());
}
std::set<int> m_vTransactionalProperties;
struct UpdateTransaction
{
unsigned int m_nUpdateCount = 0;
template<class T>
struct DelayedChange
{
const ModelProperty<T>* pProperty;
T tOldValue;
T tNewValue;
};
std::vector<DelayedChange<int>> m_vDelayedIntChanges;
std::vector<DelayedChange<std::wstring>> m_vDelayedStringChanges;
std::vector<DelayedChange<bool>> m_vDelayedBoolChanges;
};
std::unique_ptr<UpdateTransaction> m_pUpdateTransaction;
const void* m_pEndUpdateChangeArgs = nullptr;
};
} // namespace data
} // namespace ra
#endif RA_DATA_DATAMODEL_BASE_H