OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
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 SkRefCnt_DEFINED | 8 #ifndef SkRefCnt_DEFINED |
9 #define SkRefCnt_DEFINED | 9 #define SkRefCnt_DEFINED |
10 | 10 |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 231 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
232 | 232 |
233 /** | 233 /** |
234 * Shared pointer class to wrap classes that support a ref()/unref() interface. | 234 * Shared pointer class to wrap classes that support a ref()/unref() interface. |
235 * | 235 * |
236 * This can be used for classes inheriting from SkRefCnt, but it also works for
other | 236 * This can be used for classes inheriting from SkRefCnt, but it also works for
other |
237 * classes that match the interface, but have different internal choices: e.g.
the hosted class | 237 * classes that match the interface, but have different internal choices: e.g.
the hosted class |
238 * may have its ref/unref be thread-safe, but that is not assumed/imposed by sk
_sp. | 238 * may have its ref/unref be thread-safe, but that is not assumed/imposed by sk
_sp. |
239 */ | 239 */ |
240 template <typename T> class sk_sp { | 240 template <typename T> class sk_sp { |
| 241 /** Supports safe bool idiom. Obsolete with explicit operator bool. */ |
| 242 using unspecified_bool_type = T* sk_sp::*; |
241 public: | 243 public: |
242 sk_sp() : fPtr(nullptr) {} | 244 sk_sp() : fPtr(nullptr) {} |
243 sk_sp(std::nullptr_t) : fPtr(nullptr) {} | 245 sk_sp(std::nullptr_t) : fPtr(nullptr) {} |
244 | 246 |
245 /** | 247 /** |
246 * Shares the underlying object by calling ref(), so that both the argument
and the newly | 248 * Shares the underlying object by calling ref(), so that both the argument
and the newly |
247 * created sk_sp both have a reference to it. | 249 * created sk_sp both have a reference to it. |
248 */ | 250 */ |
249 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} | 251 sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} |
250 template <typename U, | 252 template <typename U, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 } | 311 } |
310 | 312 |
311 bool operator==(std::nullptr_t) const { return this->get() == nullptr; } | 313 bool operator==(std::nullptr_t) const { return this->get() == nullptr; } |
312 bool operator!=(std::nullptr_t) const { return this->get() != nullptr; } | 314 bool operator!=(std::nullptr_t) const { return this->get() != nullptr; } |
313 | 315 |
314 template <typename U> | 316 template <typename U> |
315 bool operator==(const sk_sp<U>& that) const { return this->get() == that.get
(); } | 317 bool operator==(const sk_sp<U>& that) const { return this->get() == that.get
(); } |
316 template <typename U> | 318 template <typename U> |
317 bool operator!=(const sk_sp<U>& that) const { return this->get() != that.get
(); } | 319 bool operator!=(const sk_sp<U>& that) const { return this->get() != that.get
(); } |
318 | 320 |
| 321 T& operator*() const { |
| 322 SkASSERT(this->get() != nullptr); |
| 323 return *this->get(); |
| 324 } |
| 325 |
319 // MSVC 2013 does not work correctly with explicit operator bool. | 326 // MSVC 2013 does not work correctly with explicit operator bool. |
320 // https://chromium-cpp.appspot.com/#core-blacklist | 327 // https://chromium-cpp.appspot.com/#core-blacklist |
| 328 // When explicit operator bool can be used, remove operator! and operator un
specified_bool_type. |
321 //explicit operator bool() const { return this->get() != nullptr; } | 329 //explicit operator bool() const { return this->get() != nullptr; } |
322 | 330 operator unspecified_bool_type() const { return this->get() ? &sk_sp::fPtr :
nullptr; } |
323 bool operator!() const { return this->get() == nullptr; } | 331 bool operator!() const { return this->get() == nullptr; } |
324 | 332 |
325 T* get() const { return fPtr; } | 333 T* get() const { return fPtr; } |
326 T* operator->() const { return fPtr; } | 334 T* operator->() const { return fPtr; } |
327 | 335 |
328 /** | 336 /** |
329 * Adopt the new bare pointer, and call unref() on any previously held obje
ct (if not null). | 337 * Adopt the new bare pointer, and call unref() on any previously held obje
ct (if not null). |
330 * No call to ref() will be made. | 338 * No call to ref() will be made. |
331 */ | 339 */ |
332 void reset(T* ptr = nullptr) { | 340 void reset(T* ptr = nullptr) { |
(...skipping 17 matching lines...) Expand all Loading... |
350 private: | 358 private: |
351 T* fPtr; | 359 T* fPtr; |
352 }; | 360 }; |
353 | 361 |
354 template <typename T, typename... Args> | 362 template <typename T, typename... Args> |
355 sk_sp<T> sk_make_sp(Args&&... args) { | 363 sk_sp<T> sk_make_sp(Args&&... args) { |
356 return sk_sp<T>(new T(std::forward<Args>(args)...)); | 364 return sk_sp<T>(new T(std::forward<Args>(args)...)); |
357 } | 365 } |
358 | 366 |
359 #endif | 367 #endif |
OLD | NEW |