-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlightptr.hpp
338 lines (264 loc) · 6.16 KB
/
lightptr.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
334
335
336
337
338
#ifndef GNR_LIGHTPTR_HPP
# define GNR_LIGHTPTR_HPP
# pragma once
#include <cassert>
#include <atomic>
#include <memory>
#include <type_traits>
#include <utility>
namespace gnr
{
namespace
{
using counter_type = unsigned;
using atomic_counter_type = std::atomic<counter_type>;
template <typename T>
using deleter_type = void (*)(T*);
template <typename U>
struct ref_type
{
using type = U&;
};
template <>
struct ref_type<void>
{
using type = void;
};
template <typename U>
using ref_type_t = typename ref_type<U>::type;
}
template <typename T>
class light_ptr
{
template <typename U, typename V>
struct deletion_type
{
using type = V;
};
template <typename U, typename V>
struct deletion_type<U[], V>
{
using type = V[];
};
template <typename U, typename V, std::size_t N>
struct deletion_type<U[N], V>
{
using type = V[];
};
template <typename ...A>
using deletion_type_t = typename deletion_type<A...>::type;
using element_type = std::remove_extent_t<T>;
class counter_base
{
friend class light_ptr;
using invoker_type = void (*)(counter_base*, element_type*) noexcept;
atomic_counter_type counter_{};
invoker_type const invoker_;
protected:
explicit counter_base(counter_type const c,
invoker_type const invoker) noexcept :
counter_(c),
invoker_(invoker)
{
}
public:
template <typename U>
std::enable_if_t<!std::is_void<U>::value> dec_ref(U* const ptr) noexcept
{
if (counter_type(1) ==
counter_.fetch_sub(counter_type(1), std::memory_order_relaxed))
{
using type_must_be_complete = char[sizeof(U) ? 1 : -1];
(void)sizeof(type_must_be_complete);
invoker_(this, ptr);
}
// else do nothing
}
template <typename U>
std::enable_if_t<std::is_void<U>::value> dec_ref(U* const ptr) noexcept
{
if (counter_type(1) ==
counter_.fetch_sub(counter_type(1), std::memory_order_relaxed))
{
invoker_(this, ptr);
}
// else do nothing
}
void inc_ref() noexcept
{
counter_.fetch_add(counter_type(1), std::memory_order_relaxed);
}
};
template <typename D>
class counter : public counter_base
{
std::decay_t<D> const d_;
static void invoked(counter_base* const ptr,
element_type* const e) noexcept
{
auto const c(static_cast<counter<D>*>(ptr));
// invoke deleter on the element
c->d_(e);
// delete from a static member function
delete c;
}
public:
explicit counter(counter_type const c, D&& d) noexcept :
counter_base(c, invoked),
d_(std::forward<D>(d))
{
}
};
private:
template <typename U> friend struct std::hash;
counter_base* counter_{};
element_type* ptr_;
public:
light_ptr() = default;
template <typename U>
explicit light_ptr(U* const p)
{
reset(p);
}
template <typename U, typename D>
explicit light_ptr(U* const p, D&& d)
{
reset(p, std::forward<D>(d));
}
light_ptr(light_ptr const& other) noexcept { *this = other; }
light_ptr(light_ptr&& other) noexcept { *this = std::move(other); }
~light_ptr() noexcept
{
if (counter_)
{
counter_->dec_ref(ptr_);
}
// else do nothing
}
light_ptr& operator=(light_ptr const& rhs) noexcept
{
if (*this != rhs)
{
if (counter_)
{
counter_->dec_ref(ptr_);
}
// else do nothing
if ((counter_ = rhs.counter_))
{
counter_->inc_ref();
}
// else do nothing
ptr_ = rhs.ptr_;
}
// else do nothing
return *this;
}
light_ptr& operator=(light_ptr&& rhs) noexcept
{
counter_ = rhs.counter_;
rhs.counter_ = nullptr;
ptr_ = rhs.ptr_;
return *this;
}
light_ptr& operator=(std::nullptr_t const) noexcept
{
reset();
return *this;
}
bool operator<(light_ptr const& rhs) const noexcept
{
return counter_ < rhs.counter_;
}
bool operator==(light_ptr const& rhs) const noexcept
{
return counter_ == rhs.counter_;
}
bool operator!=(light_ptr const& rhs) const noexcept
{
return !operator==(rhs);
}
bool operator==(std::nullptr_t const) const noexcept { return !counter_; }
bool operator!=(std::nullptr_t const) const noexcept { return counter_; }
explicit operator bool() const noexcept { return counter_; }
ref_type_t<T> operator*() const noexcept
{
return *reinterpret_cast<T*>(ptr_);
}
auto operator->() const noexcept { return reinterpret_cast<T*>(ptr_); }
template <typename U = T,
typename = std::enable_if_t<std::is_array<U>{}>
>
ref_type_t<element_type> operator[](std::size_t const i) const noexcept
{
return ptr_[i];
}
auto get() const noexcept { return ptr_; }
void reset() noexcept { reset(nullptr); }
void reset(std::nullptr_t const) noexcept
{
if (counter_)
{
counter_->dec_ref(ptr_);
counter_ = nullptr;
}
// else do nothing
}
template <typename U>
void reset(U* const p)
{
reset(p,
[](element_type* const p) noexcept {
std::default_delete<deletion_type_t<T, U>>()(
static_cast<U*>(p)
);
}
);
}
template <typename U, typename D>
void reset(U* const p, D&& d)
{
if (counter_)
{
counter_->dec_ref(ptr_);
}
// else do nothing
counter_ = p ?
new counter<D>(counter_type(1), std::forward<D>(d)) :
nullptr;
ptr_ = p;
}
void swap(light_ptr& other) noexcept
{
std::swap(counter_, other.counter_);
std::swap(ptr_, other.ptr_);
}
bool unique() const noexcept
{
return counter_type(1) == use_count();
}
counter_type use_count() const noexcept
{
return counter_ ?
counter_->counter_.load(std::memory_order_relaxed) :
counter_type{};
}
};
template<class T, typename ...A>
inline light_ptr<T> make_light(A&& ...args)
{
return light_ptr<T>(new T(std::forward<A>(args)...));
}
}
namespace std
{
template <typename T>
struct hash<gnr::light_ptr<T> >
{
size_t operator()(gnr::light_ptr<T> const& l) const noexcept
{
return hash<typename gnr::light_ptr<T>::element_type*>()(l.ptr_);
}
};
}
#endif // GNR_LIGHTPTR_HPP