1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_INTERNAL_MEMORY_SHARED_PTR
|
---|
30 | #define LIBCPP_INTERNAL_MEMORY_SHARED_PTR
|
---|
31 |
|
---|
32 | #include <exception>
|
---|
33 |
|
---|
34 | namespace std
|
---|
35 | {
|
---|
36 | /**
|
---|
37 | * 20.8.2.1, class bad_weak_ptr:
|
---|
38 | */
|
---|
39 |
|
---|
40 | class bad_weak_ptr: public exception
|
---|
41 | {
|
---|
42 | public:
|
---|
43 | bad_weak_ptr() noexcept = default;
|
---|
44 |
|
---|
45 | const char* what() const noexcept override
|
---|
46 | {
|
---|
47 | return "std::bad_weak_ptr";
|
---|
48 | }
|
---|
49 | };
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * 20.8.2.2, class template shared_ptr:
|
---|
53 | */
|
---|
54 |
|
---|
55 | template<class T>
|
---|
56 | class shared_ptr
|
---|
57 | {
|
---|
58 | public:
|
---|
59 | using element_type = T;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * 20.8.2.2.1, constructors:
|
---|
63 | */
|
---|
64 |
|
---|
65 | constexpr shared_ptr() noexcept;
|
---|
66 |
|
---|
67 | template<class U>
|
---|
68 | explicit shared_ptr(U* ptr);
|
---|
69 |
|
---|
70 | template<class U, class D>
|
---|
71 | shared_ptr(U* ptr, D deleter);
|
---|
72 |
|
---|
73 | template<class U, class D, class A>
|
---|
74 | shared_ptr(U* ptr, D deleter, A alloc);
|
---|
75 |
|
---|
76 | template<class D>
|
---|
77 | shared_ptr(nullptr_t, D deleter);
|
---|
78 |
|
---|
79 | template<class D, class A>
|
---|
80 | shared_ptr(nullptr_t, D deleter, A alloc);
|
---|
81 |
|
---|
82 | template<class U>
|
---|
83 | shared_ptr(const shared_ptr<U>& other, value_type* ptr);
|
---|
84 |
|
---|
85 | shared_ptr(const shared_ptr& other);
|
---|
86 |
|
---|
87 | template<class U>
|
---|
88 | shared_ptr(const shared_ptr<U>& other);
|
---|
89 |
|
---|
90 | shared_ptr(shared_ptr&& other);
|
---|
91 |
|
---|
92 | template<class U>
|
---|
93 | shared_ptr(shared_ptr<U>&& other);
|
---|
94 |
|
---|
95 | template<class U>
|
---|
96 | explicit shared_ptr(const weak_ptr<U>& other);
|
---|
97 |
|
---|
98 | template<class U, class D>
|
---|
99 | shared_ptr(unique_ptr<U, D>&& other);
|
---|
100 |
|
---|
101 | constexpr shared_ptr(nullptr_t) noexcept
|
---|
102 | : shared_ptr{}
|
---|
103 | { /* DUMMY BODY */ }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * 20.8.2.2.2, destructor:
|
---|
107 | */
|
---|
108 |
|
---|
109 | ~shared_ptr();
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * 20.8.2.2.3, assignment:
|
---|
113 | */
|
---|
114 |
|
---|
115 | shared_ptr& operator=(const shared_ptr& rhs) noexcept;
|
---|
116 |
|
---|
117 | template<class U>
|
---|
118 | shared_ptr& operator=(const shared_ptr<U>& rhs) noexcept;
|
---|
119 |
|
---|
120 | shared_ptr& operator=(shared_ptr&& rhs) noexcept;
|
---|
121 |
|
---|
122 | template<class U>
|
---|
123 | shared_ptr& operator=(shared_ptr<U>&& rhs) noexcept;
|
---|
124 |
|
---|
125 | template<class U, class D>
|
---|
126 | shared_ptr& operator=(unique_ptr<U, D>&& rhs);
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * 20.8.2.2.4, modifiers:
|
---|
130 | */
|
---|
131 |
|
---|
132 | void swap(shared_ptr& other) noexcept;
|
---|
133 |
|
---|
134 | void reset() noexcept;
|
---|
135 |
|
---|
136 | template<class U>
|
---|
137 | void reset(U* ptr);
|
---|
138 |
|
---|
139 | template<class U, class D>
|
---|
140 | void reset(U* ptr, D deleter);
|
---|
141 |
|
---|
142 | template<class U, class D, class A>
|
---|
143 | void reset(U* ptr, D deleter, A alloc);
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * 20.8.2.2.5, observers:
|
---|
147 | */
|
---|
148 |
|
---|
149 | element_type* get() const noexcept;
|
---|
150 |
|
---|
151 | T& operator*() const noexcept;
|
---|
152 |
|
---|
153 | T* operator->() const noexcept;
|
---|
154 |
|
---|
155 | long use_count() const noexcept;
|
---|
156 |
|
---|
157 | bool unique() const noexcept;
|
---|
158 |
|
---|
159 | explicit operator bool() const noexcept;
|
---|
160 |
|
---|
161 | template<class U>
|
---|
162 | bool owner_before(const shared_ptr<U>& ptr) const;
|
---|
163 |
|
---|
164 | template<class U>
|
---|
165 | bool owner_before(const weak_ptr<U>& ptr) const;
|
---|
166 |
|
---|
167 | /* private: */
|
---|
168 | };
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * 20.8.2.2.6, shared_ptr creation:
|
---|
172 | */
|
---|
173 |
|
---|
174 | template<class T, class... Args>
|
---|
175 | shared_ptr<T> make_shared(Args&&... args);
|
---|
176 |
|
---|
177 | template<class T, class A, class... Args>
|
---|
178 | shared_ptr<T> allocate_shared(const A& alloc, Args&&... args);
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * 20.8.2.2.7, shared_ptr comparisons:
|
---|
182 | */
|
---|
183 |
|
---|
184 | template<class T, class U>
|
---|
185 | bool operator==(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept;
|
---|
186 |
|
---|
187 | template<class T, class U>
|
---|
188 | bool operator!=(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept;
|
---|
189 |
|
---|
190 | template<class T, class U>
|
---|
191 | bool operator<(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept;
|
---|
192 |
|
---|
193 | template<class T, class U>
|
---|
194 | bool operator>(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept;
|
---|
195 |
|
---|
196 | template<class T, class U>
|
---|
197 | bool operator<=(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept;
|
---|
198 |
|
---|
199 | template<class T, class U>
|
---|
200 | bool operator>=(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept;
|
---|
201 |
|
---|
202 | template<class T>
|
---|
203 | bool operator==(const shared_ptr<T>& lhs, nullptr_t) noexcept;
|
---|
204 |
|
---|
205 | template<class T>
|
---|
206 | bool operator==(nullptr_t, const shared_ptr<T>& rhs) noexcept;
|
---|
207 |
|
---|
208 | template<class T>
|
---|
209 | bool operator!=(const shared_ptr<T>& lhs, nullptr_t) noexcept;
|
---|
210 |
|
---|
211 | template<class T>
|
---|
212 | bool operator!=(nullptr_t, const shared_ptr<T>& rhs) noexcept;
|
---|
213 |
|
---|
214 | template<class T>
|
---|
215 | bool operator<(const shared_ptr<T>& lhs, nullptr_t) noexcept;
|
---|
216 |
|
---|
217 | template<class T>
|
---|
218 | bool operator<(nullptr_t, const shared_ptr<T>& rhs) noexcept;
|
---|
219 |
|
---|
220 | template<class T>
|
---|
221 | bool operator>(const shared_ptr<T>& lhs, nullptr_t) noexcept;
|
---|
222 |
|
---|
223 | template<class T>
|
---|
224 | bool operator>(nullptr_t, const shared_ptr<T>& rhs) noexcept;
|
---|
225 |
|
---|
226 | template<class T>
|
---|
227 | bool operator<=(const shared_ptr<T>& lhs, nullptr_t) noexcept;
|
---|
228 |
|
---|
229 | template<class T>
|
---|
230 | bool operator<=(nullptr_t, const shared_ptr<T>& rhs) noexcept;
|
---|
231 |
|
---|
232 | template<class T>
|
---|
233 | bool operator>=(const shared_ptr<T>& lhs, nullptr_t) noexcept;
|
---|
234 |
|
---|
235 | template<class T>
|
---|
236 | bool operator>=(nullptr_t, const shared_ptr<T>& rhs) noexcept;
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * 20.8.2.2.8, shared_ptr specialized algorithms:
|
---|
240 | */
|
---|
241 |
|
---|
242 | template<class T>
|
---|
243 | void swap(shared_ptr<T>& lhs, shared_ptr<T>& rhs) noexcept
|
---|
244 | {
|
---|
245 | lhs.swap(rhs);
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * 20.8.2.2.9, shared_ptr casts:
|
---|
250 | */
|
---|
251 |
|
---|
252 | template<class T, class U>
|
---|
253 | shared_ptr<T> static_pointer_cast(const shared_ptr<U>& ptr) noexcept;
|
---|
254 |
|
---|
255 | template<class T, class U>
|
---|
256 | shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& ptr) noexcept;
|
---|
257 |
|
---|
258 | template<class T, class U>
|
---|
259 | shared_ptr<T> const_pointer_cast(const shared_ptr<U>& ptr) noexcept;
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * 20.8.2.2.10, shared_ptr get_deleter:
|
---|
263 | */
|
---|
264 |
|
---|
265 | template<class D, class T>
|
---|
266 | D* get_deleter(const shared_ptr<T>& ptr) noexcept;
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * 20.8.2.2.11, shared_ptr I/O:
|
---|
270 | */
|
---|
271 |
|
---|
272 | template<class Char, class Traits, class T>
|
---|
273 | basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os,
|
---|
274 | const shared_ptr<T>& ptr);
|
---|
275 | }
|
---|
276 |
|
---|
277 | #endif
|
---|