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)
71
#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
72
template<typename U> WKRetainPtr(WKRetainPtr<U>&& o)
77
WKRetainPtr(WKRetainPtr&& o)
85
if (PtrType ptr = m_ptr)
89
PtrType get() const { return m_ptr; }
106
PtrType operator->() const { return m_ptr; }
107
bool operator!() const { return !m_ptr; }
109
// This conversion operator allows implicit conversion to bool but not to other integer types.
110
typedef PtrType WKRetainPtr::*UnspecifiedBoolType;
111
operator UnspecifiedBoolType() const { return m_ptr ? &WKRetainPtr::m_ptr : 0; }
113
WKRetainPtr& operator=(const WKRetainPtr&);
114
template<typename U> WKRetainPtr& operator=(const WKRetainPtr<U>&);
115
WKRetainPtr& operator=(PtrType);
116
template<typename U> WKRetainPtr& operator=(U*);
118
#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
119
WKRetainPtr& operator=(WKRetainPtr&&);
120
template<typename U> WKRetainPtr& operator=(WKRetainPtr<U>&&);
124
void swap(WKRetainPtr&);
130
template<typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(const WKRetainPtr<T>& o)
132
PtrType optr = o.get();
142
template<typename T> template<typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(const WKRetainPtr<U>& o)
144
PtrType optr = o.get();
154
template<typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(PtrType optr)
165
template<typename T> template<typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(U* optr)
176
#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
177
template<typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(WKRetainPtr<T>&& o)
183
template<typename T> template<typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(WKRetainPtr<U>&& o)
190
template<typename T> inline void WKRetainPtr<T>::adopt(PtrType optr)
198
template<typename T> inline void WKRetainPtr<T>::swap(WKRetainPtr<T>& o)
200
std::swap(m_ptr, o.m_ptr);
203
template<typename T> inline void swap(WKRetainPtr<T>& a, WKRetainPtr<T>& b)
208
template<typename T, typename U> inline bool operator==(const WKRetainPtr<T>& a, const WKRetainPtr<U>& b)
210
return a.get() == b.get();
213
template<typename T, typename U> inline bool operator==(const WKRetainPtr<T>& a, U* b)
218
template<typename T, typename U> inline bool operator==(T* a, const WKRetainPtr<U>& b)
223
template<typename T, typename U> inline bool operator!=(const WKRetainPtr<T>& a, const WKRetainPtr<U>& b)
225
return a.get() != b.get();
228
template<typename T, typename U> inline bool operator!=(const WKRetainPtr<T>& a, U* b)
233
template<typename T, typename U> inline bool operator!=(T* a, const WKRetainPtr<U>& b)
238
template<typename T> inline WKRetainPtr<T> adoptWK(T) WARN_UNUSED_RETURN;
240
template<typename T> inline WKRetainPtr<T> adoptWK(T o)
242
return WKRetainPtr<T>(AdoptWK, o);
245
} // namespace WebKit
247
using WebKit::WKRetainPtr;
248
using WebKit::AdoptWK;
249
using WebKit::adoptWK;
251
#endif // WKRetainPtr_h