Skip to content
0.2.0-dev Download ZIP
311 lines ·9.0 KB Raw
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------
2//
3// Foundation/NSSharedPtr.hpp
4//
5// Copyright 2020-2024 Apple Inc.
6//
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License at
10//
11// http://www.apache.org/licenses/LICENSE-2.0
12//
13// Unless required by applicable law or agreed to in writing, software
14// distributed under the License is distributed on an "AS IS" BASIS,
15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16// See the License for the specific language governing permissions and
17// limitations under the License.
18//
19//-------------------------------------------------------------------------------------------------------------------------------------------------------------
20
21#pragma once
22
23#include <cstddef>
24#include "NSDefines.hpp"
25#include "NSObject.hpp"
26
27namespace NS
28{
29template <class _Class>
30class SharedPtr
31{
32public:
33 /**
34 * Create a new null pointer.
35 */
36 SharedPtr();
37
38 /**
39 * Destroy this SharedPtr, decreasing the reference count.
40 */
41 ~SharedPtr();
42
43 /**
44 * Create a new null pointer.
45 */
46 SharedPtr(std::nullptr_t) noexcept;
47
48 /**
49 * SharedPtr copy constructor.
50 */
51 SharedPtr(const SharedPtr<_Class>& other) noexcept;
52
53 /**
54 * Construction from another pointee type.
55 */
56 template <class _OtherClass>
57 SharedPtr(const SharedPtr<_OtherClass>& other, typename std::enable_if_t<std::is_convertible_v<_OtherClass *, _Class *>> * = nullptr) noexcept;
58
59 /**
60 * SharedPtr move constructor.
61 */
62 SharedPtr(SharedPtr<_Class>&& other) noexcept;
63
64 /**
65 * Move from another pointee type.
66 */
67 template <class _OtherClass>
68 SharedPtr(SharedPtr<_OtherClass>&& other, typename std::enable_if_t<std::is_convertible_v<_OtherClass *, _Class *>> * = nullptr) noexcept;
69
70 /**
71 * Copy assignment operator.
72 * Copying increases reference count. Only releases previous pointee if objects are different.
73 */
74 SharedPtr& operator=(const SharedPtr<_Class>& other);
75
76 /**
77 * Copy-assignment from different pointee.
78 * Copying increases reference count. Only releases previous pointee if objects are different.
79 */
80 template <class _OtherClass>
81 typename std::enable_if_t<std::is_convertible_v<_OtherClass *, _Class *>, SharedPtr &>
82 operator=(const SharedPtr<_OtherClass>& other);
83
84 /**
85 * Move assignment operator.
86 * Move without affecting reference counts, unless pointees are equal. Moved-from object is reset to nullptr.
87 */
88 SharedPtr& operator=(SharedPtr<_Class>&& other);
89
90 /**
91 * Move-asignment from different pointee.
92 * Move without affecting reference counts, unless pointees are equal. Moved-from object is reset to nullptr.
93 */
94 template <class _OtherClass>
95 typename std::enable_if_t<std::is_convertible_v<_OtherClass *, _Class *>, SharedPtr &>
96 operator=(SharedPtr<_OtherClass>&& other);
97
98 /**
99 * Access raw pointee.
100 * @warning Avoid wrapping the returned value again, as it may lead double frees unless this object becomes detached.
101 */
102 _Class* get() const;
103
104 /**
105 * Call operations directly on the pointee.
106 */
107 _Class* operator->() const;
108
109 /**
110 * Implicit cast to bool.
111 */
112 explicit operator bool() const;
113
114 /**
115 * Reset this SharedPtr to null, decreasing the reference count.
116 */
117 void reset();
118
119 /**
120 * Detach the SharedPtr from the pointee, without decreasing the reference count.
121 */
122 void detach();
123
124 template <class _OtherClass>
125 friend SharedPtr<_OtherClass> RetainPtr(_OtherClass* ptr);
126
127 template <class _OtherClass>
128 friend SharedPtr<_OtherClass> TransferPtr(_OtherClass* ptr);
129
130private:
131 _Class* m_pObject;
132};
133
134/**
135 * Create a SharedPtr by retaining an existing raw pointer.
136 * Increases the reference count of the passed-in object.
137 * If the passed-in object was in an AutoreleasePool, it will be removed from it.
138 */
139template <class _Class>
140_NS_INLINE NS::SharedPtr<_Class> RetainPtr(_Class* pObject)
141{
142 NS::SharedPtr<_Class> ret;
143 ret.m_pObject = Object::sendMessage<_Class*>(pObject, _NS_PRIVATE_SEL(retain));
144 return ret;
145}
146
147/*
148 * Create a SharedPtr by transfering the ownership of an existing raw pointer to SharedPtr.
149 * Does not increase the reference count of the passed-in pointer, it is assumed to be >= 1.
150 * This method does not remove objects from an AutoreleasePool.
151*/
152template <class _Class>
153_NS_INLINE NS::SharedPtr<_Class> TransferPtr(_Class* pObject)
154{
155 NS::SharedPtr<_Class> ret;
156 ret.m_pObject = pObject;
157 return ret;
158}
159
160}
161
162template <class _Class>
163_NS_INLINE NS::SharedPtr<_Class>::SharedPtr()
164 : m_pObject(nullptr)
165{
166}
167
168template <class _Class>
169_NS_INLINE NS::SharedPtr<_Class>::~SharedPtr<_Class>() __attribute__((no_sanitize("undefined")))
170{
171 Object::sendMessage<void>(m_pObject, _NS_PRIVATE_SEL(release));
172}
173
174template <class _Class>
175_NS_INLINE NS::SharedPtr<_Class>::SharedPtr(std::nullptr_t) noexcept
176 : m_pObject(nullptr)
177{
178}
179
180template <class _Class>
181_NS_INLINE NS::SharedPtr<_Class>::SharedPtr(const SharedPtr<_Class>& other) noexcept
182 : m_pObject(Object::sendMessage<_Class*>(other.m_pObject, _NS_PRIVATE_SEL(retain)))
183{
184}
185
186template <class _Class>
187template <class _OtherClass>
188_NS_INLINE NS::SharedPtr<_Class>::SharedPtr(const SharedPtr<_OtherClass>& other, typename std::enable_if_t<std::is_convertible_v<_OtherClass *, _Class *>> *) noexcept
189 : m_pObject(Object::sendMessage<_Class*>(other.get(), _NS_PRIVATE_SEL(retain)))
190{
191}
192
193template <class _Class>
194_NS_INLINE NS::SharedPtr<_Class>::SharedPtr(SharedPtr<_Class>&& other) noexcept
195 : m_pObject(other.m_pObject)
196{
197 other.m_pObject = nullptr;
198}
199
200template <class _Class>
201template <class _OtherClass>
202_NS_INLINE NS::SharedPtr<_Class>::SharedPtr(SharedPtr<_OtherClass>&& other, typename std::enable_if_t<std::is_convertible_v<_OtherClass *, _Class *>> *) noexcept
203 : m_pObject(reinterpret_cast<_Class*>(other.get()))
204{
205 other.detach();
206}
207
208template <class _Class>
209_NS_INLINE _Class* NS::SharedPtr<_Class>::get() const
210{
211 return m_pObject;
212}
213
214template <class _Class>
215_NS_INLINE _Class* NS::SharedPtr<_Class>::operator->() const
216{
217 return m_pObject;
218}
219
220template <class _Class>
221_NS_INLINE NS::SharedPtr<_Class>::operator bool() const
222{
223 return nullptr != m_pObject;
224}
225
226template <class _Class>
227_NS_INLINE void NS::SharedPtr<_Class>::reset() __attribute__((no_sanitize("undefined")))
228{
229 Object::sendMessage<void>(m_pObject, _NS_PRIVATE_SEL(release));
230 m_pObject = nullptr;
231}
232
233template <class _Class>
234_NS_INLINE void NS::SharedPtr<_Class>::detach()
235{
236 m_pObject = nullptr;
237}
238
239template <class _Class>
240_NS_INLINE NS::SharedPtr<_Class>& NS::SharedPtr<_Class>::operator=(const SharedPtr<_Class>& other) __attribute__((no_sanitize("undefined")))
241{
242 _Class* pOldObject = m_pObject;
243
244 m_pObject = Object::sendMessage<_Class*>(other.m_pObject, _NS_PRIVATE_SEL(retain));
245
246 Object::sendMessage<void>(pOldObject, _NS_PRIVATE_SEL(release));
247
248 return *this;
249}
250
251template <class _Class>
252template <class _OtherClass>
253typename std::enable_if_t<std::is_convertible_v<_OtherClass *, _Class *>, NS::SharedPtr<_Class> &>
254_NS_INLINE NS::SharedPtr<_Class>::operator=(const SharedPtr<_OtherClass>& other) __attribute__((no_sanitize("undefined")))
255{
256 _Class* pOldObject = m_pObject;
257
258 m_pObject = Object::sendMessage<_Class*>(other.get(), _NS_PRIVATE_SEL(retain));
259
260 Object::sendMessage<void>(pOldObject, _NS_PRIVATE_SEL(release));
261
262 return *this;
263}
264
265template <class _Class>
266_NS_INLINE NS::SharedPtr<_Class>& NS::SharedPtr<_Class>::operator=(SharedPtr<_Class>&& other) __attribute__((no_sanitize("undefined")))
267{
268 if (m_pObject != other.m_pObject)
269 {
270 Object::sendMessage<void>(m_pObject, _NS_PRIVATE_SEL(release));
271 m_pObject = other.m_pObject;
272 }
273 else
274 {
275 m_pObject = other.m_pObject;
276 Object::sendMessage<void>(other.m_pObject, _NS_PRIVATE_SEL(release));
277 }
278 other.m_pObject = nullptr;
279 return *this;
280}
281
282template <class _Class>
283template <class _OtherClass>
284typename std::enable_if_t<std::is_convertible_v<_OtherClass *, _Class *>, NS::SharedPtr<_Class> &>
285_NS_INLINE NS::SharedPtr<_Class>::operator=(SharedPtr<_OtherClass>&& other) __attribute__((no_sanitize("undefined")))
286{
287 if (m_pObject != other.get())
288 {
289 Object::sendMessage<void>(m_pObject, _NS_PRIVATE_SEL(release));
290 m_pObject = reinterpret_cast<_Class*>(other.get());
291 other.detach();
292 }
293 else
294 {
295 m_pObject = other.get();
296 other.reset();
297 }
298 return *this;
299}
300
301template <class _ClassLhs, class _ClassRhs>
302_NS_INLINE bool operator==(const NS::SharedPtr<_ClassLhs>& lhs, const NS::SharedPtr<_ClassRhs>& rhs)
303{
304 return lhs.get() == rhs.get();
305}
306
307template <class _ClassLhs, class _ClassRhs>
308_NS_INLINE bool operator!=(const NS::SharedPtr<_ClassLhs>& lhs, const NS::SharedPtr<_ClassRhs>& rhs)
309{
310 return lhs.get() != rhs.get();
311}