2 * Copyright (C) 2006, 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.
21 #ifndef WTF_OwnArrayPtr_h
22 #define WTF_OwnArrayPtr_h
24 #include "Assertions.h"
25 #include "Noncopyable.h"
27 #include "PassOwnArrayPtr.h"
32 template<typename T> class PassOwnArrayPtr;
33 template<typename T> PassOwnArrayPtr<T> adoptArrayPtr(T*);
35 template <typename T> class OwnArrayPtr {
39 OwnArrayPtr() : m_ptr(0) { }
41 // See comment in PassOwnArrayPtr.h for why this takes a const reference.
42 template<typename U> OwnArrayPtr(const PassOwnArrayPtr<U>& o);
44 // This copy constructor is used implicitly by gcc when it generates
45 // transients for assigning a PassOwnArrayPtr<T> object to a stack-allocated
46 // OwnArrayPtr<T> object. It should never be called explicitly and gcc
47 // should optimize away the constructor when generating code.
48 OwnArrayPtr(const OwnArrayPtr<T>&);
50 ~OwnArrayPtr() { deleteOwnedArrayPtr(m_ptr); }
52 PtrType get() const { return m_ptr; }
55 PassOwnArrayPtr<T> release();
56 PtrType leakPtr() WARN_UNUSED_RETURN;
58 T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
59 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
61 T& operator[](std::ptrdiff_t i) const { ASSERT(m_ptr); ASSERT(i >= 0); return m_ptr[i]; }
63 bool operator!() const { return !m_ptr; }
65 // This conversion operator allows implicit conversion to bool but not to other integer types.
67 operator bool() const { return m_ptr; }
69 typedef T* OwnArrayPtr::*UnspecifiedBoolType;
70 operator UnspecifiedBoolType() const { return m_ptr ? &OwnArrayPtr::m_ptr : 0; }
73 OwnArrayPtr& operator=(const PassOwnArrayPtr<T>&);
74 OwnArrayPtr& operator=(std::nullptr_t) { clear(); return *this; }
75 template<typename U> OwnArrayPtr& operator=(const PassOwnArrayPtr<U>&);
77 void swap(OwnArrayPtr& o) { std::swap(m_ptr, o.m_ptr); }
83 template<typename T> template<typename U> inline OwnArrayPtr<T>::OwnArrayPtr(const PassOwnArrayPtr<U>& o)
88 template<typename T> inline void OwnArrayPtr<T>::clear()
92 deleteOwnedArrayPtr(ptr);
95 template<typename T> inline PassOwnArrayPtr<T> OwnArrayPtr<T>::release()
99 return adoptArrayPtr(ptr);
102 template<typename T> inline typename OwnArrayPtr<T>::PtrType OwnArrayPtr<T>::leakPtr()
109 template<typename T> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<T>& o)
113 ASSERT(!ptr || m_ptr != ptr);
114 deleteOwnedArrayPtr(ptr);
118 template<typename T> template<typename U> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<U>& o)
122 ASSERT(!ptr || m_ptr != ptr);
123 deleteOwnedArrayPtr(ptr);
127 template <typename T> inline void swap(OwnArrayPtr<T>& a, OwnArrayPtr<T>& b)
132 template<typename T, typename U> inline bool operator==(const OwnArrayPtr<T>& a, U* b)
137 template<typename T, typename U> inline bool operator==(T* a, const OwnArrayPtr<U>& b)
142 template<typename T, typename U> inline bool operator!=(const OwnArrayPtr<T>& a, U* b)
147 template<typename T, typename U> inline bool operator!=(T* a, const OwnArrayPtr<U>& b)
152 template <typename T> inline T* getPtr(const OwnArrayPtr<T>& p)
159 using WTF::OwnArrayPtr;
161 #endif // WTF_OwnArrayPtr_h