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