-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintrusive_ptr.hpp
200 lines (159 loc) · 5.15 KB
/
intrusive_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
#ifndef INTRUSIVE_PTR_HPP
#define INTRUSIVE_PTR_HPP
//C++ 11 intrusive_ptr
//frindle api for boost
#include <memory>
#include <utility>
#include "ref_counted.hpp"
namespace std {
template<class T>
class intrusive_ptr {
public:
using pointer = T*;
using const_pointer = const T*;
using element_type = T;
using reference = T&;
using const_reference = const T&;
constexpr intrusive_ptr() noexcept : ptr_(nullptr) {}
intrusive_ptr(pointer raw_ptr, bool add_ref = true) noexcept {
set_ptr(raw_ptr, add_ref);
}
intrusive_ptr(intrusive_ptr &&other) noexcept : ptr_(other.detach()) {}
intrusive_ptr(const intrusive_ptr &other) noexcept {
set_ptr(other.get(), true);
}
template<class Y>
intrusive_ptr(intrusive_ptr<Y> other) : ptr_(other.detach()) {
static_assert(std::is_convertible<Y *, T *>::value, "Y* is not assignable to T*");
}
intrusive_ptr &operator=(pointer ptr) noexcept {
reset(ptr);
return *this;
}
intrusive_ptr &operator=(intrusive_ptr other) noexcept {
swap(other);
return *this;
}
~intrusive_ptr() {
if (ptr_) {
intrusive_ptr_release(ptr_);
}
}
pointer detach() noexcept {
auto result = ptr_;
if (result) {
ptr_ = nullptr;
}
return result;
}
pointer release() noexcept {
return detach();
}
void reset(pointer new_value = nullptr, bool add_ref = true) noexcept {
auto old = ptr_;
set_ptr(new_value, add_ref);
if (old) {
intrusive_ptr_release(old);
}
}
pointer get() const noexcept {
return ptr_;
}
pointer operator->() const noexcept {
return ptr_;
}
reference operator*() const {
return *ptr_;
}
explicit operator bool() const noexcept {
return ptr_ != nullptr;
}
void swap(intrusive_ptr &other) noexcept {
std::swap(ptr_, other.ptr_);
}
template<class C>
intrusive_ptr<C> down_pointer_cast() const noexcept {
return (ptr_) ? dynamic_cast<C *>(get()) : nullptr;
}
template<class C>
intrusive_ptr<C> up_pointer_cast() const noexcept {
return (ptr_) ? static_cast<C *>(get()) : nullptr;
}
private:
inline void set_ptr(pointer raw_ptr, bool add_ref) noexcept {
ptr_ = raw_ptr;
if (raw_ptr && add_ref) {
intrusive_ptr_add_ref(raw_ptr);
}
}
pointer ptr_;
};
template <class T>
bool operator==(const intrusive_ptr<T>& x, std::nullptr_t) {
return !x;
}
template <class T>
bool operator==(std::nullptr_t, const intrusive_ptr<T>& x) {
return !x;
}
template <class T>
bool operator!=(const intrusive_ptr<T>& x, std::nullptr_t) {
return static_cast<bool>(x);
}
template <class T>
bool operator!=(std::nullptr_t, const intrusive_ptr<T>& x) {
return static_cast<bool>(x);
}
template<class T, class U>
bool operator==(intrusive_ptr<T> const &a, intrusive_ptr<U> const &b) noexcept {
return a.get() == b.get();
}
template<class T, class U>
bool operator!=(intrusive_ptr<T> const &a, intrusive_ptr<U> const &b) noexcept {
return !(a == b);
}
template<class T>
bool operator==(intrusive_ptr<T> const &a, T *b) noexcept {
return a.get() == b;
}
template<class T>
bool operator!=(intrusive_ptr<T> const &a, T *b) noexcept {
return a.get() != b;
}
template<class T>
bool operator==(T *a, intrusive_ptr<T> const &b)noexcept {
return a == b.get();
}
template<class T>
bool operator!=(T *a, intrusive_ptr<T> const &b)noexcept {
return a != b.get();
}
template<class T, class U>
bool operator<(intrusive_ptr<T> const &a, intrusive_ptr<U> const &b) noexcept; // never throws
template<class T>
inline void swap(intrusive_ptr<T> &a, intrusive_ptr<T> &b) noexcept {
a.swap(b);
}
template<class T>
T *get_pointer(intrusive_ptr<T> const &p) noexcept {
return p.get();
}
template<class T, class U>
intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const &r) noexcept {
return r.template up_pointer_cast<T>();
}
template<class T, class U>
intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const &r) noexcept; // never throws
template<class T, class U>
intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const &r) noexcept {
return r.template down_pointer_cast<T>();
}
template<typename T>
struct hash<intrusive_ptr<T>> {
size_t operator()(const intrusive_ptr<T>& ptr) const noexcept {
using pointer_t = intrusive_ptr<_Tp> ;
return std::hash<typename pointer_t::pointer>()(ptr.get());
}
};
}
#endif //INTRUSIVE_PTR_HPP