forked from RetroAchievements/RAIntegration
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotifyTargetSet.hh
More file actions
272 lines (232 loc) · 7.77 KB
/
Copy pathNotifyTargetSet.hh
File metadata and controls
272 lines (232 loc) · 7.77 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
#ifndef RA_DATA_NOTIFYTARGET_SET_H
#define RA_DATA_NOTIFYTARGET_SET_H
#pragma once
#include "util\GSL.hh"
namespace ra {
namespace data {
/// <summary>
/// A collection of pointers to other objects.
/// </summary>
/// <remarks>
/// These are not allocated objects and do not need to be free'd.
/// This behaves like a set of references, which isn't allowed.
/// </summary>
template<class TNotifyTarget>
class NotifyTargetSet
{
private:
using TargetList = std::vector<TNotifyTarget*>;
public:
class ValidTargets
{
public:
ValidTargets(const TargetList& pTargets) noexcept
: m_pBegin(pTargets.cbegin()), m_pEnd(pTargets.cend())
{
}
~ValidTargets() noexcept = default;
ValidTargets(const ValidTargets&) noexcept = default;
ValidTargets& operator=(const ValidTargets&) noexcept = default;
ValidTargets(ValidTargets&&) noexcept = default;
ValidTargets& operator=(ValidTargets&&) noexcept = default;
class const_iterator : public std::iterator_traits<typename TargetList::const_iterator>
{
public:
TNotifyTarget& operator*() noexcept { return *(*m_pCurrent); }
const_iterator& operator++() noexcept
{
++m_pCurrent;
return *this;
}
const_iterator operator++(int)
{
auto pBeforeIncrement = *this;
m_pCurrent++;
return pBeforeIncrement;
}
bool operator==(const const_iterator& that) const noexcept
{
return m_pCurrent == that.m_pCurrent;
}
bool operator!=(const const_iterator& that) const noexcept
{
return !(*this == that);
}
private:
friend class ValidTargets;
const_iterator(typename TargetList::const_iterator pCurrent) noexcept
: m_pCurrent(std::move(pCurrent))
{
}
typename TargetList::const_iterator m_pCurrent;
};
auto begin() const noexcept { return m_pBegin; }
auto end() const noexcept { return m_pEnd; }
size_t size() const noexcept
{
return m_pEnd.m_pCurrent - m_pBegin.m_pCurrent;
}
private:
const_iterator m_pBegin;
const_iterator m_pEnd;
};
/// <summary>
/// Gets the objects in the collection.
/// </summary>
/// <remarks>
/// Should be called within a <see cref="Lock"> block to ensure the collection
/// isn't modified while it's being processed.
/// </remarks>
const ValidTargets Targets() const noexcept
{
if (!m_pTargetList)
{
GSL_SUPPRESS_F6 m_pTargetList = std::make_unique<TargetListNode>();
}
return ValidTargets(m_pTargetList->vTargetList);
}
/// <summary>
/// Adds an object reference to the collection.
/// </summary>
GSL_SUPPRESS_F6 // this should only throw an exception if we're out of memory
void Add(TNotifyTarget& pTarget) noexcept
{
if (!m_pTargetList)
{
m_pTargetList = std::make_unique<TargetListNode>();
}
else
{
const auto& vNotifyTargets = m_pTargetList->vTargetList;
auto pIter = std::find(vNotifyTargets.begin(), vNotifyTargets.end(), &pTarget);
if (pIter != vNotifyTargets.end())
return;
EnsureMutable();
}
m_pTargetList->vTargetList.push_back(&pTarget);
}
/// <summary>
/// Removes an object reference from the collection.
/// </summary>
GSL_SUPPRESS_F6 // processing collection of raw pointers should never throw an exception
void Remove(TNotifyTarget& pTarget) noexcept
{
if (!m_pTargetList)
return;
gsl::not_null<TargetList*> vNotifyTargets = gsl::make_not_null(&m_pTargetList->vTargetList);
auto pIter = std::find(vNotifyTargets->begin(), vNotifyTargets->end(), &pTarget);
if (pIter == vNotifyTargets->end())
return;
EnsureMutable();
if (vNotifyTargets != &m_pTargetList->vTargetList)
{
// m_pTargetList changed. find the element in the new list.
vNotifyTargets = gsl::make_not_null(&m_pTargetList->vTargetList);
pIter = std::find(vNotifyTargets->begin(), vNotifyTargets->end(), &pTarget);
}
if (pIter != vNotifyTargets->end())
vNotifyTargets->erase(pIter);
}
/// <summary>
/// Removes all objects from the collection.
/// </summary>
void Clear() noexcept
{
if (m_pTargetList)
{
EnsureMutable();
m_pTargetList->vTargetList.clear();
}
}
/// <summary>
/// Gets whether the collection contains no items.
/// </summary>
bool IsEmpty() const noexcept
{
return !m_pTargetList || m_pTargetList->vTargetList.empty();
}
/// <summary>
/// Locks the collection while it's being processed.
/// </summary>
/// <returns>
/// <c>true</c> if the collection was locked,
/// <c>false</c> if the collection is empty and was not locked.
/// </returns>
/// <remarks>
/// Changes made to the collection while locked won't appear until the collection is unlocked.
/// </remarks>
bool LockIfNotEmpty() noexcept
{
if (IsEmpty())
return false;
++m_pTargetList->nLockCount;
return true;
}
/// <summary>
/// Locks the collection while it's being processed.
/// </summary>
/// <remarks>
/// Changes made to the collection while locked won't appear until the collection is unlocked.
/// </remarks>
void Lock() noexcept
{
if (!m_pTargetList)
{
GSL_SUPPRESS_F6 // this should only throw an exception if we're out of memory
m_pTargetList = std::make_unique<TargetListNode>();
}
++m_pTargetList->nLockCount;
}
/// <summary>
/// Unlocks the collection and applies any pending changes.
/// </summary>
void Unlock() noexcept
{
if (!m_pTargetList)
return;
if (m_pTargetList->nLockCount > 0)
{
// list not modified. release lock
--m_pTargetList->nLockCount;
}
else if (m_pTargetList->pNext)
{
// list modified. release lock on copy of list (in m_pTargetList->pNext).
// if there are no more locks on the copy, discard it.
if (--m_pTargetList->pNext->nLockCount == 0)
m_pTargetList->pNext = std::move(m_pTargetList->pNext->pNext);
}
}
private:
struct TargetListNode
{
public:
TargetListNode() noexcept {}
TargetListNode(const TargetList& vTargetList)
: vTargetList(vTargetList)
{
}
~TargetListNode() noexcept = default;
TargetListNode(const TargetListNode&) noexcept = delete;
TargetListNode& operator=(const TargetListNode&) noexcept = delete;
TargetListNode(TargetListNode&&) noexcept = default;
TargetListNode& operator=(TargetListNode&&) noexcept = default;
TargetList vTargetList;
std::unique_ptr<TargetListNode> pNext;
int nLockCount = 0;
};
mutable std::unique_ptr<TargetListNode> m_pTargetList;
void EnsureMutable() noexcept
{
if (m_pTargetList->nLockCount)
{
// current list is locked, clone it so it can be mutated
GSL_SUPPRESS_F6 auto pTargetList = std::make_unique<TargetListNode>(m_pTargetList->vTargetList);
pTargetList->pNext = std::move(m_pTargetList);
m_pTargetList = std::move(pTargetList);
}
}
};
} // namespace data
} // namespace ra
#endif RA_DATA_NOTIFYTARGET_SET_H