]> git.saurik.com Git - apple/javascriptcore.git/blame - API/JSRetainPtr.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / API / JSRetainPtr.h
CommitLineData
b37bf2e1 1/*
14957cd0 2 * Copyright (C) 2005, 2006, 2007, 2010 Apple Inc. All rights reserved.
b37bf2e1
A
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.
81345200 13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
b37bf2e1
A
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#ifndef JSRetainPtr_h
30#define JSRetainPtr_h
31
14957cd0 32#include <JavaScriptCore/JSContextRef.h>
b37bf2e1
A
33#include <JavaScriptCore/JSStringRef.h>
34#include <algorithm>
35
36inline void JSRetain(JSStringRef string) { JSStringRetain(string); }
37inline void JSRelease(JSStringRef string) { JSStringRelease(string); }
14957cd0
A
38inline void JSRetain(JSGlobalContextRef context) { JSGlobalContextRetain(context); }
39inline void JSRelease(JSGlobalContextRef context) { JSGlobalContextRelease(context); }
b37bf2e1
A
40
41enum AdoptTag { Adopt };
42
14957cd0 43template<typename T> class JSRetainPtr {
b37bf2e1 44public:
14957cd0 45 JSRetainPtr() : m_ptr(0) { }
b37bf2e1 46 JSRetainPtr(T ptr) : m_ptr(ptr) { if (ptr) JSRetain(ptr); }
b37bf2e1 47 JSRetainPtr(AdoptTag, T ptr) : m_ptr(ptr) { }
14957cd0
A
48 JSRetainPtr(const JSRetainPtr&);
49 template<typename U> JSRetainPtr(const JSRetainPtr<U>&);
50 ~JSRetainPtr();
b37bf2e1
A
51
52 T get() const { return m_ptr; }
53
14957cd0
A
54 void clear();
55 T leakRef();
56
b37bf2e1
A
57 T operator->() const { return m_ptr; }
58
59 bool operator!() const { return !m_ptr; }
60
61 // This conversion operator allows implicit conversion to bool but not to other integer types.
62 typedef T JSRetainPtr::*UnspecifiedBoolType;
63 operator UnspecifiedBoolType() const { return m_ptr ? &JSRetainPtr::m_ptr : 0; }
64
65 JSRetainPtr& operator=(const JSRetainPtr&);
14957cd0 66 template<typename U> JSRetainPtr& operator=(const JSRetainPtr<U>&);
b37bf2e1 67 JSRetainPtr& operator=(T);
14957cd0 68 template<typename U> JSRetainPtr& operator=(U*);
b37bf2e1
A
69
70 void adopt(T);
71
72 void swap(JSRetainPtr&);
73
74private:
75 T m_ptr;
76};
77
ed1e77d3
A
78inline JSRetainPtr<JSStringRef> adopt(JSStringRef o)
79{
80 return JSRetainPtr<JSStringRef>(Adopt, o);
81}
82
83inline JSRetainPtr<JSGlobalContextRef> adopt(JSGlobalContextRef o)
84{
85 return JSRetainPtr<JSGlobalContextRef>(Adopt, o);
86}
87
14957cd0
A
88template<typename T> inline JSRetainPtr<T>::JSRetainPtr(const JSRetainPtr& o)
89 : m_ptr(o.m_ptr)
90{
91 if (m_ptr)
92 JSRetain(m_ptr);
93}
94
95template<typename T> template<typename U> inline JSRetainPtr<T>::JSRetainPtr(const JSRetainPtr<U>& o)
96 : m_ptr(o.get())
97{
98 if (m_ptr)
99 JSRetain(m_ptr);
100}
101
102template<typename T> inline JSRetainPtr<T>::~JSRetainPtr()
103{
104 if (m_ptr)
105 JSRelease(m_ptr);
106}
107
108template<typename T> inline void JSRetainPtr<T>::clear()
109{
110 if (T ptr = m_ptr) {
111 m_ptr = 0;
112 JSRelease(ptr);
113 }
114}
115
116template<typename T> inline T JSRetainPtr<T>::leakRef()
117{
118 T ptr = m_ptr;
119 m_ptr = 0;
120 return ptr;
121}
122
123template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<T>& o)
b37bf2e1
A
124{
125 T optr = o.get();
126 if (optr)
127 JSRetain(optr);
128 T ptr = m_ptr;
129 m_ptr = optr;
130 if (ptr)
131 JSRelease(ptr);
132 return *this;
133}
134
14957cd0 135template<typename T> template<typename U> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<U>& o)
b37bf2e1
A
136{
137 T optr = o.get();
138 if (optr)
139 JSRetain(optr);
140 T ptr = m_ptr;
141 m_ptr = optr;
142 if (ptr)
143 JSRelease(ptr);
144 return *this;
145}
146
14957cd0 147template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(T optr)
b37bf2e1
A
148{
149 if (optr)
150 JSRetain(optr);
151 T ptr = m_ptr;
152 m_ptr = optr;
153 if (ptr)
154 JSRelease(ptr);
155 return *this;
156}
157
14957cd0 158template<typename T> inline void JSRetainPtr<T>::adopt(T optr)
b37bf2e1
A
159{
160 T ptr = m_ptr;
161 m_ptr = optr;
162 if (ptr)
163 JSRelease(ptr);
164}
165
14957cd0 166template<typename T> template<typename U> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(U* optr)
b37bf2e1
A
167{
168 if (optr)
169 JSRetain(optr);
170 T ptr = m_ptr;
171 m_ptr = optr;
172 if (ptr)
173 JSRelease(ptr);
174 return *this;
175}
176
14957cd0 177template<typename T> inline void JSRetainPtr<T>::swap(JSRetainPtr<T>& o)
b37bf2e1
A
178{
179 std::swap(m_ptr, o.m_ptr);
180}
181
14957cd0 182template<typename T> inline void swap(JSRetainPtr<T>& a, JSRetainPtr<T>& b)
b37bf2e1
A
183{
184 a.swap(b);
185}
186
14957cd0 187template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
b37bf2e1
A
188{
189 return a.get() == b.get();
190}
191
14957cd0 192template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, U* b)
b37bf2e1
A
193{
194 return a.get() == b;
195}
196
14957cd0 197template<typename T, typename U> inline bool operator==(T* a, const JSRetainPtr<U>& b)
b37bf2e1
A
198{
199 return a == b.get();
200}
201
14957cd0 202template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
b37bf2e1
A
203{
204 return a.get() != b.get();
205}
206
14957cd0 207template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, U* b)
b37bf2e1
A
208{
209 return a.get() != b;
210}
211
14957cd0 212template<typename T, typename U> inline bool operator!=(T* a, const JSRetainPtr<U>& b)
b37bf2e1
A
213{
214 return a != b.get();
215}
216
217
218#endif // JSRetainPtr_h