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 : public std::unique_ptr<T, SkTUnref<T>
> { | 188 template <typename T> class SkAutoTUnref : public skstd::unique_ptr<T, SkTUnref<
T>> { |
189 public: | 189 public: |
190 explicit SkAutoTUnref(T* obj = nullptr) : std::unique_ptr<T, SkTUnref<T>>(ob
j) {} | 190 explicit SkAutoTUnref(T* obj = nullptr) : skstd::unique_ptr<T, SkTUnref<T>>(
obj) {} |
191 | 191 |
192 T* detach() { return this->release(); } | 192 T* detach() { return this->release(); } |
193 operator T*() const { return this->get(); } | 193 operator T*() const { return this->get(); } |
194 | |
195 // unique_ptr's operator bool() is not always be explicit on Android. Make
sure this is. | |
196 explicit operator bool() const { return this->get() != nullptr; } | |
197 }; | 194 }; |
198 // 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. :( |
199 | 196 |
200 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { | 197 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { |
201 public: | 198 public: |
202 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} | 199 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} |
203 }; | 200 }; |
204 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) | 201 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) |
205 | 202 |
206 // 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... |
224 delete (const Derived*)this; | 221 delete (const Derived*)this; |
225 } | 222 } |
226 } | 223 } |
227 void deref() const { this->unref(); } | 224 void deref() const { this->unref(); } |
228 | 225 |
229 private: | 226 private: |
230 mutable int32_t fRefCnt; | 227 mutable int32_t fRefCnt; |
231 }; | 228 }; |
232 | 229 |
233 #endif | 230 #endif |
OLD | NEW |