reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkRefCnt_DEFINED |
| 9 | #define SkRefCnt_DEFINED |
| 10 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 11 | #include "../private/SkTLogic.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 12 | #include "SkTypes.h" |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 13 | #include <atomic> |
bungeman | c901c11 | 2016-03-08 08:35:23 -0800 | [diff] [blame] | 14 | #include <functional> |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 15 | #include <memory> |
bungeman | 801b44c | 2016-04-28 11:18:07 -0700 | [diff] [blame] | 16 | #include <type_traits> |
halcanary | 217c0b3 | 2016-03-02 08:06:20 -0800 | [diff] [blame] | 17 | #include <utility> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | |
commit-bot@chromium.org | 6d2533e | 2013-10-16 15:15:58 +0000 | [diff] [blame] | 19 | /** \class SkRefCntBase |
| 20 | |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 21 | SkRefCntBase is the base class for objects that may be shared by multiple |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 22 | objects. When an existing owner wants to share a reference, it calls ref(). |
| 23 | When an owner wants to release its reference, it calls unref(). When the |
| 24 | shared object's reference count goes to zero as the result of an unref() |
| 25 | call, its (virtual) destructor is called. It is an error for the |
| 26 | destructor to be called explicitly (or via the object going out of scope on |
| 27 | the stack or calling delete) if getRefCnt() > 1. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 29 | class SK_API SkRefCntBase : SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | public: |
| 31 | /** Default construct, initializing the reference count to 1. |
| 32 | */ |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 33 | SkRefCntBase() : fRefCnt(1) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 35 | /** Destruct, asserting that the reference count is 1. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | */ |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 37 | virtual ~SkRefCntBase() { |
reed@google.com | 4c888aa | 2011-09-12 19:54:12 +0000 | [diff] [blame] | 38 | #ifdef SK_DEBUG |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 39 | SkASSERTF(getRefCnt() == 1, "fRefCnt was %d", getRefCnt()); |
| 40 | // illegal value, to catch us if we reuse after delete |
| 41 | fRefCnt.store(0, std::memory_order_relaxed); |
reed@google.com | 4c888aa | 2011-09-12 19:54:12 +0000 | [diff] [blame] | 42 | #endif |
| 43 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | |
mtklein | 21180e2 | 2014-11-25 07:30:19 -0800 | [diff] [blame] | 45 | #ifdef SK_DEBUG |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 46 | /** Return the reference count. Use only for debugging. */ |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 47 | int32_t getRefCnt() const { |
| 48 | return fRefCnt.load(std::memory_order_relaxed); |
| 49 | } |
| 50 | |
| 51 | void validate() const { |
| 52 | SkASSERT(getRefCnt() > 0); |
| 53 | } |
mtklein | 21180e2 | 2014-11-25 07:30:19 -0800 | [diff] [blame] | 54 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | |
commit-bot@chromium.org | ea6e14a | 2014-02-04 18:00:23 +0000 | [diff] [blame] | 56 | /** May return true if the caller is the only owner. |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 57 | * Ensures that all previous owner's actions are complete. |
| 58 | */ |
| 59 | bool unique() const { |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 60 | if (1 == fRefCnt.load(std::memory_order_acquire)) { |
mtklein | 7b274c7 | 2015-02-03 13:38:58 -0800 | [diff] [blame] | 61 | // The acquire barrier is only really needed if we return true. It |
| 62 | // prevents code conditioned on the result of unique() from running |
| 63 | // until previous owners are all totally done calling unref(). |
| 64 | return true; |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 65 | } |
mtklein | 7b274c7 | 2015-02-03 13:38:58 -0800 | [diff] [blame] | 66 | return false; |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 67 | } |
| 68 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | /** Increment the reference count. Must be balanced by a call to unref(). |
| 70 | */ |
| 71 | void ref() const { |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 72 | SkASSERT(getRefCnt() > 0); |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 73 | // No barrier required. |
| 74 | (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** Decrement the reference count. If the reference count is 1 before the |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 78 | decrement, then delete the object. Note that if this is the case, then |
| 79 | the object needs to have been allocated via new, and not on the stack. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | */ |
| 81 | void unref() const { |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 82 | SkASSERT(getRefCnt() > 0); |
mtklein | 7b274c7 | 2015-02-03 13:38:58 -0800 | [diff] [blame] | 83 | // A release here acts in place of all releases we "should" have been doing in ref(). |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 84 | if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) { |
mtklein | 7b274c7 | 2015-02-03 13:38:58 -0800 | [diff] [blame] | 85 | // Like unique(), the acquire is only needed on success, to make sure |
| 86 | // code in internal_dispose() doesn't happen before the decrement. |
| 87 | this->internal_dispose(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 91 | protected: |
| 92 | /** |
| 93 | * Allow subclasses to call this if they've overridden internal_dispose |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 94 | * so they can reset fRefCnt before the destructor is called or if they |
| 95 | * choose not to call the destructor (e.g. using a free list). |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 96 | */ |
| 97 | void internal_dispose_restore_refcnt_to_1() const { |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 98 | SkASSERT(0 == getRefCnt()); |
| 99 | fRefCnt.store(1, std::memory_order_relaxed); |
reed@google.com | f794303 | 2012-07-23 14:50:38 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private: |
| 103 | /** |
| 104 | * Called when the ref count goes to 0. |
| 105 | */ |
| 106 | virtual void internal_dispose() const { |
| 107 | this->internal_dispose_restore_refcnt_to_1(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 108 | delete this; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 109 | } |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 110 | |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 111 | // The following friends are those which override internal_dispose() |
| 112 | // and conditionally call SkRefCnt::internal_dispose(). |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 113 | friend class SkWeakRefCnt; |
| 114 | |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 115 | mutable std::atomic<int32_t> fRefCnt; |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 116 | |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 117 | typedef SkNoncopyable INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 120 | #ifdef SK_REF_CNT_MIXIN_INCLUDE |
| 121 | // It is the responsibility of the following include to define the type SkRefCnt. |
| 122 | // This SkRefCnt should normally derive from SkRefCntBase. |
| 123 | #include SK_REF_CNT_MIXIN_INCLUDE |
| 124 | #else |
benjaminwagner | f9634b9 | 2016-04-13 17:51:57 -0700 | [diff] [blame] | 125 | class SK_API SkRefCnt : public SkRefCntBase { |
| 126 | // "#include SK_REF_CNT_MIXIN_INCLUDE" doesn't work with this build system. |
| 127 | #if defined(GOOGLE3) |
| 128 | public: |
| 129 | void deref() const { this->unref(); } |
| 130 | #endif |
| 131 | }; |
bungeman@google.com | 10ba006 | 2013-10-25 18:40:24 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 134 | /////////////////////////////////////////////////////////////////////////////// |
| 135 | |
| 136 | /** Helper macro to safely assign one SkRefCnt[TS]* to another, checking for |
| 137 | null in on each side of the assignment, and ensuring that ref() is called |
| 138 | before unref(), in case the two pointers point to the same object. |
| 139 | */ |
Hans Boehm | 0e90812 | 2016-09-19 22:31:29 -0700 | [diff] [blame] | 140 | |
| 141 | #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || defined(SK_DEBUG) |
| 142 | // This version heuristically detects data races, since those otherwise result |
| 143 | // in redundant reference count decrements, which are exceedingly |
| 144 | // difficult to debug. |
| 145 | |
| 146 | #define SkRefCnt_SafeAssign(dst, src) \ |
| 147 | do { \ |
Hans Boehm | e9f90a2 | 2016-09-27 14:25:27 -0700 | [diff] [blame] | 148 | typedef typename std::remove_reference<decltype(dst)>::type \ |
| 149 | SkRefCntPtrT; \ |
| 150 | SkRefCntPtrT old_dst = *const_cast<SkRefCntPtrT volatile *>(&dst); \ |
Hans Boehm | 0e90812 | 2016-09-19 22:31:29 -0700 | [diff] [blame] | 151 | if (src) src->ref(); \ |
| 152 | if (old_dst) old_dst->unref(); \ |
Hans Boehm | e9f90a2 | 2016-09-27 14:25:27 -0700 | [diff] [blame] | 153 | if (old_dst != *const_cast<SkRefCntPtrT volatile *>(&dst)) { \ |
| 154 | SkDebugf("Detected racing Skia calls at %s:%d\n", \ |
| 155 | __FILE__, __LINE__); \ |
Hans Boehm | 0e90812 | 2016-09-19 22:31:29 -0700 | [diff] [blame] | 156 | } \ |
| 157 | dst = src; \ |
| 158 | } while (0) |
| 159 | |
| 160 | #else /* !(SK_BUILD_FOR_ANDROID_FRAMEWORK || SK_DEBUG) */ |
| 161 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 162 | #define SkRefCnt_SafeAssign(dst, src) \ |
| 163 | do { \ |
| 164 | if (src) src->ref(); \ |
| 165 | if (dst) dst->unref(); \ |
| 166 | dst = src; \ |
| 167 | } while (0) |
| 168 | |
Hans Boehm | 0e90812 | 2016-09-19 22:31:29 -0700 | [diff] [blame] | 169 | #endif |
| 170 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 171 | |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 172 | /** Call obj->ref() and return obj. The obj must not be nullptr. |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 173 | */ |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 174 | template <typename T> static inline T* SkRef(T* obj) { |
| 175 | SkASSERT(obj); |
| 176 | obj->ref(); |
| 177 | return obj; |
| 178 | } |
| 179 | |
| 180 | /** Check if the argument is non-null, and if so, call obj->ref() and return obj. |
| 181 | */ |
| 182 | template <typename T> static inline T* SkSafeRef(T* obj) { |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 183 | if (obj) { |
| 184 | obj->ref(); |
| 185 | } |
bungeman@google.com | 1fd201b | 2012-08-22 18:56:56 +0000 | [diff] [blame] | 186 | return obj; |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** Check if the argument is non-null, and if so, call obj->unref() |
| 190 | */ |
| 191 | template <typename T> static inline void SkSafeUnref(T* obj) { |
| 192 | if (obj) { |
| 193 | obj->unref(); |
| 194 | } |
| 195 | } |
| 196 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 197 | template<typename T> static inline void SkSafeSetNull(T*& obj) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 198 | if (obj) { |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 199 | obj->unref(); |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 200 | obj = nullptr; |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
reed@google.com | 7f6d6d4 | 2011-07-15 15:25:22 +0000 | [diff] [blame] | 204 | /////////////////////////////////////////////////////////////////////////////// |
| 205 | |
mtklein | 08d1fcc | 2014-11-20 09:18:31 -0800 | [diff] [blame] | 206 | // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead of 8 or 16. |
| 207 | // There's only benefit to using this if the deriving class does not otherwise need a vtable. |
| 208 | template <typename Derived> |
| 209 | class SkNVRefCnt : SkNoncopyable { |
| 210 | public: |
| 211 | SkNVRefCnt() : fRefCnt(1) {} |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 212 | ~SkNVRefCnt() { SkASSERTF(1 == getRefCnt(), "NVRefCnt was %d", getRefCnt()); } |
mtklein | 08d1fcc | 2014-11-20 09:18:31 -0800 | [diff] [blame] | 213 | |
| 214 | // Implementation is pretty much the same as SkRefCntBase. All required barriers are the same: |
| 215 | // - unique() needs acquire when it returns true, and no barrier if it returns false; |
| 216 | // - ref() doesn't need any barrier; |
| 217 | // - unref() needs a release barrier, and an acquire if it's going to call delete. |
| 218 | |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 219 | bool unique() const { return 1 == fRefCnt.load(std::memory_order_acquire); } |
| 220 | void ref() const { (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed); } |
reed | 90d0ff0 | 2014-11-24 12:02:31 -0800 | [diff] [blame] | 221 | void unref() const { |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 222 | if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) { |
| 223 | // restore the 1 for our destructor's assert |
| 224 | SkDEBUGCODE(fRefCnt.store(1, std::memory_order_relaxed)); |
mtklein | 97466ab | 2015-12-07 13:37:00 -0800 | [diff] [blame] | 225 | delete (const Derived*)this; |
reed | 90d0ff0 | 2014-11-24 12:02:31 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
mtklein | 7b274c7 | 2015-02-03 13:38:58 -0800 | [diff] [blame] | 228 | void deref() const { this->unref(); } |
Florin Malita | 844aa33 | 2014-11-20 16:56:22 -0500 | [diff] [blame] | 229 | |
mtklein | 08d1fcc | 2014-11-20 09:18:31 -0800 | [diff] [blame] | 230 | private: |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 231 | mutable std::atomic<int32_t> fRefCnt; |
| 232 | int32_t getRefCnt() const { |
| 233 | return fRefCnt.load(std::memory_order_relaxed); |
| 234 | } |
mtklein | 08d1fcc | 2014-11-20 09:18:31 -0800 | [diff] [blame] | 235 | }; |
| 236 | |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 237 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 238 | |
| 239 | /** |
| 240 | * Shared pointer class to wrap classes that support a ref()/unref() interface. |
| 241 | * |
| 242 | * This can be used for classes inheriting from SkRefCnt, but it also works for other |
| 243 | * classes that match the interface, but have different internal choices: e.g. the hosted class |
| 244 | * may have its ref/unref be thread-safe, but that is not assumed/imposed by sk_sp. |
| 245 | */ |
| 246 | template <typename T> class sk_sp { |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 247 | /** Supports safe bool idiom. Obsolete with explicit operator bool. */ |
| 248 | using unspecified_bool_type = T* sk_sp::*; |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 249 | public: |
bungeman | c901c11 | 2016-03-08 08:35:23 -0800 | [diff] [blame] | 250 | using element_type = T; |
| 251 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 252 | constexpr sk_sp() : fPtr(nullptr) {} |
| 253 | constexpr sk_sp(std::nullptr_t) : fPtr(nullptr) {} |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | * Shares the underlying object by calling ref(), so that both the argument and the newly |
| 257 | * created sk_sp both have a reference to it. |
| 258 | */ |
| 259 | sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} |
bungeman | 801b44c | 2016-04-28 11:18:07 -0700 | [diff] [blame] | 260 | template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>> |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 261 | sk_sp(const sk_sp<U>& that) : fPtr(SkSafeRef(that.get())) {} |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * Move the underlying object from the argument to the newly created sk_sp. Afterwards only |
| 265 | * the new sk_sp will have a reference to the object, and the argument will point to null. |
| 266 | * No call to ref() or unref() will be made. |
| 267 | */ |
| 268 | sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {} |
bungeman | 801b44c | 2016-04-28 11:18:07 -0700 | [diff] [blame] | 269 | template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>> |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 270 | sk_sp(sk_sp<U>&& that) : fPtr(that.release()) {} |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * Adopt the bare pointer into the newly created sk_sp. |
| 274 | * No call to ref() or unref() will be made. |
| 275 | */ |
| 276 | explicit sk_sp(T* obj) : fPtr(obj) {} |
| 277 | |
| 278 | /** |
| 279 | * Calls unref() on the underlying object pointer. |
| 280 | */ |
| 281 | ~sk_sp() { |
| 282 | SkSafeUnref(fPtr); |
bungeman | c901c11 | 2016-03-08 08:35:23 -0800 | [diff] [blame] | 283 | SkDEBUGCODE(fPtr = nullptr); |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 284 | } |
| 285 | |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 286 | sk_sp<T>& operator=(std::nullptr_t) { this->reset(); return *this; } |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 287 | |
| 288 | /** |
| 289 | * Shares the underlying object referenced by the argument by calling ref() on it. If this |
| 290 | * sk_sp previously had a reference to an object (i.e. not null) it will call unref() on that |
| 291 | * object. |
| 292 | */ |
| 293 | sk_sp<T>& operator=(const sk_sp<T>& that) { |
| 294 | this->reset(SkSafeRef(that.get())); |
| 295 | return *this; |
| 296 | } |
bungeman | 801b44c | 2016-04-28 11:18:07 -0700 | [diff] [blame] | 297 | template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>> |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 298 | sk_sp<T>& operator=(const sk_sp<U>& that) { |
| 299 | this->reset(SkSafeRef(that.get())); |
| 300 | return *this; |
| 301 | } |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 302 | |
| 303 | /** |
| 304 | * Move the underlying object from the argument to the sk_sp. If the sk_sp previously held |
| 305 | * a reference to another object, unref() will be called on that object. No call to ref() |
| 306 | * will be made. |
| 307 | */ |
| 308 | sk_sp<T>& operator=(sk_sp<T>&& that) { |
| 309 | this->reset(that.release()); |
| 310 | return *this; |
| 311 | } |
bungeman | 801b44c | 2016-04-28 11:18:07 -0700 | [diff] [blame] | 312 | template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>> |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 313 | sk_sp<T>& operator=(sk_sp<U>&& that) { |
| 314 | this->reset(that.release()); |
| 315 | return *this; |
| 316 | } |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 317 | |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 318 | T& operator*() const { |
| 319 | SkASSERT(this->get() != nullptr); |
| 320 | return *this->get(); |
| 321 | } |
| 322 | |
halcanary | 89bd99b | 2016-03-02 08:21:39 -0800 | [diff] [blame] | 323 | // MSVC 2013 does not work correctly with explicit operator bool. |
| 324 | // https://chromium-cpp.appspot.com/#core-blacklist |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 325 | // When explicit operator bool can be used, remove operator! and operator unspecified_bool_type. |
halcanary | 89bd99b | 2016-03-02 08:21:39 -0800 | [diff] [blame] | 326 | //explicit operator bool() const { return this->get() != nullptr; } |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 327 | operator unspecified_bool_type() const { return this->get() ? &sk_sp::fPtr : nullptr; } |
halcanary | 89bd99b | 2016-03-02 08:21:39 -0800 | [diff] [blame] | 328 | bool operator!() const { return this->get() == nullptr; } |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 329 | |
| 330 | T* get() const { return fPtr; } |
| 331 | T* operator->() const { return fPtr; } |
| 332 | |
| 333 | /** |
| 334 | * Adopt the new bare pointer, and call unref() on any previously held object (if not null). |
| 335 | * No call to ref() will be made. |
| 336 | */ |
| 337 | void reset(T* ptr = nullptr) { |
bungeman | c901c11 | 2016-03-08 08:35:23 -0800 | [diff] [blame] | 338 | // Calling fPtr->unref() may call this->~() or this->reset(T*). |
| 339 | // http://wg21.cmeerw.net/lwg/issue998 |
| 340 | // http://wg21.cmeerw.net/lwg/issue2262 |
| 341 | T* oldPtr = fPtr; |
reed | 941da9d | 2016-03-06 13:54:00 -0800 | [diff] [blame] | 342 | fPtr = ptr; |
bungeman | c901c11 | 2016-03-08 08:35:23 -0800 | [diff] [blame] | 343 | SkSafeUnref(oldPtr); |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Return the bare pointer, and set the internal object pointer to nullptr. |
| 348 | * The caller must assume ownership of the object, and manage its reference count directly. |
| 349 | * No call to unref() will be made. |
| 350 | */ |
| 351 | T* SK_WARN_UNUSED_RESULT release() { |
| 352 | T* ptr = fPtr; |
| 353 | fPtr = nullptr; |
| 354 | return ptr; |
| 355 | } |
| 356 | |
bungeman | c901c11 | 2016-03-08 08:35:23 -0800 | [diff] [blame] | 357 | void swap(sk_sp<T>& that) /*noexcept*/ { |
| 358 | using std::swap; |
| 359 | swap(fPtr, that.fPtr); |
| 360 | } |
| 361 | |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 362 | private: |
| 363 | T* fPtr; |
| 364 | }; |
| 365 | |
bungeman | c901c11 | 2016-03-08 08:35:23 -0800 | [diff] [blame] | 366 | template <typename T> inline void swap(sk_sp<T>& a, sk_sp<T>& b) /*noexcept*/ { |
| 367 | a.swap(b); |
| 368 | } |
| 369 | |
| 370 | template <typename T, typename U> inline bool operator==(const sk_sp<T>& a, const sk_sp<U>& b) { |
| 371 | return a.get() == b.get(); |
| 372 | } |
| 373 | template <typename T> inline bool operator==(const sk_sp<T>& a, std::nullptr_t) /*noexcept*/ { |
| 374 | return !a; |
| 375 | } |
| 376 | template <typename T> inline bool operator==(std::nullptr_t, const sk_sp<T>& b) /*noexcept*/ { |
| 377 | return !b; |
| 378 | } |
| 379 | |
| 380 | template <typename T, typename U> inline bool operator!=(const sk_sp<T>& a, const sk_sp<U>& b) { |
| 381 | return a.get() != b.get(); |
| 382 | } |
| 383 | template <typename T> inline bool operator!=(const sk_sp<T>& a, std::nullptr_t) /*noexcept*/ { |
| 384 | return static_cast<bool>(a); |
| 385 | } |
| 386 | template <typename T> inline bool operator!=(std::nullptr_t, const sk_sp<T>& b) /*noexcept*/ { |
| 387 | return static_cast<bool>(b); |
| 388 | } |
| 389 | |
| 390 | template <typename T, typename U> inline bool operator<(const sk_sp<T>& a, const sk_sp<U>& b) { |
| 391 | // Provide defined total order on sk_sp. |
| 392 | // http://wg21.cmeerw.net/lwg/issue1297 |
| 393 | // http://wg21.cmeerw.net/lwg/issue1401 . |
| 394 | return std::less<skstd::common_type_t<T*, U*>>()(a.get(), b.get()); |
| 395 | } |
| 396 | template <typename T> inline bool operator<(const sk_sp<T>& a, std::nullptr_t) { |
| 397 | return std::less<T*>()(a.get(), nullptr); |
| 398 | } |
| 399 | template <typename T> inline bool operator<(std::nullptr_t, const sk_sp<T>& b) { |
| 400 | return std::less<T*>()(nullptr, b.get()); |
| 401 | } |
| 402 | |
| 403 | template <typename T, typename U> inline bool operator<=(const sk_sp<T>& a, const sk_sp<U>& b) { |
| 404 | return !(b < a); |
| 405 | } |
| 406 | template <typename T> inline bool operator<=(const sk_sp<T>& a, std::nullptr_t) { |
| 407 | return !(nullptr < a); |
| 408 | } |
| 409 | template <typename T> inline bool operator<=(std::nullptr_t, const sk_sp<T>& b) { |
| 410 | return !(b < nullptr); |
| 411 | } |
| 412 | |
| 413 | template <typename T, typename U> inline bool operator>(const sk_sp<T>& a, const sk_sp<U>& b) { |
| 414 | return b < a; |
| 415 | } |
| 416 | template <typename T> inline bool operator>(const sk_sp<T>& a, std::nullptr_t) { |
| 417 | return nullptr < a; |
| 418 | } |
| 419 | template <typename T> inline bool operator>(std::nullptr_t, const sk_sp<T>& b) { |
| 420 | return b < nullptr; |
| 421 | } |
| 422 | |
| 423 | template <typename T, typename U> inline bool operator>=(const sk_sp<T>& a, const sk_sp<U>& b) { |
| 424 | return !(a < b); |
| 425 | } |
| 426 | template <typename T> inline bool operator>=(const sk_sp<T>& a, std::nullptr_t) { |
| 427 | return !(a < nullptr); |
| 428 | } |
| 429 | template <typename T> inline bool operator>=(std::nullptr_t, const sk_sp<T>& b) { |
| 430 | return !(nullptr < b); |
| 431 | } |
| 432 | |
halcanary | 217c0b3 | 2016-03-02 08:06:20 -0800 | [diff] [blame] | 433 | template <typename T, typename... Args> |
| 434 | sk_sp<T> sk_make_sp(Args&&... args) { |
| 435 | return sk_sp<T>(new T(std::forward<Args>(args)...)); |
| 436 | } |
| 437 | |
reed | 647cc84 | 2016-03-08 12:54:48 -0800 | [diff] [blame] | 438 | /* |
| 439 | * Returns a sk_sp wrapping the provided ptr AND calls ref on it (if not null). |
| 440 | * |
| 441 | * This is different than the semantics of the constructor for sk_sp, which just wraps the ptr, |
| 442 | * effectively "adopting" it. |
reed | 647cc84 | 2016-03-08 12:54:48 -0800 | [diff] [blame] | 443 | */ |
| 444 | template <typename T> sk_sp<T> sk_ref_sp(T* obj) { |
| 445 | return sk_sp<T>(SkSafeRef(obj)); |
| 446 | } |
| 447 | |
| 448 | #endif |