1 /*
2  *  Copyright (C) 2006, 2010 Apple Inc. All rights reserved.
3  *
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.
8  *
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.
13  *
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.
18  *
19  */
20 
21 #ifndef WTF_OwnArrayPtr_h
22 #define WTF_OwnArrayPtr_h
23 
24 #include "Assertions.h"
25 #include "Noncopyable.h"
26 #include "NullPtr.h"
27 #include "PassOwnArrayPtr.h"
28 #include <algorithm>
29 
30 namespace WTF {
31 
32 template<typename T> class PassOwnArrayPtr;
33 template<typename T> PassOwnArrayPtr<T> adoptArrayPtr(T*);
34 
35 template <typename T> class OwnArrayPtr {
36 public:
37     typedef T* PtrType;
38 
OwnArrayPtr()39     OwnArrayPtr() : m_ptr(0) { }
40 
41     // See comment in PassOwnArrayPtr.h for why this takes a const reference.
42     template<typename U> OwnArrayPtr(const PassOwnArrayPtr<U>& o);
43 
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>&);
49 
~OwnArrayPtr()50     ~OwnArrayPtr() { deleteOwnedArrayPtr(m_ptr); }
51 
get()52     PtrType get() const { return m_ptr; }
53 
54     void clear();
55     PassOwnArrayPtr<T> release();
56     PtrType leakPtr() WARN_UNUSED_RETURN;
57 
58     T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
59     PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
60 
61     T& operator[](std::ptrdiff_t i) const { ASSERT(m_ptr); ASSERT(i >= 0); return m_ptr[i]; }
62 
63     bool operator!() const { return !m_ptr; }
64 
65     // This conversion operator allows implicit conversion to bool but not to other integer types.
66 #if COMPILER(WINSCW)
67     operator bool() const { return m_ptr; }
68 #else
69     typedef T* OwnArrayPtr::*UnspecifiedBoolType;
UnspecifiedBoolType()70     operator UnspecifiedBoolType() const { return m_ptr ? &OwnArrayPtr::m_ptr : 0; }
71 #endif
72 
73     OwnArrayPtr& operator=(const PassOwnArrayPtr<T>&);
74     OwnArrayPtr& operator=(std::nullptr_t) { clear(); return *this; }
75     template<typename U> OwnArrayPtr& operator=(const PassOwnArrayPtr<U>&);
76 
swap(OwnArrayPtr & o)77     void swap(OwnArrayPtr& o) { std::swap(m_ptr, o.m_ptr); }
78 
79 #ifdef LOOSE_OWN_ARRAY_PTR
OwnArrayPtr(PtrType ptr)80     explicit OwnArrayPtr(PtrType ptr) : m_ptr(ptr) { }
81     void set(PtrType);
82 #endif
83 
84 private:
85     PtrType m_ptr;
86 };
87 
OwnArrayPtr(const PassOwnArrayPtr<U> & o)88 template<typename T> template<typename U> inline OwnArrayPtr<T>::OwnArrayPtr(const PassOwnArrayPtr<U>& o)
89     : m_ptr(o.leakPtr())
90 {
91 }
92 
clear()93 template<typename T> inline void OwnArrayPtr<T>::clear()
94 {
95     PtrType ptr = m_ptr;
96     m_ptr = 0;
97     deleteOwnedArrayPtr(ptr);
98 }
99 
release()100 template<typename T> inline PassOwnArrayPtr<T> OwnArrayPtr<T>::release()
101 {
102     PtrType ptr = m_ptr;
103     m_ptr = 0;
104     return adoptArrayPtr(ptr);
105 }
106 
leakPtr()107 template<typename T> inline typename OwnArrayPtr<T>::PtrType OwnArrayPtr<T>::leakPtr()
108 {
109     PtrType ptr = m_ptr;
110     m_ptr = 0;
111     return ptr;
112 }
113 
114 #ifdef LOOSE_OWN_ARRAY_PTR
set(PtrType ptr)115 template<typename T> inline void OwnArrayPtr<T>::set(PtrType ptr)
116 {
117     ASSERT(!ptr || m_ptr != ptr);
118     PtrType oldPtr = m_ptr;
119     m_ptr = ptr;
120     deleteOwnedArrayPtr(oldPtr);
121 }
122 #endif
123 
124 template<typename T> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<T>& o)
125 {
126     PtrType ptr = m_ptr;
127     m_ptr = o.leakPtr();
128     ASSERT(!ptr || m_ptr != ptr);
129     deleteOwnedArrayPtr(ptr);
130     return *this;
131 }
132 
133 template<typename T> template<typename U> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<U>& o)
134 {
135     PtrType ptr = m_ptr;
136     m_ptr = o.leakPtr();
137     ASSERT(!ptr || m_ptr != ptr);
138     deleteOwnedArrayPtr(ptr);
139     return *this;
140 }
141 
swap(OwnArrayPtr<T> & a,OwnArrayPtr<T> & b)142 template <typename T> inline void swap(OwnArrayPtr<T>& a, OwnArrayPtr<T>& b)
143 {
144     a.swap(b);
145 }
146 
147 template<typename T, typename U> inline bool operator==(const OwnArrayPtr<T>& a, U* b)
148 {
149     return a.get() == b;
150 }
151 
152 template<typename T, typename U> inline bool operator==(T* a, const OwnArrayPtr<U>& b)
153 {
154     return a == b.get();
155 }
156 
157 template<typename T, typename U> inline bool operator!=(const OwnArrayPtr<T>& a, U* b)
158 {
159     return a.get() != b;
160 }
161 
162 template<typename T, typename U> inline bool operator!=(T* a, const OwnArrayPtr<U>& b)
163 {
164     return a != b.get();
165 }
166 
getPtr(const OwnArrayPtr<T> & p)167 template <typename T> inline T* getPtr(const OwnArrayPtr<T>& p)
168 {
169     return p.get();
170 }
171 
172 } // namespace WTF
173 
174 using WTF::OwnArrayPtr;
175 
176 #endif // WTF_OwnArrayPtr_h
177