]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - API/JSRetainPtr.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / API / JSRetainPtr.h
... / ...
CommitLineData
1/*
2 * Copyright (C) 2005, 2006, 2007, 2010 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#ifndef JSRetainPtr_h
30#define JSRetainPtr_h
31
32#include <JavaScriptCore/JSContextRef.h>
33#include <JavaScriptCore/JSStringRef.h>
34#include <algorithm>
35
36inline void JSRetain(JSStringRef string) { JSStringRetain(string); }
37inline void JSRelease(JSStringRef string) { JSStringRelease(string); }
38inline void JSRetain(JSGlobalContextRef context) { JSGlobalContextRetain(context); }
39inline void JSRelease(JSGlobalContextRef context) { JSGlobalContextRelease(context); }
40
41enum AdoptTag { Adopt };
42
43template<typename T> class JSRetainPtr {
44public:
45 JSRetainPtr() : m_ptr(0) { }
46 JSRetainPtr(T ptr) : m_ptr(ptr) { if (ptr) JSRetain(ptr); }
47 JSRetainPtr(AdoptTag, T ptr) : m_ptr(ptr) { }
48 JSRetainPtr(const JSRetainPtr&);
49 template<typename U> JSRetainPtr(const JSRetainPtr<U>&);
50 ~JSRetainPtr();
51
52 T get() const { return m_ptr; }
53
54 void clear();
55 T leakRef();
56
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&);
66 template<typename U> JSRetainPtr& operator=(const JSRetainPtr<U>&);
67 JSRetainPtr& operator=(T);
68 template<typename U> JSRetainPtr& operator=(U*);
69
70 void adopt(T);
71
72 void swap(JSRetainPtr&);
73
74private:
75 T m_ptr;
76};
77
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
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)
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
135template<typename T> template<typename U> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<U>& o)
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
147template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(T optr)
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
158template<typename T> inline void JSRetainPtr<T>::adopt(T optr)
159{
160 T ptr = m_ptr;
161 m_ptr = optr;
162 if (ptr)
163 JSRelease(ptr);
164}
165
166template<typename T> template<typename U> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(U* optr)
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
177template<typename T> inline void JSRetainPtr<T>::swap(JSRetainPtr<T>& o)
178{
179 std::swap(m_ptr, o.m_ptr);
180}
181
182template<typename T> inline void swap(JSRetainPtr<T>& a, JSRetainPtr<T>& b)
183{
184 a.swap(b);
185}
186
187template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
188{
189 return a.get() == b.get();
190}
191
192template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, U* b)
193{
194 return a.get() == b;
195}
196
197template<typename T, typename U> inline bool operator==(T* a, const JSRetainPtr<U>& b)
198{
199 return a == b.get();
200}
201
202template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
203{
204 return a.get() != b.get();
205}
206
207template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, U* b)
208{
209 return a.get() != b;
210}
211
212template<typename T, typename U> inline bool operator!=(T* a, const JSRetainPtr<U>& b)
213{
214 return a != b.get();
215}
216
217
218#endif // JSRetainPtr_h