OLD | NEW |
| (Empty) |
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 <wtf/Assertions.h> | |
30 #include <wtf/NullPtr.h> | |
31 #include <wtf/OwnPtrCommon.h> | |
32 #include <wtf/TypeTraits.h> | |
33 | |
34 namespace WTF { | |
35 | |
36 // Unlike most of our smart pointers, PassOwnPtr can take either the pointer
type or the pointed-to type. | |
37 | |
38 template<typename T> class OwnPtr; | |
39 template<typename T> class PassOwnPtr; | |
40 template<typename T> PassOwnPtr<T> adoptPtr(T*); | |
41 | |
42 template<typename T> class PassOwnPtr { | |
43 public: | |
44 typedef typename RemovePointer<T>::Type ValueType; | |
45 typedef ValueType* PtrType; | |
46 | |
47 PassOwnPtr() : m_ptr(0) { } | |
48 PassOwnPtr(std::nullptr_t) : m_ptr(0) { } | |
49 | |
50 // It somewhat breaks the type system to allow transfer of ownership out
of | |
51 // a const PassOwnPtr. However, it makes it much easier to work with Pas
sOwnPtr | |
52 // temporaries, and we don't have a need to use real const PassOwnPtrs a
nyway. | |
53 PassOwnPtr(const PassOwnPtr& o) : m_ptr(o.leakPtr()) { } | |
54 template<typename U> PassOwnPtr(const PassOwnPtr<U>& o) : m_ptr(o.leakPt
r()) { } | |
55 | |
56 ~PassOwnPtr() { deleteOwnedPtr(m_ptr); } | |
57 | |
58 PtrType get() const { return m_ptr; } | |
59 | |
60 PtrType leakPtr() const 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 t
o other integer types. | |
68 typedef PtrType PassOwnPtr::*UnspecifiedBoolType; | |
69 operator UnspecifiedBoolType() const { return m_ptr ? &PassOwnPtr::m_ptr
: 0; } | |
70 | |
71 PassOwnPtr& operator=(const PassOwnPtr&) { COMPILE_ASSERT(!sizeof(T*), P
assOwnPtr_should_never_be_assigned_to); return *this; } | |
72 | |
73 template<typename U> friend PassOwnPtr<U> adoptPtr(U*); | |
74 | |
75 private: | |
76 explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) { } | |
77 | |
78 // We should never have two OwnPtrs for the same underlying object (othe
rwise we'll get | |
79 // double-destruction), so these equality operators should never be need
ed. | |
80 template<typename U> bool operator==(const PassOwnPtr<U>&) { COMPILE_ASS
ERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | |
81 template<typename U> bool operator!=(const PassOwnPtr<U>&) { COMPILE_ASS
ERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | |
82 template<typename U> bool operator==(const OwnPtr<U>&) { COMPILE_ASSERT(
!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | |
83 template<typename U> bool operator!=(const OwnPtr<U>&) { COMPILE_ASSERT(
!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | |
84 | |
85 mutable PtrType m_ptr; | |
86 }; | |
87 | |
88 template<typename T> inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::l
eakPtr() const | |
89 { | |
90 PtrType ptr = m_ptr; | |
91 m_ptr = 0; | |
92 return ptr; | |
93 } | |
94 | |
95 template<typename T, typename U> inline bool operator==(const PassOwnPtr<T>&
a, const PassOwnPtr<U>& b) | |
96 { | |
97 return a.get() == b.get(); | |
98 } | |
99 | |
100 template<typename T, typename U> inline bool operator==(const PassOwnPtr<T>&
a, const OwnPtr<U>& b) | |
101 { | |
102 return a.get() == b.get(); | |
103 } | |
104 | |
105 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a,
const PassOwnPtr<U>& b) | |
106 { | |
107 return a.get() == b.get(); | |
108 } | |
109 | |
110 template<typename T, typename U> inline bool operator==(const PassOwnPtr<T>&
a, U* b) | |
111 { | |
112 return a.get() == b; | |
113 } | |
114 | |
115 template<typename T, typename U> inline bool operator==(T* a, const PassOwnP
tr<U>& b) | |
116 { | |
117 return a == b.get(); | |
118 } | |
119 | |
120 template<typename T, typename U> inline bool operator!=(const PassOwnPtr<T>&
a, const PassOwnPtr<U>& b) | |
121 { | |
122 return a.get() != b.get(); | |
123 } | |
124 | |
125 template<typename T, typename U> inline bool operator!=(const PassOwnPtr<T>&
a, const OwnPtr<U>& b) | |
126 { | |
127 return a.get() != b.get(); | |
128 } | |
129 | |
130 template<typename T, typename U> inline bool operator!=(const OwnPtr<T>& a,
const PassOwnPtr<U>& b) | |
131 { | |
132 return a.get() != b.get(); | |
133 } | |
134 | |
135 template<typename T, typename U> inline bool operator!=(const PassOwnPtr<T>&
a, U* b) | |
136 { | |
137 return a.get() != b; | |
138 } | |
139 | |
140 template<typename T, typename U> inline bool operator!=(T* a, const PassOwnP
tr<U>& b) | |
141 { | |
142 return a != b.get(); | |
143 } | |
144 | |
145 template<typename T> inline PassOwnPtr<T> adoptPtr(T* ptr) | |
146 { | |
147 return PassOwnPtr<T>(ptr); | |
148 } | |
149 | |
150 template<typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(co
nst PassOwnPtr<U>& p) | |
151 { | |
152 return adoptPtr(static_cast<T*>(p.leakPtr())); | |
153 } | |
154 | |
155 template<typename T, typename U> inline PassOwnPtr<T> const_pointer_cast(con
st PassOwnPtr<U>& p) | |
156 { | |
157 return adoptPtr(const_cast<T*>(p.leakPtr())); | |
158 } | |
159 | |
160 template<typename T> inline T* getPtr(const PassOwnPtr<T>& p) | |
161 { | |
162 return p.get(); | |
163 } | |
164 | |
165 } // namespace WTF | |
166 | |
167 using WTF::PassOwnPtr; | |
168 using WTF::adoptPtr; | |
169 using WTF::const_pointer_cast; | |
170 using WTF::static_pointer_cast; | |
171 | |
172 #endif // WTF_PassOwnPtr_h | |
OLD | NEW |