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" | |
13 #include "SkTypes.h" | 12 #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 : public skstd::unique_ptr<T, SkTUnref<
T>> { | 188 template <typename T> class SkAutoTUnref { |
189 public: | 189 public: |
190 explicit SkAutoTUnref(T* obj = nullptr) : skstd::unique_ptr<T, SkTUnref<T>>(
obj) {} | 190 explicit SkAutoTUnref(T* obj = nullptr) : fPtr(obj) {} |
191 | 191 |
192 T* detach() { return this->release(); } | 192 void swap(SkAutoTUnref& other) { fPtr.swap(other.fPtr); } |
193 operator T*() const { return this->get(); } | 193 |
| 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; |
194 }; | 204 }; |
195 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because i
t's templated. :( | 205 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because i
t's templated. :( |
196 | 206 |
197 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { | 207 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { |
198 public: | 208 public: |
199 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} | 209 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} |
200 }; | 210 }; |
201 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) | 211 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) |
202 | 212 |
203 // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead o
f 8 or 16. | 213 // 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... |
221 delete (const Derived*)this; | 231 delete (const Derived*)this; |
222 } | 232 } |
223 } | 233 } |
224 void deref() const { this->unref(); } | 234 void deref() const { this->unref(); } |
225 | 235 |
226 private: | 236 private: |
227 mutable int32_t fRefCnt; | 237 mutable int32_t fRefCnt; |
228 }; | 238 }; |
229 | 239 |
230 #endif | 240 #endif |
OLD | NEW |