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 <WebKit2/WKType.h>
34 enum WKAdoptTag { AdoptWK };
36 template<typename T> class WKRetainPtr {
45 WKRetainPtr(PtrType ptr)
52 WKRetainPtr(WKAdoptTag, PtrType ptr)
57 template<typename U> WKRetainPtr(const WKRetainPtr<U>& o)
60 if (PtrType ptr = m_ptr)
64 WKRetainPtr(const WKRetainPtr& o)
67 if (PtrType ptr = m_ptr)
73 if (PtrType ptr = m_ptr)
77 PtrType get() const { return m_ptr; }
94 PtrType operator->() const { return m_ptr; }
95 bool operator!() const { return !m_ptr; }
97 // This conversion operator allows implicit conversion to bool but not to other integer types.
98 typedef PtrType WKRetainPtr::*UnspecifiedBoolType;
99 operator UnspecifiedBoolType() const { return m_ptr ? &WKRetainPtr::m_ptr : 0; }
101 WKRetainPtr& operator=(const WKRetainPtr&);
102 template<typename U> WKRetainPtr& operator=(const WKRetainPtr<U>&);
103 WKRetainPtr& operator=(PtrType);
104 template<typename U> WKRetainPtr& operator=(U*);
107 void swap(WKRetainPtr&);
113 template<typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(const WKRetainPtr<T>& o)
115 PtrType optr = o.get();
125 template<typename T> template<typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(const WKRetainPtr<U>& o)
127 PtrType optr = o.get();
137 template<typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(PtrType optr)
148 template<typename T> inline void WKRetainPtr<T>::adopt(PtrType optr)
156 template<typename T> template<typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(U* optr)
167 template<typename T> inline void WKRetainPtr<T>::swap(WKRetainPtr<T>& o)
169 std::swap(m_ptr, o.m_ptr);
172 template<typename T> inline void swap(WKRetainPtr<T>& a, WKRetainPtr<T>& b)
177 template<typename T, typename U> inline bool operator==(const WKRetainPtr<T>& a, const WKRetainPtr<U>& b)
179 return a.get() == b.get();
182 template<typename T, typename U> inline bool operator==(const WKRetainPtr<T>& a, U* b)
187 template<typename T, typename U> inline bool operator==(T* a, const WKRetainPtr<U>& b)
192 template<typename T, typename U> inline bool operator!=(const WKRetainPtr<T>& a, const WKRetainPtr<U>& b)
194 return a.get() != b.get();
197 template<typename T, typename U> inline bool operator!=(const WKRetainPtr<T>& a, U* b)
202 template<typename T, typename U> inline bool operator!=(T* a, const WKRetainPtr<U>& b)
207 template<typename T> static inline WKRetainPtr<T> adoptWK(T o)
209 return WKRetainPtr<T>(AdoptWK, o);
212 } // namespace WebKit
214 using WebKit::WKRetainPtr;
215 using WebKit::AdoptWK;
216 using WebKit::adoptWK;
218 #endif // WKRetainPtr_h