]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/OwnPtr.h
f80ec54ddc2cdceefcf0df705e6b44872596c483
[apple/javascriptcore.git] / wtf / OwnPtr.h
1 /*
2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21 #ifndef WTF_OwnPtr_h
22 #define WTF_OwnPtr_h
23
24 #include "Assertions.h"
25 #include "NullPtr.h"
26 #include "OwnPtrCommon.h"
27 #include "TypeTraits.h"
28 #include <algorithm>
29 #include <memory>
30
31 namespace WTF {
32
33 // Unlike most of our smart pointers, OwnPtr can take either the pointer type or the pointed-to type.
34
35 template<typename T> class PassOwnPtr;
36 template<typename T> PassOwnPtr<T> adoptPtr(T*);
37
38 template<typename T> class OwnPtr {
39 public:
40 typedef typename RemovePointer<T>::Type ValueType;
41 typedef ValueType* PtrType;
42
43 OwnPtr() : m_ptr(0) { }
44
45 // See comment in PassOwnPtr.h for why this takes a const reference.
46 template<typename U> OwnPtr(const PassOwnPtr<U>& o);
47
48 // This copy constructor is used implicitly by gcc when it generates
49 // transients for assigning a PassOwnPtr<T> object to a stack-allocated
50 // OwnPtr<T> object. It should never be called explicitly and gcc
51 // should optimize away the constructor when generating code.
52 OwnPtr(const OwnPtr<ValueType>&);
53
54 ~OwnPtr() { deleteOwnedPtr(m_ptr); }
55
56 PtrType get() const { return m_ptr; }
57
58 void clear();
59 PassOwnPtr<T> release();
60 PtrType leakPtr() WARN_UNUSED_RETURN;
61
62 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
63 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
64
65 bool operator!() const { return !m_ptr; }
66
67 // This conversion operator allows implicit conversion to bool but not to other integer types.
68 typedef PtrType OwnPtr::*UnspecifiedBoolType;
69 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0; }
70
71 OwnPtr& operator=(const PassOwnPtr<T>&);
72 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; }
73 template<typename U> OwnPtr& operator=(const PassOwnPtr<U>&);
74
75 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
76
77 #ifdef LOOSE_OWN_PTR
78 explicit OwnPtr(PtrType ptr) : m_ptr(ptr) { }
79 void set(PtrType);
80 #endif
81
82 private:
83 OwnPtr& operator=(const OwnPtr<T>&);
84
85 // We should never have two OwnPtrs for the same underlying object (otherwise we'll get
86 // double-destruction), so these equality operators should never be needed.
87 template<typename U> bool operator==(const OwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
88 template<typename U> bool operator!=(const OwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
89 template<typename U> bool operator==(const PassOwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
90 template<typename U> bool operator!=(const PassOwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
91
92 PtrType m_ptr;
93 };
94
95 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<U>& o)
96 : m_ptr(o.leakPtr())
97 {
98 }
99
100 template<typename T> inline void OwnPtr<T>::clear()
101 {
102 PtrType ptr = m_ptr;
103 m_ptr = 0;
104 deleteOwnedPtr(ptr);
105 }
106
107 template<typename T> inline PassOwnPtr<T> OwnPtr<T>::release()
108 {
109 PtrType ptr = m_ptr;
110 m_ptr = 0;
111 return adoptPtr(ptr);
112 }
113
114 template<typename T> inline typename OwnPtr<T>::PtrType OwnPtr<T>::leakPtr()
115 {
116 PtrType ptr = m_ptr;
117 m_ptr = 0;
118 return ptr;
119 }
120
121 #ifdef LOOSE_OWN_PTR
122 template<typename T> inline void OwnPtr<T>::set(PtrType ptr)
123 {
124 ASSERT(!ptr || m_ptr != ptr);
125 PtrType oldPtr = m_ptr;
126 m_ptr = ptr;
127 deleteOwnedPtr(oldPtr);
128 }
129 #endif
130
131 template<typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<T>& o)
132 {
133 PtrType ptr = m_ptr;
134 m_ptr = o.leakPtr();
135 ASSERT(!ptr || m_ptr != ptr);
136 deleteOwnedPtr(ptr);
137 return *this;
138 }
139
140 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<U>& o)
141 {
142 PtrType ptr = m_ptr;
143 m_ptr = o.leakPtr();
144 ASSERT(!ptr || m_ptr != ptr);
145 deleteOwnedPtr(ptr);
146 return *this;
147 }
148
149 template<typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b)
150 {
151 a.swap(b);
152 }
153
154 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a, U* b)
155 {
156 return a.get() == b;
157 }
158
159 template<typename T, typename U> inline bool operator==(T* a, const OwnPtr<U>& b)
160 {
161 return a == b.get();
162 }
163
164 template<typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, U* b)
165 {
166 return a.get() != b;
167 }
168
169 template<typename T, typename U> inline bool operator!=(T* a, const OwnPtr<U>& b)
170 {
171 return a != b.get();
172 }
173
174 template<typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>& p)
175 {
176 return p.get();
177 }
178
179 } // namespace WTF
180
181 using WTF::OwnPtr;
182
183 #endif // WTF_OwnPtr_h