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