Loading...
Searching...
No Matches
JSRetainPtr.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2005-2020 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY 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#pragma once
30
34#include <algorithm>
35#include <utility>
36
37#if !defined(WARN_UNUSED_RETURN)
38#define WARN_UNUSED_RETURN
39#endif
40
41inline void JSRetain(JSClassRef context) { JSClassRetain(context); }
42inline void JSRelease(JSClassRef context) { JSClassRelease(context); }
43inline void JSRetain(JSGlobalContextRef context) { JSGlobalContextRetain(context); }
44inline void JSRelease(JSGlobalContextRef context) { JSGlobalContextRelease(context); }
45inline void JSRetain(JSStringRef string) { JSStringRetain(string); }
46inline void JSRelease(JSStringRef string) { JSStringRelease(string); }
47
48enum AdoptTag { Adopt };
49
50template<typename T> class JSRetainPtr {
51public:
52 JSRetainPtr() = default;
53 JSRetainPtr(T ptr) : m_ptr(ptr) { if (ptr) JSRetain(ptr); }
57
58 T get() const { return m_ptr; }
59
60 void clear();
62
63 T operator->() const { return m_ptr; }
64
65 bool operator!() const { return !m_ptr; }
66 explicit operator bool() const { return m_ptr; }
67
71
72 void swap(JSRetainPtr&);
73
76
77 // FIXME: Make this private once Apple's internal code is updated to not rely on it.
78 // https://bugs.webkit.org/show_bug.cgi?id=189644
80
81private:
82 T m_ptr { nullptr };
83};
84
88
89template<typename T> inline JSRetainPtr<T>::JSRetainPtr(AdoptTag, T ptr)
90 : m_ptr(ptr)
91{
92}
93
98
103
108
109template<typename T> inline JSRetainPtr<T>::JSRetainPtr(const JSRetainPtr& o)
110 : m_ptr(o.m_ptr)
111{
112 if (m_ptr)
113 JSRetain(m_ptr);
114}
115
116template<typename T> inline JSRetainPtr<T>::JSRetainPtr(JSRetainPtr&& o)
117 : m_ptr(o.leakRef())
118{
119}
120
121template<typename T> inline JSRetainPtr<T>::~JSRetainPtr()
122{
123 if (m_ptr)
124 JSRelease(m_ptr);
125}
126
127template<typename T> inline void JSRetainPtr<T>::clear()
128{
129 if (T ptr = leakRef())
130 JSRelease(ptr);
131}
132
133template<typename T> inline T JSRetainPtr<T>::leakRef()
134{
135 return std::exchange(m_ptr, nullptr);
136}
137
138template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<T>& o)
139{
140 return operator=(o.get());
141}
142
143template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(JSRetainPtr&& o)
144{
145 if (T ptr = std::exchange(m_ptr, o.leakRef()))
146 JSRelease(ptr);
147 return *this;
148}
149
150template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(T optr)
151{
152 if (optr)
153 JSRetain(optr);
154 if (T ptr = std::exchange(m_ptr, optr))
155 JSRelease(ptr);
156 return *this;
157}
158
159template<typename T> inline void JSRetainPtr<T>::swap(JSRetainPtr<T>& o)
160{
161 std::swap(m_ptr, o.m_ptr);
162}
163
164template<typename T> inline void swap(JSRetainPtr<T>& a, JSRetainPtr<T>& b)
165{
166 a.swap(b);
167}
168
169template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
170{
171 return a.get() == b.get();
172}
173
174template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, U* b)
175{
176 return a.get() == b;
177}
178
179template<typename T, typename U> inline bool operator==(T* a, const JSRetainPtr<U>& b)
180{
181 return a == b.get();
182}
183
184template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
185{
186 return a.get() != b.get();
187}
188
189template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, U* b)
190{
191 return a.get() != b;
192}
193
194template<typename T, typename U> inline bool operator!=(T* a, const JSRetainPtr<U>& b)
195{
196 return a != b.get();
197}
struct OpaqueJSClass * JSClassRef
Definition JSBase.h:52
struct OpaqueJSString * JSStringRef
Definition JSBase.h:49
struct OpaqueJSContext * JSGlobalContextRef
Definition JSBase.h:46
void JSGlobalContextRelease(JSGlobalContextRef ctx)
Releases a global JavaScript execution context.
JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef ctx)
Retains a global JavaScript execution context.
void JSClassRelease(JSClassRef jsClass)
Releases a JavaScript class.
JSClassRef JSClassRetain(JSClassRef jsClass)
Retains a JavaScript class.
void swap(JSRetainPtr< T > &a, JSRetainPtr< T > &b)
Definition JSRetainPtr.h:164
JSRetainPtr< JSClassRef > adopt(JSClassRef)
Definition JSRetainPtr.h:94
AdoptTag
Definition JSRetainPtr.h:48
@ Adopt
Definition JSRetainPtr.h:48
#define WARN_UNUSED_RETURN
Definition JSRetainPtr.h:38
bool operator==(const JSRetainPtr< T > &a, const JSRetainPtr< U > &b)
Definition JSRetainPtr.h:169
void JSRelease(JSClassRef context)
Definition JSRetainPtr.h:42
bool operator!=(const JSRetainPtr< T > &a, const JSRetainPtr< U > &b)
Definition JSRetainPtr.h:184
void JSRetain(JSClassRef context)
Definition JSRetainPtr.h:41
void JSStringRelease(JSStringRef string)
Releases a JavaScript string.
JSStringRef JSStringRetain(JSStringRef string)
Retains a JavaScript string.
Definition JSRetainPtr.h:50
void swap(JSRetainPtr &)
Definition JSRetainPtr.h:159
JSRetainPtr(T ptr)
Definition JSRetainPtr.h:53
JSRetainPtr & operator=(const JSRetainPtr &)
Definition JSRetainPtr.h:138
friend JSRetainPtr< JSStringRef > adopt(JSStringRef)
Definition JSRetainPtr.h:99
bool operator!() const
Definition JSRetainPtr.h:65
T get() const
Definition JSRetainPtr.h:58
T leakRef() WARN_UNUSED_RETURN
Definition JSRetainPtr.h:133
void clear()
Definition JSRetainPtr.h:127
~JSRetainPtr()
Definition JSRetainPtr.h:121
JSRetainPtr()=default