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 : public std::unique_ptr<T, SkTUnref<T> > { |
189 public: | 189 public: |
190 explicit SkAutoTUnref(T* obj = nullptr) : skstd::unique_ptr<T, SkTUnref<T>>( obj) {} | 190 explicit SkAutoTUnref(T* obj = nullptr) : std::unique_ptr<T, SkTUnref<T>>(ob j) {} |
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 | |
bungeman-skia
2015/12/02 22:27:13
Does adding something like
using inherited_t = st
| |
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; } | |
194 }; | 197 }; |
195 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because i t's templated. :( | 198 // Can't use the #define trick below to guard a bare SkAutoTUnref(...) because i t's templated. :( |
196 | 199 |
197 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { | 200 class SkAutoUnref : public SkAutoTUnref<SkRefCnt> { |
198 public: | 201 public: |
199 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} | 202 SkAutoUnref(SkRefCnt* obj) : SkAutoTUnref<SkRefCnt>(obj) {} |
200 }; | 203 }; |
201 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) | 204 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref) |
202 | 205 |
203 // This is a variant of SkRefCnt that's Not Virtual, so weighs 4 bytes instead o f 8 or 16. | 206 // 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; | 224 delete (const Derived*)this; |
222 } | 225 } |
223 } | 226 } |
224 void deref() const { this->unref(); } | 227 void deref() const { this->unref(); } |
225 | 228 |
226 private: | 229 private: |
227 mutable int32_t fRefCnt; | 230 mutable int32_t fRefCnt; |
228 }; | 231 }; |
229 | 232 |
230 #endif | 233 #endif |
OLD | NEW |