Lagrange
weak_ptr.h
1// Source: https://github.com/X-czh/smart_ptr
2// SPDX-License-Identifier: MIT
3//
4// This file has been modified by Adobe.
5//
6// All modifications are Copyright 2022 Adobe.
7
8#pragma once
9
10#include <lagrange/internal/smart_ptr/control_block.h>
11#include <lagrange/internal/shared_ptr.h>
12
13#include <type_traits> // remove_extent
14
15namespace lagrange::internal {
16
17// Forward declarations
18
19template <typename T>
20class shared_ptr;
21
22// 20.7.2.3 Class template weak_ptr
23
24template <typename T>
26{
27public:
28 template <typename U>
29 friend class shared_ptr;
30
31 template <typename U>
32 friend class weak_ptr;
33
34 using element_type = typename std::remove_extent<T>::type;
35
36 // 20.7.2.3.1, constructors:
37
40 constexpr weak_ptr() noexcept
41 : _ptr{}
42 , _control_block{}
43 {}
44
47 template <class U>
48 weak_ptr(shared_ptr<U> const& sp) noexcept
49 : _ptr{sp._ptr}
50 , _control_block{sp._control_block}
51 {
52 if (_control_block) _control_block->inc_wref();
53 }
54
57 weak_ptr(weak_ptr const& wp) noexcept
58 : _ptr{wp._ptr}
59 , _control_block{wp._control_block}
60 {
61 if (_control_block) _control_block->inc_wref();
62 }
63
66 template <class U>
67 weak_ptr(weak_ptr<U> const& wp) noexcept
68 : _ptr{wp._ptr}
69 , _control_block{wp._control_block}
70 {
71 if (_control_block) _control_block->inc_wref();
72 }
73
74 // 20.7.2.3.2, destructor
75
76 ~weak_ptr()
77 {
78 if (_control_block) _control_block->dec_wref();
79 }
80
81 // 20.7.2.3.3, assignment
82
83 weak_ptr& operator=(const weak_ptr& wp) noexcept
84 {
85 weak_ptr{wp}.swap(*this);
86 return *this;
87 }
88
89 template <typename U>
90 weak_ptr& operator=(const weak_ptr<U>& wp) noexcept
91 {
92 weak_ptr{wp}.swap(*this);
93 return *this;
94 }
95
96 template <typename U>
97 weak_ptr& operator=(const shared_ptr<U>& sp) noexcept
98 {
99 weak_ptr{sp}.swap(*this);
100 return *this;
101 }
102
103 // 20.7.2.3.4, modifiers
104
106 void swap(weak_ptr& wp) noexcept
107 {
108 using std::swap;
109 swap(_ptr, wp._ptr);
110 swap(_control_block, wp._control_block);
111 }
112
114 void reset() noexcept { weak_ptr{}.swap(*this); }
115
116 // 20.7.2.3.5, observers
117
119 long use_count() const noexcept { return (_control_block) ? _control_block->use_count() : 0; }
120
122 bool expired() const noexcept { return (_control_block) ? _control_block->expired() : false; }
123
125 shared_ptr<T> lock() const noexcept
126 {
127 return (expired()) ? shared_ptr<T>{} : shared_ptr<T>{*this};
128 }
129
130private:
131 element_type* _ptr;
132 control_block_base* _control_block;
133};
134
135// 20.7.2.3.6, specialized algorithm
136
138template <typename T>
139inline void swap(weak_ptr<T>& wp1, weak_ptr<T>& wp2)
140{
141 wp1.swap(wp2);
142}
143
144} // namespace lagrange::internal
Definition: control_block.h:24
NOT implemented: custom allocator support.
Definition: shared_ptr.h:110
Definition: weak_ptr.h:26
constexpr weak_ptr() noexcept
Default constructor, creates an empty weak_ptr Postconditions: use_count() == 0.
Definition: weak_ptr.h:40
void reset() noexcept
Resets *this to empty.
Definition: weak_ptr.h:114
long use_count() const noexcept
Gets use_count.
Definition: weak_ptr.h:119
weak_ptr(shared_ptr< U > const &sp) noexcept
Conversion constructor: shares ownership with sp Postconditions: use_count() == sp....
Definition: weak_ptr.h:48
weak_ptr(weak_ptr const &wp) noexcept
Copy constructor: shares ownership with wp Postconditions: use_count() == wp.use_count().
Definition: weak_ptr.h:57
weak_ptr(weak_ptr< U > const &wp) noexcept
Copy constructor: shares ownership with wp Postconditions: use_count() == wp.use_count().
Definition: weak_ptr.h:67
bool expired() const noexcept
Checks if use_count == 0.
Definition: weak_ptr.h:122
shared_ptr< T > lock() const noexcept
Checks if there is a managed object.
Definition: weak_ptr.h:125
void swap(weak_ptr &wp) noexcept
Exchanges the contents of *this and sp.
Definition: weak_ptr.h:106
constexpr void swap(function_ref< R(Args...)> &lhs, function_ref< R(Args...)> &rhs) noexcept
Swaps the referred callables of lhs and rhs.
Definition: function_ref.h:114
nullptr_t, size_t, ptrdiff_t basic_ostream bad_weak_ptr extent, remove_extent, is_array,...
Definition: attribute_string_utils.h:21
void swap(shared_ptr< T > &sp1, shared_ptr< T > &sp2)
Swaps with another shared_ptr.
Definition: shared_ptr.h:384