| 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 |
| 11 #include "../private/SkAtomics.h" | 11 #include "../private/SkAtomics.h" |
| 12 #include "../private/SkUniquePtr.h" |
| 12 #include "SkTypes.h" | 13 #include "SkTypes.h" |
| 13 #include <memory> | |
| 14 | 14 |
| 15 /** \class SkRefCntBase | 15 /** \class SkRefCntBase |
| 16 | 16 |
| 17 SkRefCntBase is the base class for objects that may be shared by multiple | 17 SkRefCntBase is the base class for objects that may be shared by multiple |
| 18 objects. When an existing owner wants to share a reference, it calls ref(). | 18 objects. When an existing owner wants to share a reference, it calls ref(). |
| 19 When an owner wants to release its reference, it calls unref(). When the | 19 When an owner wants to release its reference, it calls unref(). When the |
| 20 shared object's reference count goes to zero as the result of an unref() | 20 shared object's reference count goes to zero as the result of an unref() |
| 21 call, its (virtual) destructor is called. It is an error for the | 21 call, its (virtual) destructor is called. It is an error for the |
| 22 destructor to be called explicitly (or via the object going out of scope on | 22 destructor to be called explicitly (or via the object going out of scope on |
| 23 the stack or calling delete) if getRefCnt() > 1. | 23 the stack or calling delete) if getRefCnt() > 1. |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 178 |
| 179 /////////////////////////////////////////////////////////////////////////////// | 179 /////////////////////////////////////////////////////////////////////////////// |
| 180 | 180 |
| 181 template <typename T> struct SkTUnref { | 181 template <typename T> struct SkTUnref { |
| 182 void operator()(T* t) { t->unref(); } | 182 void operator()(T* t) { t->unref(); } |
| 183 }; | 183 }; |
| 184 | 184 |
| 185 /** | 185 /** |
| 186 * Utility class that simply unref's its argument in the destructor. | 186 * Utility class that simply unref's its argument in the destructor. |
| 187 */ | 187 */ |
| 188 template <typename T> class SkAutoTUnref { | 188 template <typename T> class SkAutoTUnref : public skstd::unique_ptr<T, SkTUnref<
T>> { |
| 189 public: | 189 public: |
| 190 explicit SkAutoTUnref(T* obj = nullptr) : fPtr(obj) {} | 190 explicit SkAutoTUnref(T* obj = nullptr) : skstd::unique_ptr<T, SkTUnref<T>>(
obj) {} |
| 191 | 191 |
| 192 void swap(SkAutoTUnref& other) { fPtr.swap(other.fPtr); } | 192 T* detach() { return this->release(); } |
| 193 | 193 operator T*() const { return this->get(); } |
| 194 T* get() const { return fPtr.get(); } | |
| 195 operator T* () const { return fPtr.get(); } | |
| 196 T* operator->() const { return fPtr.get(); } | |
| 197 | |
| 198 void reset(T* ptr = nullptr) { fPtr.reset(ptr); } | |
| 199 T* detach() { return fPtr.release(); } | |
| 200 T* release() { return fPtr.release(); } | |
| 201 | |
| 202 private: | |
| 203 std::unique_ptr<T, SkTUnref<T>> fPtr; | |
| 204 }; | 194 }; |
| 205 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because i
t's templated. :( | 195 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because i
t's templated. :( |
| 206 | 196 |
| 207 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { | 197 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { |
| 208 public: | 198 public: |
| 209 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} | 199 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} |
| 210 }; | 200 }; |
| 211 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) | 201 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) |
| 212 | 202 |
| 213 // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead o
f 8 or 16. | 203 // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead o
f 8 or 16. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 231 delete (const Derived*)this; | 221 delete (const Derived*)this; |
| 232 } | 222 } |
| 233 } | 223 } |
| 234 void deref() const { this->unref(); } | 224 void deref() const { this->unref(); } |
| 235 | 225 |
| 236 private: | 226 private: |
| 237 mutable int32_t fRefCnt; | 227 mutable int32_t fRefCnt; |
| 238 }; | 228 }; |
| 239 | 229 |
| 240 #endif | 230 #endif |
| OLD | NEW |