OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkUniquePtr_DEFINED | 8 #ifndef SkUniquePtr_DEFINED |
9 #define SkUniquePtr_DEFINED | 9 #define SkUniquePtr_DEFINED |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... |
41 template <typename U> static T* detector(...); | 41 template <typename U> static T* detector(...); |
42 using type = decltype(detector<remove_reference_t<D>>(0)); | 42 using type = decltype(detector<remove_reference_t<D>>(0)); |
43 }; | 43 }; |
44 | 44 |
45 public: | 45 public: |
46 using pointer = typename pointer_type_detector::type; | 46 using pointer = typename pointer_type_detector::type; |
47 using element_type = T; | 47 using element_type = T; |
48 using deleter_type = D; | 48 using deleter_type = D; |
49 | 49 |
50 private: | 50 private: |
51 template <typename B, bool = std::is_empty<B>::value /*&& !is_final<B>::valu
e*/> | 51 template <typename B, bool> |
52 struct compressed_base : private B { | 52 struct compressed_base : private B { |
53 /*constexpr*/ compressed_base() : B() {} | 53 /*constexpr*/ compressed_base() : B() {} |
54 /*constexpr*/ compressed_base(const B& b) : B(b) {} | 54 /*constexpr*/ compressed_base(const B& b) : B(b) {} |
55 /*constexpr*/ compressed_base(B&& b) : B(std::move(b)) {} | 55 /*constexpr*/ compressed_base(B&& b) : B(std::move(b)) {} |
56 /*constexpr*/ B& get() /*noexcept*/ { return *this; } | 56 /*constexpr*/ B& get() /*noexcept*/ { return *this; } |
57 /*constexpr*/ B const& get() const /*noexcept*/ { return *this; } | 57 /*constexpr*/ B const& get() const /*noexcept*/ { return *this; } |
58 void swap(compressed_base&) /*noexcept*/ { } | 58 void swap(compressed_base&) /*noexcept*/ { } |
59 }; | 59 }; |
60 | 60 |
61 template <typename B> struct compressed_base<B, false> { | 61 template <typename B> struct compressed_base<B, false> { |
62 B fb; | 62 B fb; |
63 /*constexpr*/ compressed_base() : B() {} | 63 /*constexpr*/ compressed_base() : B() {} |
64 /*constexpr*/ compressed_base(const B& b) : fb(b) {} | 64 /*constexpr*/ compressed_base(const B& b) : fb(b) {} |
65 /*constexpr*/ compressed_base(B&& b) : fb(std::move(b)) {} | 65 /*constexpr*/ compressed_base(B&& b) : fb(std::move(b)) {} |
66 /*constexpr*/ B& get() /*noexcept*/ { return fb; } | 66 /*constexpr*/ B& get() /*noexcept*/ { return fb; } |
67 /*constexpr*/ B const& get() const /*noexcept*/ { return fb; } | 67 /*constexpr*/ B const& get() const /*noexcept*/ { return fb; } |
68 void swap(compressed_base& that) /*noexcept*/ { SkTSwap(fb, that.fB); } | 68 void swap(compressed_base& that) /*noexcept*/ { SkTSwap(fb, that.fB); } |
69 }; | 69 }; |
70 | 70 |
71 struct compressed_data : private compressed_base<deleter_type> { | 71 // C++14 adds '&& !std::is_final<deleter_type>::value' to the bool condition
. |
| 72 // compressed_base_t exists and has this form to work around a bug in vs2013
sp2-3 |
| 73 using compressed_base_t = compressed_base<deleter_type, std::is_empty<delete
r_type>::value>; |
| 74 |
| 75 struct compressed_data : private compressed_base_t { |
72 pointer fPtr; | 76 pointer fPtr; |
73 /*constexpr*/ compressed_data() : compressed_base<deleter_type>(), fPtr(
) {} | 77 /*constexpr*/ compressed_data() : compressed_base_t(), fPtr() {} |
74 /*constexpr*/ compressed_data(const pointer& ptr, const deleter_type& d) | 78 /*constexpr*/ compressed_data(const pointer& ptr, const deleter_type& d) |
75 : compressed_base<deleter_type>(d), fPtr(ptr) {} | 79 : compressed_base_t(d), fPtr(ptr) {} |
76 template <typename U1, typename U2, typename = enable_if_t< | 80 template <typename U1, typename U2, typename = enable_if_t< |
77 is_convertible<U1, pointer>::value && is_convertible<U2, deleter_typ
e>::value | 81 is_convertible<U1, pointer>::value && is_convertible<U2, deleter_typ
e>::value |
78 >> /*constexpr*/ compressed_data(U1&& ptr, U2&& d) | 82 >> /*constexpr*/ compressed_data(U1&& ptr, U2&& d) |
79 : compressed_base<deleter_type>(std::forward<U2>(d)), fPtr(std::forw
ard<U1>(ptr)) {} | 83 : compressed_base_t(std::forward<U2>(d)), fPtr(std::forward<U1>(ptr)
) {} |
80 /*constexpr*/ pointer& getPointer() /*noexcept*/ { return fPtr; } | 84 /*constexpr*/ pointer& getPointer() /*noexcept*/ { return fPtr; } |
81 /*constexpr*/ pointer const& getPointer() const /*noexcept*/ { return fP
tr; } | 85 /*constexpr*/ pointer const& getPointer() const /*noexcept*/ { return fP
tr; } |
82 /*constexpr*/ deleter_type& getDeleter() /*noexcept*/ { | 86 /*constexpr*/ deleter_type& getDeleter() /*noexcept*/ { |
83 return compressed_base<deleter_type>::get(); | 87 return compressed_base_t::get(); |
84 } | 88 } |
85 /*constexpr*/ deleter_type const& getDeleter() const /*noexcept*/ { | 89 /*constexpr*/ deleter_type const& getDeleter() const /*noexcept*/ { |
86 return compressed_base<deleter_type>::get(); | 90 return compressed_base_t::get(); |
87 } | 91 } |
88 void swap(compressed_data& that) /*noexcept*/ { | 92 void swap(compressed_data& that) /*noexcept*/ { |
89 compressed_base<deleter_type>::swap(static_cast<compressed_base<dele
ter_type>>(that)); | 93 compressed_base_t::swap(static_cast<compressed_base_t>(that)); |
90 SkTSwap(fPtr, that.fPtr); | 94 SkTSwap(fPtr, that.fPtr); |
91 } | 95 } |
92 }; | 96 }; |
93 compressed_data data; | 97 compressed_data data; |
94 | 98 |
95 public: | 99 public: |
96 /*constexpr*/ unique_ptr() /*noexcept*/ : data() { | 100 /*constexpr*/ unique_ptr() /*noexcept*/ : data() { |
97 static_assert(!std::is_pointer<deleter_type>::value, "Deleter nullptr fu
nction pointer!"); | 101 static_assert(!std::is_pointer<deleter_type>::value, "Deleter nullptr fu
nction pointer!"); |
98 } | 102 } |
99 | 103 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 template <typename U> static T* detector(...); | 218 template <typename U> static T* detector(...); |
215 using type = decltype(detector<remove_reference_t<D>>(0)); | 219 using type = decltype(detector<remove_reference_t<D>>(0)); |
216 }; | 220 }; |
217 | 221 |
218 public: | 222 public: |
219 using pointer = typename pointer_type_detector::type; | 223 using pointer = typename pointer_type_detector::type; |
220 using element_type = T; | 224 using element_type = T; |
221 using deleter_type = D; | 225 using deleter_type = D; |
222 | 226 |
223 private: | 227 private: |
224 template <typename B, bool = std::is_empty<B>::value /*&& !is_final<B>::valu
e*/> | 228 template <typename B, bool> struct compressed_base : private B { |
225 struct compressed_base : private B { | |
226 /*constexpr*/ compressed_base() : B() {} | 229 /*constexpr*/ compressed_base() : B() {} |
227 /*constexpr*/ compressed_base(const B& b) : B(b) {} | 230 /*constexpr*/ compressed_base(const B& b) : B(b) {} |
228 /*constexpr*/ compressed_base(B&& b) : B(std::move(b)) {} | 231 /*constexpr*/ compressed_base(B&& b) : B(std::move(b)) {} |
229 /*constexpr*/ B& get() /*noexcept*/ { return *this; } | 232 /*constexpr*/ B& get() /*noexcept*/ { return *this; } |
230 /*constexpr*/ B const& get() const /*noexcept*/ { return *this; } | 233 /*constexpr*/ B const& get() const /*noexcept*/ { return *this; } |
231 void swap(compressed_base&) /*noexcept*/ { } | 234 void swap(compressed_base&) /*noexcept*/ { } |
232 }; | 235 }; |
233 | 236 |
234 template <typename B> struct compressed_base<B, false> { | 237 template <typename B> struct compressed_base<B, false> { |
235 B fb; | 238 B fb; |
236 /*constexpr*/ compressed_base() : B() {} | 239 /*constexpr*/ compressed_base() : B() {} |
237 /*constexpr*/ compressed_base(const B& b) : fb(b) {} | 240 /*constexpr*/ compressed_base(const B& b) : fb(b) {} |
238 /*constexpr*/ compressed_base(B&& b) : fb(std::move(b)) {} | 241 /*constexpr*/ compressed_base(B&& b) : fb(std::move(b)) {} |
239 /*constexpr*/ B& get() /*noexcept*/ { return fb; } | 242 /*constexpr*/ B& get() /*noexcept*/ { return fb; } |
240 /*constexpr*/ B const& get() const /*noexcept*/ { return fb; } | 243 /*constexpr*/ B const& get() const /*noexcept*/ { return fb; } |
241 void swap(compressed_base& that) /*noexcept*/ { SkTSwap(fb, that.fB); } | 244 void swap(compressed_base& that) /*noexcept*/ { SkTSwap(fb, that.fB); } |
242 }; | 245 }; |
243 | 246 |
244 struct compressed_data : private compressed_base<deleter_type> { | 247 // C++14 adds '&& !std::is_final<deleter_type>::value' to the bool condition
. |
| 248 // compressed_base_t exists and has this form to work around a bug in vs2013
sp2-3 |
| 249 using compressed_base_t = compressed_base<deleter_type, std::is_empty<delete
r_type>::value>; |
| 250 |
| 251 struct compressed_data : private compressed_base_t { |
245 pointer fPtr; | 252 pointer fPtr; |
246 /*constexpr*/ compressed_data() : compressed_base<deleter_type>(), fPtr(
) {} | 253 /*constexpr*/ compressed_data() : compressed_base_t(), fPtr() {} |
247 /*constexpr*/ compressed_data(const pointer& ptr, const deleter_type& d) | 254 /*constexpr*/ compressed_data(const pointer& ptr, const deleter_type& d) |
248 : compressed_base<deleter_type>(d), fPtr(ptr) {} | 255 : compressed_base_t(d), fPtr(ptr) {} |
249 template <typename U1, typename U2, typename = enable_if_t< | 256 template <typename U1, typename U2, typename = enable_if_t< |
250 is_convertible<U1, pointer>::value && is_convertible<U2, deleter_typ
e>::value | 257 is_convertible<U1, pointer>::value && is_convertible<U2, deleter_typ
e>::value |
251 >> /*constexpr*/ compressed_data(U1&& ptr, U2&& d) | 258 >> /*constexpr*/ compressed_data(U1&& ptr, U2&& d) |
252 : compressed_base<deleter_type>(std::forward<U2>(d)), fPtr(std::forw
ard<U1>(ptr)) {} | 259 : compressed_base_t(std::forward<U2>(d)), fPtr(std::forward<U1>(ptr)
) {} |
253 /*constexpr*/ pointer& getPointer() /*noexcept*/ { return fPtr; } | 260 /*constexpr*/ pointer& getPointer() /*noexcept*/ { return fPtr; } |
254 /*constexpr*/ pointer const& getPointer() const /*noexcept*/ { return fP
tr; } | 261 /*constexpr*/ pointer const& getPointer() const /*noexcept*/ { return fP
tr; } |
255 /*constexpr*/ deleter_type& getDeleter() /*noexcept*/ { | 262 /*constexpr*/ deleter_type& getDeleter() /*noexcept*/ { |
256 return compressed_base<deleter_type>::get(); | 263 return compressed_base_t::get(); |
257 } | 264 } |
258 /*constexpr*/ deleter_type const& getDeleter() const /*noexcept*/ { | 265 /*constexpr*/ deleter_type const& getDeleter() const /*noexcept*/ { |
259 return compressed_base<deleter_type>::get(); | 266 return compressed_base_t::get(); |
260 } | 267 } |
261 void swap(compressed_data& that) /*noexcept*/ { | 268 void swap(compressed_data& that) /*noexcept*/ { |
262 compressed_base<deleter_type>::swap(static_cast<compressed_base<dele
ter_type>>(that)); | 269 compressed_base_t::swap(static_cast<compressed_base_t>(that)); |
263 SkTSwap(fPtr, that.fPtr); | 270 SkTSwap(fPtr, that.fPtr); |
264 } | 271 } |
265 }; | 272 }; |
266 compressed_data data; | 273 compressed_data data; |
267 | 274 |
268 public: | 275 public: |
269 /*constexpr*/ unique_ptr() /*noexcept*/ : data() { | 276 /*constexpr*/ unique_ptr() /*noexcept*/ : data() { |
270 static_assert(!std::is_pointer<deleter_type>::value, "Deleter nullptr fu
nction pointer!"); | 277 static_assert(!std::is_pointer<deleter_type>::value, "Deleter nullptr fu
nction pointer!"); |
271 } | 278 } |
272 | 279 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 | 399 |
393 template <typename T, typename D> | 400 template <typename T, typename D> |
394 inline bool operator!=(std::nullptr_t, const unique_ptr<T, D>& b) /*noexcept*/ { | 401 inline bool operator!=(std::nullptr_t, const unique_ptr<T, D>& b) /*noexcept*/ { |
395 //return (bool)b; | 402 //return (bool)b; |
396 return b.is_attached(); | 403 return b.is_attached(); |
397 } | 404 } |
398 | 405 |
399 } // namespace skstd | 406 } // namespace skstd |
400 | 407 |
401 #endif | 408 #endif |
OLD | NEW |