forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntrusiveList.h
462 lines (397 loc) · 16.6 KB
/
IntrusiveList.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <iterator>
#include <utility>
#include <lib/support/CodeUtils.h>
namespace chip {
class IntrusiveListBase;
enum class IntrusiveMode
{
// Strict mode features
//
// Node constructor puts the hook in a well-known default state.
//
// Node destructor checks if the hook is in the well-known default state.
// If not, an assertion is raised.
//
// Every time an object is inserted in the intrusive container, the
// container checks if the hook is in the well-known default state. If
// not, an assertion is raised.
//
// Every time an object is being erased from the intrusive container, the
// container puts the erased object in the well-known default state.
//
// Note: The strict mode works similar as safe mode for Boost.Intrusive
//
Strict,
// Auto-unlink mode features
//
// When the destructor of the node is called, the hook checks if the node
// is inserted in a container. If so, the hook removes the node from the
// container.
//
// The hook has a member function called unlink() that can be used to
// unlink the node from the container at any time, without having any
// reference to the container, if the user wants to do so.
//
// This auto-unlink feature is useful in certain applications but it must
// be used very carefully:
//
// If several threads are using the same container the destructor of the
// auto-unlink hook will be called without any thread synchronization so
// removing the object is thread-unsafe. Container contents change
// silently without modifying the container directly. This can lead to
// surprising effects.
//
// These auto-unlink hooks have also strict-mode properties:
//
// Node constructors put the hook in a well-known default state.
//
// Every time an object is inserted in the intrusive container, the
// container checks if the hook is in the well-known default state. If
// not, an assertion is raised.
//
// Every time an object is erased from an intrusive container, the
// container puts the erased object in the well-known default state.
//
// Note: The strict mode works similar as auto-unlink mode for
// Boost.Intrusive
//
AutoUnlink,
};
class IntrusiveListNodePrivateBase
{
public:
IntrusiveListNodePrivateBase() : mPrev(nullptr), mNext(nullptr) {}
~IntrusiveListNodePrivateBase() { VerifyOrDie(!IsInList()); }
// Note: The copy construct/assignment is not provided because the list node state is not copyable.
// The move construct/assignment is not provided because all modifications to the list shall go through the list object.
IntrusiveListNodePrivateBase(const IntrusiveListNodePrivateBase &) = delete;
IntrusiveListNodePrivateBase & operator=(const IntrusiveListNodePrivateBase &) = delete;
IntrusiveListNodePrivateBase(IntrusiveListNodePrivateBase &&) = delete;
IntrusiveListNodePrivateBase & operator=(IntrusiveListNodePrivateBase &&) = delete;
bool IsInList() const { return (mPrev != nullptr && mNext != nullptr); }
private:
friend class IntrusiveListBase;
IntrusiveListNodePrivateBase(IntrusiveListNodePrivateBase * prev, IntrusiveListNodePrivateBase * next) :
mPrev(prev), mNext(next)
{}
void TakePlaceOf(const IntrusiveListNodePrivateBase * that)
{
// prerequisite `that` is in a list
// `this` will take place of the position of `that`.
// `that` will be emptied by the caller after this function
mPrev = that->mPrev;
mNext = that->mNext;
mPrev->mNext = this;
mNext->mPrev = this;
}
void Prepend(IntrusiveListNodePrivateBase * node)
{
VerifyOrDie(IsInList());
VerifyOrDie(!node->IsInList());
node->mPrev = mPrev;
node->mNext = this;
mPrev->mNext = node;
mPrev = node;
}
void Append(IntrusiveListNodePrivateBase * node)
{
VerifyOrDie(IsInList());
VerifyOrDie(!node->IsInList());
node->mPrev = this;
node->mNext = mNext;
mNext->mPrev = node;
mNext = node;
}
protected:
void Remove()
{
VerifyOrDie(IsInList());
mPrev->mNext = mNext;
mNext->mPrev = mPrev;
mPrev = nullptr;
mNext = nullptr;
}
private:
IntrusiveListNodePrivateBase * mPrev;
IntrusiveListNodePrivateBase * mNext;
};
// Strict mode implementation
template <IntrusiveMode Mode = IntrusiveMode::Strict>
class IntrusiveListNodeBase : public IntrusiveListNodePrivateBase
{
};
// Partial specialization implementation for auto-unlink mode.
template <>
class IntrusiveListNodeBase<IntrusiveMode::AutoUnlink> : public IntrusiveListNodePrivateBase
{
public:
~IntrusiveListNodeBase() { Unlink(); }
void Unlink()
{
if (IsInList())
Remove();
}
};
// Non-template part of IntrusiveList. It appears that for list structure, both mode can share same implementation.
class IntrusiveListBase
{
public:
class ConstIteratorBase
{
private:
friend class IntrusiveListBase;
ConstIteratorBase(const IntrusiveListNodePrivateBase * cur) : mCurrent(cur) {}
const IntrusiveListNodePrivateBase & operator*() { return *mCurrent; }
public:
using difference_type = std::ptrdiff_t;
using iterator_category = std::bidirectional_iterator_tag;
ConstIteratorBase(const ConstIteratorBase &) = default;
ConstIteratorBase(ConstIteratorBase &&) = default;
ConstIteratorBase & operator=(const ConstIteratorBase &) = default;
ConstIteratorBase & operator=(ConstIteratorBase &&) = default;
bool operator==(const ConstIteratorBase & that) const { return mCurrent == that.mCurrent; }
bool operator!=(const ConstIteratorBase & that) const { return !(*this == that); }
ConstIteratorBase & operator++()
{
mCurrent = mCurrent->mNext;
return *this;
}
ConstIteratorBase operator++(int)
{
ConstIteratorBase res(mCurrent);
mCurrent = mCurrent->mNext;
return res;
}
ConstIteratorBase & operator--()
{
mCurrent = mCurrent->mPrev;
return *this;
}
ConstIteratorBase operator--(int)
{
ConstIteratorBase res(mCurrent);
mCurrent = mCurrent->mPrev;
return res;
}
protected:
const IntrusiveListNodePrivateBase * mCurrent;
};
class IteratorBase
{
private:
friend class IntrusiveListBase;
IteratorBase(IntrusiveListNodePrivateBase * cur) : mCurrent(cur) {}
IntrusiveListNodePrivateBase & operator*() { return *mCurrent; }
public:
using difference_type = std::ptrdiff_t;
using iterator_category = std::bidirectional_iterator_tag;
IteratorBase(const IteratorBase &) = default;
IteratorBase(IteratorBase &&) = default;
IteratorBase & operator=(const IteratorBase &) = default;
IteratorBase & operator=(IteratorBase &&) = default;
bool operator==(const IteratorBase & that) const { return mCurrent == that.mCurrent; }
bool operator!=(const IteratorBase & that) const { return !(*this == that); }
IteratorBase & operator++()
{
mCurrent = mCurrent->mNext;
return *this;
}
IteratorBase operator++(int)
{
IteratorBase res(mCurrent);
mCurrent = mCurrent->mNext;
return res;
}
IteratorBase & operator--()
{
mCurrent = mCurrent->mPrev;
return *this;
}
IteratorBase operator--(int)
{
IteratorBase res(mCurrent);
mCurrent = mCurrent->mPrev;
return res;
}
protected:
IntrusiveListNodePrivateBase * mCurrent;
};
bool Empty() const { return mNode.mNext == &mNode; }
void Erase(IteratorBase pos) { pos.mCurrent->Remove(); }
protected:
// The list is formed as a ring with mNode being the end.
//
// begin end
// v v
// item(first) -> item -> ... -> item(last) -> mNode
// ^ |
// \------------------------------------------/
//
IntrusiveListBase() : mNode(&mNode, &mNode) {}
~IntrusiveListBase()
{
VerifyOrDie(Empty());
/* clear mNode such that the destructor checking mNode.IsInList doesn't fail */
mNode.Remove();
}
IntrusiveListBase(const IntrusiveListBase &) = delete;
IntrusiveListBase & operator=(const IntrusiveListBase &) = delete;
IntrusiveListBase(IntrusiveListBase && that) : mNode(&mNode, &mNode) { *this = std::move(that); }
IntrusiveListBase & operator=(IntrusiveListBase && that)
{
VerifyOrDie(Empty());
if (!that.Empty())
{
mNode.TakePlaceOf(&that.mNode);
that.mNode.mNext = &that.mNode;
that.mNode.mPrev = &that.mNode;
}
else
{
// Do nothing here if that is empty, because there is a prerequisite that `this` is empty.
}
return *this;
}
ConstIteratorBase begin() const { return ConstIteratorBase(mNode.mNext); }
ConstIteratorBase end() const { return ConstIteratorBase(&mNode); }
IteratorBase begin() { return IteratorBase(mNode.mNext); }
IteratorBase end() { return IteratorBase(&mNode); }
void PushFront(IntrusiveListNodePrivateBase * node) { mNode.Append(node); }
void PushBack(IntrusiveListNodePrivateBase * node) { mNode.Prepend(node); }
void InsertBefore(IteratorBase pos, IntrusiveListNodePrivateBase * node) { pos.mCurrent->Prepend(node); }
void InsertAfter(IteratorBase pos, IntrusiveListNodePrivateBase * node) { pos.mCurrent->Append(node); }
void Remove(IntrusiveListNodePrivateBase * node) { node->Remove(); }
/// @brief Replace an original node in list with a new node.
void Replace(IntrusiveListNodePrivateBase * original, IntrusiveListNodePrivateBase * replacement)
{
// VerifyOrDie(Contains(original)); This check is too heavy to do, but it shall hold
VerifyOrDie(!replacement->IsInList());
replacement->TakePlaceOf(original);
original->mPrev = nullptr;
original->mNext = nullptr;
}
bool Contains(const IntrusiveListNodePrivateBase * node) const
{
for (auto & iter : *this)
{
if (&iter == node)
return true;
}
return false;
}
private:
IntrusiveListNodePrivateBase mNode;
};
/// The hook converts between node object T and IntrusiveListNodeBase
///
/// When using this hook, the node type (T) MUST inherit from IntrusiveListNodeBase.
///
template <typename T, IntrusiveMode Mode>
class IntrusiveListBaseHook
{
public:
static_assert(std::is_base_of<IntrusiveListNodeBase<Mode>, T>::value, "T must be derived from IntrusiveListNodeBase");
static T * ToObject(IntrusiveListNodePrivateBase * node) { return static_cast<T *>(node); }
static const T * ToObject(const IntrusiveListNodePrivateBase * node) { return static_cast<T *>(node); }
static T * ToObject(IntrusiveListNodeBase<Mode> * node) { return static_cast<T *>(node); }
static const T * ToObject(const IntrusiveListNodeBase<Mode> * node) { return static_cast<T *>(node); }
static IntrusiveListNodeBase<Mode> * ToNode(T * object) { return static_cast<IntrusiveListNodeBase<Mode> *>(object); }
static const IntrusiveListNodeBase<Mode> * ToNode(const T * object)
{
return static_cast<const IntrusiveListNodeBase<Mode> *>(object);
}
};
/// A double-linked list where the data is stored together with the previous/next pointers for cache efficiency / and compactness.
///
/// The default hook (IntrusiveListBaseHook<T>) requires T to inherit from IntrusiveListNodeBase.
///
/// Consumers must ensure that the IntrusiveListNodeBase object associated with
/// a node is removed from any list it might belong to before it is destroyed.
///
/// Consumers must ensure that a list is empty before it is destroyed.
///
/// A node may only belong to a single list. The code will assert (via VerifyOrDie) on this invariant.
///
/// Example usage:
///
/// class ListNode : public IntrusiveListNodeBase {};
/// // ...
/// ListNode a,b,c;
/// IntrusiveList<ListNode> list; // NOTE: node lifetime >= list lifetime
///
/// list.PushBack(&a);
/// list.PushFront(&b);
/// assert(list.Contains(&a) && list.Contains(&b) && !list.Contains(&c));
/// list.Remove(&a);
/// list.Remove(&b);
template <typename T, IntrusiveMode Mode = IntrusiveMode::Strict, typename Hook = IntrusiveListBaseHook<T, Mode>>
class IntrusiveList : public IntrusiveListBase
{
public:
IntrusiveList() : IntrusiveListBase() {}
IntrusiveList(IntrusiveList &&) = default;
IntrusiveList & operator=(IntrusiveList &&) = default;
class ConstIterator : public IntrusiveListBase::ConstIteratorBase
{
public:
using value_type = const T;
using pointer = const T *;
using reference = const T &;
ConstIterator(IntrusiveListBase::ConstIteratorBase && base) : IntrusiveListBase::ConstIteratorBase(std::move(base)) {}
const T * operator->() { return Hook::ToObject(mCurrent); }
const T & operator*() { return *Hook::ToObject(mCurrent); }
ConstIterator & operator++() { return static_cast<ConstIterator &>(IntrusiveListBase::ConstIteratorBase::operator++()); }
ConstIterator operator++(int) { return IntrusiveListBase::ConstIteratorBase::operator++(1); }
ConstIterator & operator--() { return static_cast<ConstIterator &>(IntrusiveListBase::ConstIteratorBase::operator--()); }
ConstIterator operator--(int) { return IntrusiveListBase::ConstIteratorBase::operator--(1); }
};
class Iterator : public IntrusiveListBase::IteratorBase
{
public:
using value_type = T;
using pointer = T *;
using reference = T &;
Iterator(IntrusiveListBase::IteratorBase && base) : IntrusiveListBase::IteratorBase(std::move(base)) {}
T * operator->() { return Hook::ToObject(mCurrent); }
T & operator*() { return *Hook::ToObject(mCurrent); }
Iterator & operator++() { return static_cast<Iterator &>(IntrusiveListBase::IteratorBase::operator++()); }
Iterator operator++(int) { return IntrusiveListBase::IteratorBase::operator++(1); }
Iterator & operator--() { return static_cast<Iterator &>(IntrusiveListBase::IteratorBase::operator--()); }
Iterator operator--(int) { return IntrusiveListBase::IteratorBase::operator--(1); }
};
ConstIterator begin() const { return IntrusiveListBase::begin(); }
ConstIterator end() const { return IntrusiveListBase::end(); }
Iterator begin() { return IntrusiveListBase::begin(); }
Iterator end() { return IntrusiveListBase::end(); }
void PushFront(T * value) { IntrusiveListBase::PushFront(Hook::ToNode(value)); }
void PushBack(T * value) { IntrusiveListBase::PushBack(Hook::ToNode(value)); }
void InsertBefore(Iterator pos, T * value) { IntrusiveListBase::InsertBefore(pos, Hook::ToNode(value)); }
void InsertAfter(Iterator pos, T * value) { IntrusiveListBase::InsertAfter(pos, Hook::ToNode(value)); }
void Remove(T * value) { IntrusiveListBase::Remove(Hook::ToNode(value)); }
void Replace(T * original, T * replacement) { IntrusiveListBase::Replace(Hook::ToNode(original), Hook::ToNode(replacement)); }
bool Contains(const T * value) const { return IntrusiveListBase::Contains(Hook::ToNode(value)); }
void Clear()
{
while (begin() != end())
{
Remove(&(*begin()));
}
}
};
} // namespace chip