]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/PassOwnPtr.h
JavaScriptCore-903.5.tar.gz
[apple/javascriptcore.git] / wtf / PassOwnPtr.h
1 /*
2 * Copyright (C) 2009, 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef WTF_PassOwnPtr_h
27 #define WTF_PassOwnPtr_h
28
29 #include "Assertions.h"
30 #include "NullPtr.h"
31 #include "OwnPtrCommon.h"
32 #include "TypeTraits.h"
33
34 #if !PLATFORM(CHROMIUM) && !PLATFORM(WIN) && !PLATFORM(MAC) && !PLATFORM(QT)
35 // Remove this once we make all WebKit code compatible with stricter rules about PassOwnPtr.
36 #define LOOSE_PASS_OWN_PTR
37 #endif
38
39 namespace WTF {
40
41 // Unlike most of our smart pointers, PassOwnPtr can take either the pointer type or the pointed-to type.
42
43 template<typename T> class OwnPtr;
44 template<typename T> class PassOwnPtr;
45 template<typename T> PassOwnPtr<T> adoptPtr(T*);
46
47 template<typename T> class PassOwnPtr {
48 public:
49 typedef typename RemovePointer<T>::Type ValueType;
50 typedef ValueType* PtrType;
51
52 PassOwnPtr() : m_ptr(0) { }
53 PassOwnPtr(std::nullptr_t) : m_ptr(0) { }
54
55 // It somewhat breaks the type system to allow transfer of ownership out of
56 // a const PassOwnPtr. However, it makes it much easier to work with PassOwnPtr
57 // temporaries, and we don't have a need to use real const PassOwnPtrs anyway.
58 PassOwnPtr(const PassOwnPtr& o) : m_ptr(o.leakPtr()) { }
59 template<typename U> PassOwnPtr(const PassOwnPtr<U>& o) : m_ptr(o.leakPtr()) { }
60
61 ~PassOwnPtr() { deleteOwnedPtr(m_ptr); }
62
63 PtrType get() const { return m_ptr; }
64
65 void clear();
66 PtrType leakPtr() const WARN_UNUSED_RETURN;
67
68 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
69 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
70
71 bool operator!() const { return !m_ptr; }
72
73 // This conversion operator allows implicit conversion to bool but not to other integer types.
74 typedef PtrType PassOwnPtr::*UnspecifiedBoolType;
75 operator UnspecifiedBoolType() const { return m_ptr ? &PassOwnPtr::m_ptr : 0; }
76
77 PassOwnPtr& operator=(const PassOwnPtr<T>&);
78 #if !defined(LOOSE_PASS_OWN_PTR) || !HAVE(NULLPTR)
79 PassOwnPtr& operator=(std::nullptr_t) { clear(); return *this; }
80 #endif
81 template<typename U> PassOwnPtr& operator=(const PassOwnPtr<U>&);
82
83 template<typename U> friend PassOwnPtr<U> adoptPtr(U*);
84
85 #ifdef LOOSE_PASS_OWN_PTR
86 PassOwnPtr(PtrType ptr) : m_ptr(ptr) { }
87 PassOwnPtr& operator=(PtrType);
88 #endif
89
90 private:
91 #ifndef LOOSE_PASS_OWN_PTR
92 explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) { }
93 #endif
94
95 // We should never have two OwnPtrs for the same underlying object (otherwise we'll get
96 // double-destruction), so these equality operators should never be needed.
97 template<typename U> bool operator==(const PassOwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
98 template<typename U> bool operator!=(const PassOwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
99 template<typename U> bool operator==(const OwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
100 template<typename U> bool operator!=(const OwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
101
102 mutable PtrType m_ptr;
103 };
104
105 template<typename T> inline void PassOwnPtr<T>::clear()
106 {
107 PtrType ptr = m_ptr;
108 m_ptr = 0;
109 deleteOwnedPtr(ptr);
110 }
111
112 template<typename T> inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leakPtr() const
113 {
114 PtrType ptr = m_ptr;
115 m_ptr = 0;
116 return ptr;
117 }
118
119 #ifdef LOOSE_PASS_OWN_PTR
120 template<typename T> inline PassOwnPtr<T>& PassOwnPtr<T>::operator=(PtrType optr)
121 {
122 PtrType ptr = m_ptr;
123 m_ptr = optr;
124 ASSERT(!ptr || m_ptr != ptr);
125 if (ptr)
126 deleteOwnedPtr(ptr);
127 return *this;
128 }
129 #endif
130
131 template<typename T> inline PassOwnPtr<T>& PassOwnPtr<T>::operator=(const PassOwnPtr<T>& optr)
132 {
133 PtrType ptr = m_ptr;
134 m_ptr = optr.leakPtr();
135 ASSERT(!ptr || m_ptr != ptr);
136 if (ptr)
137 deleteOwnedPtr(ptr);
138 return *this;
139 }
140
141 template<typename T> template<typename U> inline PassOwnPtr<T>& PassOwnPtr<T>::operator=(const PassOwnPtr<U>& optr)
142 {
143 PtrType ptr = m_ptr;
144 m_ptr = optr.leakPtr();
145 ASSERT(!ptr || m_ptr != ptr);
146 if (ptr)
147 deleteOwnedPtr(ptr);
148 return *this;
149 }
150
151 template<typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a, const PassOwnPtr<U>& b)
152 {
153 return a.get() == b.get();
154 }
155
156 template<typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a, const OwnPtr<U>& b)
157 {
158 return a.get() == b.get();
159 }
160
161 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a, const PassOwnPtr<U>& b)
162 {
163 return a.get() == b.get();
164 }
165
166 template<typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a, U* b)
167 {
168 return a.get() == b;
169 }
170
171 template<typename T, typename U> inline bool operator==(T* a, const PassOwnPtr<U>& b)
172 {
173 return a == b.get();
174 }
175
176 template<typename T, typename U> inline bool operator!=(const PassOwnPtr<T>& a, const PassOwnPtr<U>& b)
177 {
178 return a.get() != b.get();
179 }
180
181 template<typename T, typename U> inline bool operator!=(const PassOwnPtr<T>& a, const OwnPtr<U>& b)
182 {
183 return a.get() != b.get();
184 }
185
186 template<typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, const PassOwnPtr<U>& b)
187 {
188 return a.get() != b.get();
189 }
190
191 template<typename T, typename U> inline bool operator!=(const PassOwnPtr<T>& a, U* b)
192 {
193 return a.get() != b;
194 }
195
196 template<typename T, typename U> inline bool operator!=(T* a, const PassOwnPtr<U>& b)
197 {
198 return a != b.get();
199 }
200
201 template<typename T> inline PassOwnPtr<T> adoptPtr(T* ptr)
202 {
203 return PassOwnPtr<T>(ptr);
204 }
205
206 template<typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(const PassOwnPtr<U>& p)
207 {
208 return adoptPtr(static_cast<T*>(p.leakPtr()));
209 }
210
211 template<typename T, typename U> inline PassOwnPtr<T> const_pointer_cast(const PassOwnPtr<U>& p)
212 {
213 return adoptPtr(const_cast<T*>(p.leakPtr()));
214 }
215
216 template<typename T> inline T* getPtr(const PassOwnPtr<T>& p)
217 {
218 return p.get();
219 }
220
221 } // namespace WTF
222
223 using WTF::PassOwnPtr;
224 using WTF::adoptPtr;
225 using WTF::const_pointer_cast;
226 using WTF::static_pointer_cast;
227
228 #endif // WTF_PassOwnPtr_h