initial import
[vuplus_webkit] / Source / JavaScriptCore / wtf / OwnArrayPtr.h
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
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
50     ~OwnArrayPtr() { deleteOwnedArrayPtr(m_ptr); }
51
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;
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
77     void swap(OwnArrayPtr& o) { std::swap(m_ptr, o.m_ptr); }
78
79 private:
80     PtrType m_ptr;
81 };
82
83 template<typename T> template<typename U> inline OwnArrayPtr<T>::OwnArrayPtr(const PassOwnArrayPtr<U>& o)
84     : m_ptr(o.leakPtr())
85 {
86 }
87
88 template<typename T> inline void OwnArrayPtr<T>::clear()
89 {
90     PtrType ptr = m_ptr;
91     m_ptr = 0;
92     deleteOwnedArrayPtr(ptr);
93 }
94
95 template<typename T> inline PassOwnArrayPtr<T> OwnArrayPtr<T>::release()
96 {
97     PtrType ptr = m_ptr;
98     m_ptr = 0;
99     return adoptArrayPtr(ptr);
100 }
101
102 template<typename T> inline typename OwnArrayPtr<T>::PtrType OwnArrayPtr<T>::leakPtr()
103 {
104     PtrType ptr = m_ptr;
105     m_ptr = 0;
106     return ptr;
107 }
108
109 template<typename T> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<T>& o)
110 {
111     PtrType ptr = m_ptr;
112     m_ptr = o.leakPtr();
113     ASSERT(!ptr || m_ptr != ptr);
114     deleteOwnedArrayPtr(ptr);
115     return *this;
116 }
117
118 template<typename T> template<typename U> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<U>& o)
119 {
120     PtrType ptr = m_ptr;
121     m_ptr = o.leakPtr();
122     ASSERT(!ptr || m_ptr != ptr);
123     deleteOwnedArrayPtr(ptr);
124     return *this;
125 }
126
127 template <typename T> inline void swap(OwnArrayPtr<T>& a, OwnArrayPtr<T>& b)
128 {
129     a.swap(b);
130 }
131
132 template<typename T, typename U> inline bool operator==(const OwnArrayPtr<T>& a, U* b)
133 {
134     return a.get() == b; 
135 }
136
137 template<typename T, typename U> inline bool operator==(T* a, const OwnArrayPtr<U>& b) 
138 {
139     return a == b.get(); 
140 }
141
142 template<typename T, typename U> inline bool operator!=(const OwnArrayPtr<T>& a, U* b)
143 {
144     return a.get() != b; 
145 }
146
147 template<typename T, typename U> inline bool operator!=(T* a, const OwnArrayPtr<U>& b)
148 {
149     return a != b.get(); 
150 }
151
152 template <typename T> inline T* getPtr(const OwnArrayPtr<T>& p)
153 {
154     return p.get();
155 }
156
157 } // namespace WTF
158
159 using WTF::OwnArrayPtr;
160
161 #endif // WTF_OwnArrayPtr_h