-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver_ptr.hpp
333 lines (281 loc) · 7.65 KB
/
observer_ptr.hpp
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
/*
* Copyright (C) 2017 Giel van Schijndel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
// NOTE: While similar in intent to N3840: this is _not_ that observer_ptr because this, unlike that
// proposal, takes care of clearing pointers preventing dangling observer_ptr instances.
#ifndef INCLUDED_OBSERVER_PTR_HPP
#define INCLUDED_OBSERVER_PTR_HPP
#include <memory>
#include <mutex>
#include <type_traits>
#include <unordered_set>
namespace util {
// Implementation details, not part of public API
namespace detail {
class observable_deleter_base;
struct observer_data
{
void* p = nullptr;
observable_deleter_base* d = nullptr;
};
class observable_deleter_base
{
public:
observable_deleter_base() = default;
observable_deleter_base(observable_deleter_base&& rhs) noexcept : observers([&rhs] {
std::lock_guard<decltype(rhs.mutex)> lock{rhs.mutex};
return decltype(rhs.observers){std::move(rhs.observers)};
}())
{
}
observable_deleter_base& operator=(observable_deleter_base rhs) noexcept
{
std::lock_guard<decltype(mutex)> lock{mutex};
assert(observers.empty());
observers = std::move(rhs.observers);
return *this;
}
~observable_deleter_base()
{
assert(observers.empty());
}
void wipe() noexcept
{
std::lock_guard<decltype(mutex)> lock{mutex};
for (auto& observer : observers)
{
assert(observer->d == this);
observer->p = nullptr;
observer->d = nullptr;
}
observers.clear();
}
void push(observer_data& o) noexcept
{
const auto observer_inserted = [&] {
std::lock_guard<decltype(mutex)> lock{mutex};
return observers.insert(&o).second;
}();
assert(observer_inserted);
if (observer_inserted)
{
o.d = this;
}
}
void pop(observer_data& o) noexcept
{
const auto count_erased = [&] {
std::lock_guard<decltype(mutex)> lock{mutex};
return observers.erase(&o);
}();
assert(count_erased > 0);
if (count_erased > 0)
{
o.p = nullptr;
o.d = nullptr;
}
}
private:
// Yes this might be possible to do more efficiently with atomics, but will also be _much_ more
// error prone.
std::mutex mutex;
std::unordered_set<observer_data*> observers;
};
}
template <typename T>
class observer_ptr : private detail::observer_data
{
public:
class observable_deleter final : public detail::observable_deleter_base
{
public:
void operator()(T* p) noexcept(noexcept(delete p))
{
static_assert(!std::is_void<T>::value, "can't delete pointer to incomplete type");
static_assert(sizeof(T) > 0, "can't delete pointer to incomplete type");
wipe();
delete p;
}
};
constexpr observer_ptr() noexcept = default;
constexpr observer_ptr(std::nullptr_t) noexcept
{
}
/// Convert from compatible unique_ptr
template <typename U,
typename = typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
observer_ptr(std::unique_ptr<U, typename observer_ptr<U>::observable_deleter>& rhs) noexcept
{
if (rhs)
{
this->p = static_cast<T*>(rhs.get());
rhs.get_deleter().push(*this);
}
}
observer_ptr(const observer_ptr& rhs) noexcept
{
if (rhs)
{
this->p = static_cast<T*>(rhs.get());
rhs.d->push(*this);
}
}
template <typename U,
typename = typename std::enable_if<std::is_convertible<U*, T*>::value
&& !std::is_same<U, T>::value>::type>
observer_ptr(const observer_ptr<U>& rhs) noexcept
{
if (rhs)
{
this->p = static_cast<T*>(rhs.get());
rhs.d->push(*this);
}
}
template <typename U>
observer_ptr(const observer_ptr<U>& rhs, T* const rp) noexcept
{
assert(rp != nullptr);
assert(rhs.d != nullptr);
this->p = rp;
rhs.d->push(*this);
}
observer_ptr& operator=(const observer_ptr& rhs) noexcept
{
if (this->p == rhs.p)
return *this;
reset();
if (rhs)
{
this->p = static_cast<T*>(rhs.get());
rhs.d->push(*this);
}
return *this;
}
~observer_ptr() noexcept
{
reset();
}
void reset()
{
if (d)
{
d->pop(*this);
}
}
constexpr T* get() const noexcept
{
return static_cast<T*>(this->p);
}
constexpr explicit operator bool() const
{
return this->p != nullptr;
}
constexpr T& operator*() const noexcept
{
assert(this->p != nullptr);
return *get();
}
constexpr T* operator->() const noexcept
{
assert(this->p != nullptr);
return get();
}
private:
template <typename>
friend class observer_ptr;
};
template <typename T>
using observable_ptr = std::unique_ptr<T, typename observer_ptr<T>::observable_deleter>;
template <typename T, typename... Args>
observable_ptr<T> make_observable(Args&&... args)
{
return observable_ptr<T>{new T(std::forward<Args>(args)...)};
}
template <typename T, typename TS>
observer_ptr<T> dynamic_pointer_cast(const observer_ptr<TS>& r) noexcept
{
if (auto* p = dynamic_cast<T*>(r.get()))
return observer_ptr<T>{r, p};
return {};
}
template <typename T1, typename T2>
constexpr bool operator==(const observer_ptr<T1>& lhs, const observer_ptr<T2>& rhs) noexcept
{
return lhs.get() == rhs.get();
}
template <typename T1, typename T2>
constexpr bool operator==(const observer_ptr<T1>& lhs, T2* rhs) noexcept
{
return lhs.get() == rhs;
}
template <typename T1, typename T2>
constexpr bool operator==(T1* lhs, const observer_ptr<T2>& rhs) noexcept
{
return lhs == rhs.get();
}
template <typename T>
constexpr bool operator==(const observer_ptr<T>& lhs, std::nullptr_t) noexcept
{
return !lhs;
}
template <typename T>
constexpr bool operator==(std::nullptr_t, const observer_ptr<T>& rhs) noexcept
{
return !rhs;
}
template <typename T1, typename T2>
constexpr bool operator!=(const observer_ptr<T1>& lhs, const observer_ptr<T2>& rhs) noexcept
{
return !(lhs == rhs);
}
template <typename T1, typename T2>
constexpr bool operator!=(const observer_ptr<T1>& lhs, T2* rhs) noexcept
{
return !(lhs == rhs);
}
template <typename T1, typename T2>
constexpr bool operator!=(T1* lhs, const observer_ptr<T2>& rhs) noexcept
{
return !(lhs == rhs);
}
template <typename T>
constexpr bool operator!=(const observer_ptr<T>& lhs, std::nullptr_t) noexcept
{
return static_cast<bool>(lhs);
}
template <typename T>
constexpr bool operator!=(std::nullptr_t, const observer_ptr<T>& rhs) noexcept
{
return static_cast<bool>(rhs);
}
template <typename T1, typename T2>
constexpr bool operator<(const observer_ptr<T1>& lhs, const observer_ptr<T2>& rhs) noexcept
{
return lhs.get() < rhs.get();
}
template <typename T1, typename T2>
constexpr bool operator<(const observer_ptr<T1>& lhs, T2* rhs) noexcept
{
return lhs.get() < rhs;
}
template <typename T1, typename T2>
constexpr bool operator<(T1* lhs, const observer_ptr<T2>& rhs) noexcept
{
return lhs < rhs.get();
}
}
#endif /* INCLUDED_OBSERVER_PTR_HPP */