initial import
[vuplus_webkit] / Source / JavaScriptCore / API / JSRetainPtr.h
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 Computer, 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
36 inline void JSRetain(JSStringRef string) { JSStringRetain(string); }
37 inline void JSRelease(JSStringRef string) { JSStringRelease(string); }
38 inline void JSRetain(JSGlobalContextRef context) { JSGlobalContextRetain(context); }
39 inline void JSRelease(JSGlobalContextRef context) { JSGlobalContextRelease(context); }
40
41 enum AdoptTag { Adopt };
42
43 template<typename T> class JSRetainPtr {
44 public:
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
74     // FIXME: Remove releaseRef once we change all callers to call leakRef instead.
75     T releaseRef() { return leakRef(); }
76
77 private:
78     T m_ptr;
79 };
80
81 template<typename T> inline JSRetainPtr<T>::JSRetainPtr(const JSRetainPtr& o)
82     : m_ptr(o.m_ptr)
83 {
84     if (m_ptr)
85         JSRetain(m_ptr);
86 }
87
88 template<typename T> template<typename U> inline JSRetainPtr<T>::JSRetainPtr(const JSRetainPtr<U>& o)
89     : m_ptr(o.get())
90 {
91     if (m_ptr)
92         JSRetain(m_ptr);
93 }
94
95 template<typename T> inline JSRetainPtr<T>::~JSRetainPtr()
96 {
97     if (m_ptr)
98         JSRelease(m_ptr);
99 }
100
101 template<typename T> inline void JSRetainPtr<T>::clear()
102 {
103     if (T ptr = m_ptr) {
104         m_ptr = 0;
105         JSRelease(ptr);
106     }
107 }
108
109 template<typename T> inline T JSRetainPtr<T>::leakRef()
110 {
111     T ptr = m_ptr;
112     m_ptr = 0;
113     return ptr;
114 }
115
116 template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<T>& o)
117 {
118     T optr = o.get();
119     if (optr)
120         JSRetain(optr);
121     T ptr = m_ptr;
122     m_ptr = optr;
123     if (ptr)
124         JSRelease(ptr);
125     return *this;
126 }
127
128 template<typename T> template<typename U> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<U>& o)
129 {
130     T optr = o.get();
131     if (optr)
132         JSRetain(optr);
133     T ptr = m_ptr;
134     m_ptr = optr;
135     if (ptr)
136         JSRelease(ptr);
137     return *this;
138 }
139
140 template<typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(T optr)
141 {
142     if (optr)
143         JSRetain(optr);
144     T ptr = m_ptr;
145     m_ptr = optr;
146     if (ptr)
147         JSRelease(ptr);
148     return *this;
149 }
150
151 template<typename T> inline void JSRetainPtr<T>::adopt(T optr)
152 {
153     T ptr = m_ptr;
154     m_ptr = optr;
155     if (ptr)
156         JSRelease(ptr);
157 }
158
159 template<typename T> template<typename U> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(U* optr)
160 {
161     if (optr)
162         JSRetain(optr);
163     T ptr = m_ptr;
164     m_ptr = optr;
165     if (ptr)
166         JSRelease(ptr);
167     return *this;
168 }
169
170 template<typename T> inline void JSRetainPtr<T>::swap(JSRetainPtr<T>& o)
171 {
172     std::swap(m_ptr, o.m_ptr);
173 }
174
175 template<typename T> inline void swap(JSRetainPtr<T>& a, JSRetainPtr<T>& b)
176 {
177     a.swap(b);
178 }
179
180 template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
181
182     return a.get() == b.get(); 
183 }
184
185 template<typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, U* b)
186
187     return a.get() == b; 
188 }
189
190 template<typename T, typename U> inline bool operator==(T* a, const JSRetainPtr<U>& b) 
191 {
192     return a == b.get(); 
193 }
194
195 template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b)
196
197     return a.get() != b.get(); 
198 }
199
200 template<typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, U* b)
201 {
202     return a.get() != b; 
203 }
204
205 template<typename T, typename U> inline bool operator!=(T* a, const JSRetainPtr<U>& b)
206
207     return a != b.get(); 
208 }
209
210
211 #endif // JSRetainPtr_h