MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
weak_ptr.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_MEMORY_WEAK_PTR_HPP__
2#define MSTL_CORE_MEMORY_WEAK_PTR_HPP__
3
11
12#include "shared_ptr.hpp"
14
20
29template <typename T>
30class weak_ptr {
31public:
32 using element_type = T;
33
34private:
35 element_type* ptr_ = nullptr;
36 _INNER __smart_ptr_counter* owner_ = nullptr;
37
38 template <typename U>
39 friend class weak_ptr;
40
41 template <typename U>
42 friend class shared_ptr;
43
44public:
51 weak_ptr(nullptr_t np = nullptr) noexcept {}
52
60 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
61 weak_ptr(const shared_ptr<U>& sp) noexcept
62 : ptr_(sp.get()), owner_(reinterpret_cast<_INNER __smart_ptr_counter*>(sp.owner_)) {
63 if (owner_) {
64 owner_->incref_weak();
65 }
66 }
67
72 weak_ptr(const weak_ptr& wp) noexcept
73 : ptr_(wp.ptr_), owner_(wp.owner_) {
74 if (owner_) {
75 owner_->incref_weak();
76 }
77 }
78
84 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
85 weak_ptr(const weak_ptr<U>& wp) noexcept
86 : ptr_(wp.ptr_), owner_(wp.owner_) {
87 if (owner_) {
88 owner_->incref_weak();
89 }
90 }
91
96 weak_ptr(weak_ptr&& wp) noexcept
97 : ptr_(wp.ptr_), owner_(wp.owner_) {
98 wp.ptr_ = nullptr;
99 wp.owner_ = nullptr;
100 }
101
107 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
108 weak_ptr(weak_ptr<U>&& wp) noexcept
109 : ptr_(wp.ptr_), owner_(wp.owner_) {
110 wp.ptr_ = nullptr;
111 wp.owner_ = nullptr;
112 }
113
119 reset();
120 }
121
127 weak_ptr& operator =(const weak_ptr& wp) noexcept {
128 if (_MSTL addressof(wp) == this) return *this;
129 if (owner_) owner_->decref_weak();
130 ptr_ = wp.ptr_;
131 owner_ = wp.owner_;
132 if (owner_) owner_->incref_weak();
133 return *this;
134 }
135
142 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
143 weak_ptr& operator =(const weak_ptr<U>& wp) noexcept {
144 if (owner_) owner_->decref_weak();
145 ptr_ = wp.ptr_;
146 owner_ = wp.owner_;
147 if (owner_) owner_->incref_weak();
148 return *this;
149 }
150
157 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
158 weak_ptr& operator =(const shared_ptr<U>& sp) noexcept {
159 if (owner_) owner_->decref_weak();
160 ptr_ = sp.get();
161 owner_ = reinterpret_cast<_INNER __smart_ptr_counter*>(sp.owner_);
162 if (owner_) owner_->incref_weak();
163 return *this;
164 }
165
171 weak_ptr& operator =(weak_ptr&& wp) noexcept {
172 if (_MSTL addressof(wp) == this) return *this;
173 if (owner_) owner_->decref_weak();
174 ptr_ = wp.ptr_;
175 owner_ = wp.owner_;
176 wp.ptr_ = nullptr;
177 wp.owner_ = nullptr;
178 return *this;
179 }
180
187 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
188 weak_ptr& operator =(weak_ptr<U>&& wp) noexcept {
189 if (owner_) owner_->decref_weak();
190 ptr_ = wp.ptr_;
191 owner_ = wp.owner_;
192 wp.ptr_ = nullptr;
193 wp.owner_ = nullptr;
194 return *this;
195 }
196
202 void reset() noexcept {
203 if (owner_) {
204 owner_->decref_weak();
205 owner_ = nullptr;
206 }
207 ptr_ = nullptr;
208 }
209
214 void swap(weak_ptr& wp) noexcept {
215 if (_MSTL addressof(wp) == this) return;
216 _MSTL swap(ptr_, wp.ptr_);
217 _MSTL swap(owner_, wp.owner_);
218 }
219
224 MSTL_NODISCARD long use_count() const noexcept {
225 return owner_ ? static_cast<long>(owner_->use_count()) : 0;
226 }
227
232 MSTL_NODISCARD bool expired() const noexcept {
233 return use_count() == 0;
234 }
235
242 MSTL_NODISCARD shared_ptr<T> lock() const noexcept {
243 if (owner_ && owner_->try_incref_strong()) {
244 return shared_ptr<T>(ptr_, owner_);
245 }
246 return shared_ptr<T>();
247 }
248
255 template <typename U>
256 MSTL_NODISCARD bool owner_equal(const weak_ptr<U>& rhs) const noexcept {
257 return owner_ == rhs.owner_;
258 }
259
266 template <typename U>
267 MSTL_NODISCARD bool owner_equal(const shared_ptr<U>& rhs) const noexcept {
268 return owner_ == reinterpret_cast<_INNER __smart_ptr_counter*>(rhs.owner_);
269 }
270
277 template <typename U>
278 MSTL_NODISCARD bool owner_before(const weak_ptr<U>& rhs) const noexcept {
279 return owner_ < rhs.owner_;
280 }
281
288 template <typename U>
289 MSTL_NODISCARD bool owner_before(const shared_ptr<U>& rhs) const noexcept {
290 return owner_ < reinterpret_cast<_INNER __smart_ptr_counter*>(rhs.owner_);
291 }
292};
293
294
300template <typename T>
302
309template <typename T>
311 using is_transparent = void;
312
316 MSTL_NODISCARD bool operator ()(const shared_ptr<T>& lhs, const shared_ptr<T>& rhs) const noexcept {
317 return lhs.owner_before(rhs);
318 }
319
323 MSTL_NODISCARD bool operator ()(const shared_ptr<T>& lhs, const weak_ptr<T>& rhs) const noexcept {
324 return lhs.owner_before(rhs);
325 }
326
330 MSTL_NODISCARD bool operator ()(const weak_ptr<T>& lhs, const shared_ptr<T>& rhs) const noexcept {
331 return lhs.owner_before(rhs);
332 }
333};
334
339template <typename T>
341 using is_transparent = void;
342
346 MSTL_NODISCARD bool operator ()(const weak_ptr<T>& lhs, const weak_ptr<T>& rhs) const noexcept {
347 return lhs.owner_before(rhs);
348 }
349
353 MSTL_NODISCARD bool operator ()(const weak_ptr<T>& lhs, const shared_ptr<T>& rhs) const noexcept {
354 return lhs.owner_before(rhs);
355 }
356
360 MSTL_NODISCARD bool operator ()(const shared_ptr<T>& lhs, const weak_ptr<T>& rhs) const noexcept {
361 return lhs.owner_before(rhs);
362 }
363};
364
370template <>
371struct owner_less<void> {
372 using is_transparent = void;
373
377 template <typename T, typename U>
378 MSTL_NODISCARD bool operator ()(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) const noexcept {
379 return lhs.owner_before(rhs);
380 }
381
385 template <typename T, typename U>
386 MSTL_NODISCARD bool operator ()(const shared_ptr<T>& lhs, const weak_ptr<U>& rhs) const noexcept {
387 return lhs.owner_before(rhs);
388 }
389
393 template <typename T, typename U>
394 MSTL_NODISCARD bool operator ()(const weak_ptr<T>& lhs, const shared_ptr<U>& rhs) const noexcept {
395 return lhs.owner_before(rhs);
396 }
397
401 template <typename T, typename U>
402 MSTL_NODISCARD bool operator ()(const weak_ptr<T>& lhs, const weak_ptr<U>& rhs) const noexcept {
403 return lhs.owner_before(rhs);
404 }
405};
406 // WeakPointer
408
410#endif // MSTL_CORE_MEMORY_WEAK_PTR_HPP__
共享智能指针类模板
弱智能指针类模板
weak_ptr(nullptr_t np=nullptr) noexcept
默认构造函数
MSTL_NODISCARD bool expired() const noexcept
检查观察的对象是否已被销毁
~weak_ptr()
析构函数
void reset() noexcept
重置弱指针
weak_ptr(const weak_ptr< U > &wp) noexcept
类型转换拷贝构造函数
MSTL_NODISCARD bool owner_before(const shared_ptr< U > &rhs) const noexcept
与共享指针比较所有权顺序
weak_ptr(const shared_ptr< U > &sp) noexcept
共享智能指针构造函数
MSTL_NODISCARD long use_count() const noexcept
获取观察对象的引用计数
MSTL_NODISCARD bool owner_before(const weak_ptr< U > &rhs) const noexcept
比较所有权顺序
weak_ptr(const weak_ptr &wp) noexcept
拷贝构造函数
weak_ptr(weak_ptr &&wp) noexcept
移动构造函数
void swap(weak_ptr &wp) noexcept
交换两个弱指针
MSTL_NODISCARD bool owner_equal(const weak_ptr< U > &rhs) const noexcept
检查所有权是否相等
weak_ptr & operator=(const weak_ptr &wp) noexcept
拷贝赋值运算符
T element_type
元素类型
weak_ptr(weak_ptr< U > &&wp) noexcept
类型转换移动构造函数
MSTL_NODISCARD shared_ptr< T > lock() const noexcept
尝试获取共享智能指针
MSTL_NODISCARD bool owner_equal(const shared_ptr< U > &rhs) const noexcept
与共享指针检查所有权是否相等
MSTL_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
decltype(nullptr) nullptr_t
空指针类型
#define _MSTL
全局命名空间MSTL前缀
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
void swap()=delete
删除无参数的swap重载
MSTL共享智能指针实现
void is_transparent
支持透明比较
void is_transparent
支持透明比较
void is_transparent
支持透明比较
智能指针的所有权比较器