Ultralight C++ API 1.3.0
Loading...
Searching...
No Matches
JSRetainPtr.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2005-2018 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
33#include <algorithm>
34
35#if !defined(WARN_UNUSED_RETURN)
36#define WARN_UNUSED_RETURN
37#endif
38
39inline void JSRetain(JSStringRef string) { JSStringRetain(string); }
40inline void JSRelease(JSStringRef string) { JSStringRelease(string); }
41inline void JSRetain(JSGlobalContextRef context) { JSGlobalContextRetain(context); }
42inline void JSRelease(JSGlobalContextRef context) { JSGlobalContextRelease(context); }
43
44enum AdoptTag { Adopt };
45
46template<typename T> class JSRetainPtr {
47public:
48 JSRetainPtr() = default;
49 JSRetainPtr(T ptr) : m_ptr(ptr) { if (ptr) JSRetain(ptr); }
53
54 T get() const { return m_ptr; }
55
56 void clear();
58
59 T operator->() const { return m_ptr; }
60
61 bool operator!() const { return !m_ptr; }
62 explicit operator bool() const { return m_ptr; }
63
67
68 void swap(JSRetainPtr&);
69
72
73 // FIXME: Make this private once Apple's internal code is updated to not rely on it.
74 // https://bugs.webkit.org/show_bug.cgi?id=189644
76
77private:
78 T m_ptr { nullptr };
79};
80
83
84template<typename T> inline JSRetainPtr<T>::JSRetainPtr(AdoptTag, T ptr)
85 : m_ptr(ptr)
86{
87}
88
90{
92}
93
95{
97}
98
99template<typename T> inline JSRetainPtr<T>::JSRetainPtr(const JSRetainPtr& o)
100 : m_ptr(o.m_ptr)
101{
102 if (m_ptr)
103 JSRetain(m_ptr);
104}
105
106template<typename T> inline JSRetainPtr<T>::JSRetainPtr(JSRetainPtr&& o)
107 : m_ptr(o.leakRef())
108{
109}
110
111template<typename T> inline JSRetainPtr<T>::~JSRetainPtr()
112{
113 if (m_ptr)
114 JSRelease(m_ptr);
115}
116
117template<typename T> inline void JSRetainPtr<T>::clear()
118{
119 if (T ptr = leakRef())
120 JSRelease(ptr);
121}
122
123template<typename T> inline T JSRetainPtr<T>::leakRef()
124{
125 return std::exchange(m_ptr, nullptr);
126}
127
128template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<T>& o)
129{
130 return operator=(o.get());
131}
132
133template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(JSRetainPtr&& o)
134{
135 if (T ptr = std::exchange(m_ptr, o.leakRef()))
136 JSRelease(ptr);
137 return *this;
138}
139
140template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(T optr)
141{
142 if (optr)
143 JSRetain(optr);
144 if (T ptr = std::exchange(m_ptr, optr))
145 JSRelease(ptr);
146 return *this;
147}
148
149template<typename T> inline void JSRetainPtr<T>::swap(JSRetainPtr<T>& o)
150{
151 std::swap(m_ptr, o.m_ptr);
152}
153
154template<typename T> inline void swap(JSRetainPtr<T>& a, JSRetainPtr<T>& b)
155{
156 a.swap(b);
157}
158
159template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
160{
161 return a.get() == b.get();
162}
163
164template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, U* b)
165{
166 return a.get() == b;
167}
168
169template<typename T, typename U> inline bool operator==(T* a, const JSRetainPtr<U>& b)
170{
171 return a == b.get();
172}
173
174template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
175{
176 return a.get() != b.get();
177}
178
179template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, U* b)
180{
181 return a.get() != b;
182}
183
184template<typename T, typename U> inline bool operator!=(T* a, const JSRetainPtr<U>& b)
185{
186 return a != b.get();
187}
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.
JSRetainPtr< JSStringRef > adopt(JSStringRef)
Definition JSRetainPtr.h:89
void JSRelease(JSStringRef string)
Definition JSRetainPtr.h:40
void swap(JSRetainPtr< T > &a, JSRetainPtr< T > &b)
Definition JSRetainPtr.h:154
AdoptTag
Definition JSRetainPtr.h:44
@ Adopt
Definition JSRetainPtr.h:44
#define WARN_UNUSED_RETURN
Definition JSRetainPtr.h:36
void JSRetain(JSStringRef string)
Definition JSRetainPtr.h:39
bool operator==(const JSRetainPtr< T > &a, const JSRetainPtr< U > &b)
Definition JSRetainPtr.h:159
bool operator!=(const JSRetainPtr< T > &a, const JSRetainPtr< U > &b)
Definition JSRetainPtr.h:174
void JSStringRelease(JSStringRef string)
Releases a JavaScript string.
JSStringRef JSStringRetain(JSStringRef string)
Retains a JavaScript string.
Definition JSRetainPtr.h:46
void swap(JSRetainPtr &)
Definition JSRetainPtr.h:149
friend JSRetainPtr< JSStringRef > adopt(JSStringRef)
Definition JSRetainPtr.h:89
JSRetainPtr & operator=(const JSRetainPtr &)
Definition JSRetainPtr.h:128
JSRetainPtr(T ptr)
Definition JSRetainPtr.h:49
bool operator!() const
Definition JSRetainPtr.h:61
T leakRef() WARN_UNUSED_RETURN
Definition JSRetainPtr.h:123
JSRetainPtr()=default
T get() const
Definition JSRetainPtr.h:54
void clear()
Definition JSRetainPtr.h:117
~JSRetainPtr()
Definition JSRetainPtr.h:111