2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
29 #include <WebKit/WKType.h>
31 #include <wtf/GetPtr.h>
32 #include <wtf/HashFunctions.h>
33 #include <wtf/HashTraits.h>
34 #include <wtf/RefPtr.h>
38 enum WKAdoptTag { AdoptWK };
40 template<typename T> class WKRetainPtr {
49 WKRetainPtr(PtrType ptr)
56 WKRetainPtr(WKAdoptTag, PtrType ptr)
61 template<typename U> WKRetainPtr(const WKRetainPtr<U>& o)
64 if (PtrType ptr = m_ptr)
68 WKRetainPtr(const WKRetainPtr& o)
71 if (PtrType ptr = m_ptr)
75 template<typename U> WKRetainPtr(WKRetainPtr<U>&& o)
80 WKRetainPtr(WKRetainPtr&& o)
87 if (PtrType ptr = m_ptr)
91 // Hash table deleted values, which are only constructed and never copied or destroyed.
92 WKRetainPtr(WTF::HashTableDeletedValueType)
93 : m_ptr(hashTableDeletedValue())
97 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
98 constexpr static T hashTableDeletedValue() { return reinterpret_cast<T>(-1); }
100 PtrType get() const { return m_ptr; }
117 PtrType operator->() const { return m_ptr; }
118 bool operator!() const { return !m_ptr; }
120 // This conversion operator allows implicit conversion to bool but not to other integer types.
121 typedef PtrType WKRetainPtr::*UnspecifiedBoolType;
122 operator UnspecifiedBoolType() const { return m_ptr ? &WKRetainPtr::m_ptr : 0; }
124 WKRetainPtr& operator=(const WKRetainPtr&);
125 template<typename U> WKRetainPtr& operator=(const WKRetainPtr<U>&);
126 WKRetainPtr& operator=(PtrType);
127 template<typename U> WKRetainPtr& operator=(U*);
129 WKRetainPtr& operator=(WKRetainPtr&&);
130 template<typename U> WKRetainPtr& operator=(WKRetainPtr<U>&&);
133 void swap(WKRetainPtr&);
139 template<typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(const WKRetainPtr<T>& o)
141 PtrType optr = o.get();
151 template<typename T> template<typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(const WKRetainPtr<U>& o)
153 PtrType optr = o.get();
163 template<typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(PtrType optr)
174 template<typename T> template<typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(U* optr)
185 template<typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(WKRetainPtr<T>&& o)
191 template<typename T> template<typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(WKRetainPtr<U>&& o)
197 template<typename T> inline void WKRetainPtr<T>::adopt(PtrType optr)
205 template<typename T> inline void WKRetainPtr<T>::swap(WKRetainPtr<T>& o)
207 std::swap(m_ptr, o.m_ptr);
210 template<typename T> inline void swap(WKRetainPtr<T>& a, WKRetainPtr<T>& b)
215 template<typename T, typename U> inline bool operator==(const WKRetainPtr<T>& a, const WKRetainPtr<U>& b)
217 return a.get() == b.get();
220 template<typename T, typename U> inline bool operator==(const WKRetainPtr<T>& a, U* b)
225 template<typename T, typename U> inline bool operator==(T* a, const WKRetainPtr<U>& b)
230 template<typename T, typename U> inline bool operator!=(const WKRetainPtr<T>& a, const WKRetainPtr<U>& b)
232 return a.get() != b.get();
235 template<typename T, typename U> inline bool operator!=(const WKRetainPtr<T>& a, U* b)
240 template<typename T, typename U> inline bool operator!=(T* a, const WKRetainPtr<U>& b)
245 #if defined(__GNUC__) && !(defined(__CC_ARM) || defined(__ARMCC__))
246 #define WK_WARN_UNUSED_RETURN __attribute__((warn_unused_result))
248 #define WK_WARN_UNUSED_RETURN
251 template<typename T> inline WKRetainPtr<T> adoptWK(T) WK_WARN_UNUSED_RETURN;
253 #undef WK_WARN_UNUSED_RETURN
255 template<typename T> inline WKRetainPtr<T> adoptWK(T o)
257 return WKRetainPtr<T>(AdoptWK, o);
260 } // namespace WebKit
262 using WebKit::WKRetainPtr;
263 using WebKit::AdoptWK;
264 using WebKit::adoptWK;
268 template <typename T> struct IsSmartPtr<WKRetainPtr<T>> {
269 static const bool value = true;
272 template<typename P> struct DefaultHash<WKRetainPtr<P>> {
273 typedef PtrHash<WKRetainPtr<P>> Hash;
276 template<typename P> struct HashTraits<WKRetainPtr<P>> : SimpleClassHashTraits<WKRetainPtr<P>> {
277 static P emptyValue() { return nullptr; }
280 static PeekType peek(const WKRetainPtr<P>& value) { return value.get(); }
281 static PeekType peek(P value) { return value; }
286 #endif // WKRetainPtr_h