2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
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.
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.
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.
24 #include <wtf/Assertions.h>
25 #include <wtf/NullPtr.h>
26 #include <wtf/OwnPtrCommon.h>
27 #include <wtf/TypeTraits.h>
33 // Unlike most of our smart pointers, OwnPtr can take either the pointer type or the pointed-to type.
35 template<typename T> class PassOwnPtr;
36 template<typename T> PassOwnPtr<T> adoptPtr(T*);
38 template<typename T> class OwnPtr {
40 typedef typename RemovePointer<T>::Type ValueType;
41 typedef ValueType* PtrType;
43 OwnPtr() : m_ptr(0) { }
44 OwnPtr(std::nullptr_t) : m_ptr(0) { }
46 // See comment in PassOwnPtr.h for why this takes a const reference.
47 template<typename U> OwnPtr(const PassOwnPtr<U>& o);
49 // This copy constructor is used implicitly by gcc when it generates
50 // transients for assigning a PassOwnPtr<T> object to a stack-allocated
51 // OwnPtr<T> object. It should never be called explicitly and gcc
52 // should optimize away the constructor when generating code.
53 OwnPtr(const OwnPtr<ValueType>&);
55 ~OwnPtr() { deleteOwnedPtr(m_ptr); }
57 PtrType get() const { return m_ptr; }
60 PassOwnPtr<T> release();
61 PtrType leakPtr() WARN_UNUSED_RETURN;
63 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
64 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
66 bool operator!() const { return !m_ptr; }
68 // This conversion operator allows implicit conversion to bool but not to other integer types.
69 typedef PtrType OwnPtr::*UnspecifiedBoolType;
70 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0; }
72 OwnPtr& operator=(const PassOwnPtr<T>&);
73 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; }
74 template<typename U> OwnPtr& operator=(const PassOwnPtr<U>&);
76 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
79 OwnPtr& operator=(const OwnPtr<T>&);
81 // We should never have two OwnPtrs for the same underlying object (otherwise we'll get
82 // double-destruction), so these equality operators should never be needed.
83 template<typename U> bool operator==(const OwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
84 template<typename U> bool operator!=(const OwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
85 template<typename U> bool operator==(const PassOwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
86 template<typename U> bool operator!=(const PassOwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
91 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<U>& o)
96 template<typename T> inline void OwnPtr<T>::clear()
103 template<typename T> inline PassOwnPtr<T> OwnPtr<T>::release()
107 return adoptPtr(ptr);
110 template<typename T> inline typename OwnPtr<T>::PtrType OwnPtr<T>::leakPtr()
117 template<typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<T>& o)
121 ASSERT(!ptr || m_ptr != ptr);
126 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<U>& o)
130 ASSERT(!ptr || m_ptr != ptr);
135 template<typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b)
140 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a, U* b)
145 template<typename T, typename U> inline bool operator==(T* a, const OwnPtr<U>& b)
150 template<typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, U* b)
155 template<typename T, typename U> inline bool operator!=(T* a, const OwnPtr<U>& b)
160 template<typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>& p)
169 #endif // WTF_OwnPtr_h