| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef PlatformSTL_h |
| 6 #define PlatformSTL_h |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #define PLATFORM_EXPORT |
| 11 #ifndef CHECK |
| 12 #define CHECK(condition) ((void) 0) |
| 13 #endif |
| 14 #define DCHECK(condition) ((void) 0) |
| 15 #define NOTREACHED() |
| 16 #define DCHECK_EQ(i, j) DCHECK(i == j) |
| 17 #define DCHECK_GE(i, j) DCHECK(i >= j) |
| 18 #define DCHECK_LT(i, j) DCHECK(i < j) |
| 19 #define DCHECK_GT(i, j) DCHECK(i > j) |
| 20 template <typename T> |
| 21 inline void USE(T) { } |
| 22 |
| 23 #if defined(__APPLE__) && !defined(_LIBCPP_VERSION) |
| 24 |
| 25 namespace std { |
| 26 |
| 27 template <typename T1, typename T2> |
| 28 struct is_convertible { |
| 29 private: |
| 30 struct True_ { |
| 31 char x[2]; |
| 32 }; |
| 33 struct False_ { |
| 34 }; |
| 35 |
| 36 static True_ helper(T2 const &); |
| 37 static False_ helper(...); |
| 38 |
| 39 public: |
| 40 static bool const value = ( |
| 41 sizeof(True_) == sizeof(is_convertible::helper(T1())) |
| 42 ); |
| 43 }; |
| 44 |
| 45 template <bool, class T = void> |
| 46 struct enable_if { |
| 47 }; |
| 48 |
| 49 template <class T> |
| 50 struct enable_if<true, T> { |
| 51 typedef T type; |
| 52 }; |
| 53 |
| 54 template<class T> |
| 55 struct remove_extent { |
| 56 typedef T type; |
| 57 }; |
| 58 |
| 59 template<class T> |
| 60 struct remove_extent<T[]> { |
| 61 typedef T type; |
| 62 }; |
| 63 |
| 64 template<class T, std::size_t N> |
| 65 struct remove_extent<T[N]> { |
| 66 typedef T type; |
| 67 }; |
| 68 |
| 69 typedef decltype(nullptr) nullptr_t; |
| 70 |
| 71 template<class T, T v> |
| 72 struct integral_constant { |
| 73 static constexpr T value = v; |
| 74 typedef T value_type; |
| 75 typedef integral_constant type; |
| 76 constexpr operator value_type() const noexcept { return value; } |
| 77 constexpr value_type operator()() const noexcept { return value; } |
| 78 }; |
| 79 |
| 80 typedef integral_constant<bool, true> true_type; |
| 81 typedef integral_constant<bool, false> false_type; |
| 82 |
| 83 template<class T> |
| 84 struct is_array : false_type {}; |
| 85 |
| 86 template<class T> |
| 87 struct is_array<T[]> : true_type {}; |
| 88 |
| 89 template<class T, std::size_t N> |
| 90 struct is_array<T[N]> : true_type {}; |
| 91 |
| 92 template <typename T> |
| 93 struct OwnedPtrDeleter { |
| 94 static void deletePtr(T* ptr) |
| 95 { |
| 96 static_assert(sizeof(T) > 0, "type must be complete"); |
| 97 delete ptr; |
| 98 } |
| 99 }; |
| 100 |
| 101 template <typename T> |
| 102 struct OwnedPtrDeleter<T[]> { |
| 103 static void deletePtr(T* ptr) |
| 104 { |
| 105 static_assert(sizeof(T) > 0, "type must be complete"); |
| 106 delete[] ptr; |
| 107 } |
| 108 }; |
| 109 |
| 110 template <class T, int n> |
| 111 struct OwnedPtrDeleter<T[n]> { |
| 112 static_assert(sizeof(T) < 0, "do not use array with size as type"); |
| 113 }; |
| 114 |
| 115 template <typename T> class unique_ptr { |
| 116 public: |
| 117 typedef typename remove_extent<T>::type ValueType; |
| 118 typedef ValueType* PtrType; |
| 119 |
| 120 unique_ptr() : m_ptr(nullptr) {} |
| 121 unique_ptr(std::nullptr_t) : m_ptr(nullptr) {} |
| 122 unique_ptr(unique_ptr&&); |
| 123 template <typename U, typename = typename enable_if<is_convertible<U*, T*>::
value>::type> unique_ptr(unique_ptr<U>&&); |
| 124 |
| 125 ~unique_ptr() |
| 126 { |
| 127 OwnedPtrDeleter<T>::deletePtr(m_ptr); |
| 128 m_ptr = nullptr; |
| 129 } |
| 130 |
| 131 PtrType get() const { return m_ptr; } |
| 132 |
| 133 void reset(PtrType = nullptr); |
| 134 PtrType release(); |
| 135 |
| 136 ValueType& operator*() const { DCHECK(m_ptr); return *m_ptr; } |
| 137 PtrType operator->() const { DCHECK(m_ptr); return m_ptr; } |
| 138 |
| 139 ValueType& operator[](std::ptrdiff_t i) const; |
| 140 |
| 141 bool operator!() const { return !m_ptr; } |
| 142 explicit operator bool() const { return m_ptr; } |
| 143 |
| 144 unique_ptr& operator=(std::nullptr_t) { reset(); return *this; } |
| 145 |
| 146 |
| 147 unique_ptr& operator=(unique_ptr&&); |
| 148 template <typename U> unique_ptr& operator=(unique_ptr<U>&&); |
| 149 |
| 150 void swap(unique_ptr& o) { std::swap(m_ptr, o.m_ptr); } |
| 151 |
| 152 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
| 153 |
| 154 explicit unique_ptr(PtrType ptr) : m_ptr(ptr) {} |
| 155 |
| 156 private: |
| 157 |
| 158 // We should never have two unique_ptrs for the same underlying object |
| 159 // (otherwise we'll get double-destruction), so these equality operators |
| 160 // should never be needed. |
| 161 template <typename U> bool operator==(const unique_ptr<U>&) const |
| 162 { |
| 163 static_assert(!sizeof(U*), "unique_ptrs should never be equal"); |
| 164 return false; |
| 165 } |
| 166 template <typename U> bool operator!=(const unique_ptr<U>&) const |
| 167 { |
| 168 static_assert(!sizeof(U*), "unique_ptrs should never be equal"); |
| 169 return false; |
| 170 } |
| 171 |
| 172 PtrType m_ptr; |
| 173 }; |
| 174 |
| 175 |
| 176 template <typename T> inline void unique_ptr<T>::reset(PtrType ptr) |
| 177 { |
| 178 PtrType p = m_ptr; |
| 179 m_ptr = ptr; |
| 180 OwnedPtrDeleter<T>::deletePtr(p); |
| 181 } |
| 182 |
| 183 template <typename T> inline typename unique_ptr<T>::PtrType unique_ptr<T>::rele
ase() |
| 184 { |
| 185 PtrType ptr = m_ptr; |
| 186 m_ptr = nullptr; |
| 187 return ptr; |
| 188 } |
| 189 |
| 190 template <typename T> inline typename unique_ptr<T>::ValueType& unique_ptr<T>::o
perator[](std::ptrdiff_t i) const |
| 191 { |
| 192 static_assert(is_array<T>::value, "elements access is possible for arrays on
ly"); |
| 193 DCHECK(m_ptr); |
| 194 DCHECK_GE(i, 0); |
| 195 return m_ptr[i]; |
| 196 } |
| 197 |
| 198 template <typename T> inline unique_ptr<T>::unique_ptr(unique_ptr<T>&& o) |
| 199 : m_ptr(o.release()) |
| 200 { |
| 201 } |
| 202 |
| 203 template <typename T> |
| 204 template <typename U, typename> inline unique_ptr<T>::unique_ptr(unique_ptr<U>&&
o) |
| 205 : m_ptr(o.release()) |
| 206 { |
| 207 static_assert(!is_array<T>::value, "pointers to array must never be converte
d"); |
| 208 } |
| 209 |
| 210 template <typename T> inline unique_ptr<T>& unique_ptr<T>::operator=(unique_ptr<
T>&& o) |
| 211 { |
| 212 PtrType ptr = m_ptr; |
| 213 m_ptr = o.release(); |
| 214 DCHECK(!ptr || m_ptr != ptr); |
| 215 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 216 |
| 217 return *this; |
| 218 } |
| 219 |
| 220 template <typename T> |
| 221 template <typename U> inline unique_ptr<T>& unique_ptr<T>::operator=(unique_ptr<
U>&& o) |
| 222 { |
| 223 static_assert(!is_array<T>::value, "pointers to array must never be converte
d"); |
| 224 PtrType ptr = m_ptr; |
| 225 m_ptr = o.release(); |
| 226 DCHECK(!ptr || m_ptr != ptr); |
| 227 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 228 |
| 229 return *this; |
| 230 } |
| 231 |
| 232 template <typename T> inline void swap(unique_ptr<T>& a, unique_ptr<T>& b) |
| 233 { |
| 234 a.swap(b); |
| 235 } |
| 236 |
| 237 template <typename T, typename U> inline bool operator==(const unique_ptr<T>& a,
U* b) |
| 238 { |
| 239 return a.get() == b; |
| 240 } |
| 241 |
| 242 template <typename T, typename U> inline bool operator==(T* a, const unique_ptr<
U>& b) |
| 243 { |
| 244 return a == b.get(); |
| 245 } |
| 246 |
| 247 template <typename T, typename U> inline bool operator!=(const unique_ptr<T>& a,
U* b) |
| 248 { |
| 249 return a.get() != b; |
| 250 } |
| 251 |
| 252 template <typename T, typename U> inline bool operator!=(T* a, const unique_ptr<
U>& b) |
| 253 { |
| 254 return a != b.get(); |
| 255 } |
| 256 |
| 257 template <typename T> inline typename unique_ptr<T>::PtrType getPtr(const unique
_ptr<T>& p) |
| 258 { |
| 259 return p.get(); |
| 260 } |
| 261 |
| 262 template <typename T> |
| 263 unique_ptr<T> move(unique_ptr<T>& ptr) |
| 264 { |
| 265 return unique_ptr<T>(ptr.release()); |
| 266 } |
| 267 |
| 268 } |
| 269 |
| 270 template <typename T> |
| 271 std::unique_ptr<T> wrapUnique(T* ptr) |
| 272 { |
| 273 return std::unique_ptr<T>(ptr); |
| 274 } |
| 275 |
| 276 #endif // PlatformSTL_h |
| OLD | NEW |