8 #ifndef SkRefCnt_DEFINED
9 #define SkRefCnt_DEFINED
11 #include "../private/SkTLogic.h"
16 #include <type_traits>
19 #define SK_SUPPORT_TRANSITION_TO_SP_INTERFACES
41 SkASSERTF(getRefCnt() == 1,
"fRefCnt was %d", getRefCnt());
43 fRefCnt.store(0, std::memory_order_relaxed);
50 return fRefCnt.load(std::memory_order_relaxed);
53 void validate()
const {
54 SkASSERT(getRefCnt() > 0);
62 if (1 == fRefCnt.load(std::memory_order_acquire)) {
74 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
79 SkASSERT(getRefCnt() >= 0);
81 SkASSERT(getRefCnt() > 0);
84 (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed);
92 SkASSERT(getRefCnt() > 0);
94 if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) {
97 this->internal_dispose();
108 SkASSERT(0 == getRefCnt());
109 fRefCnt.store(1, std::memory_order_relaxed);
117 this->internal_dispose_restore_refcnt_to_1();
123 friend class SkWeakRefCnt;
125 mutable std::atomic<int32_t> fRefCnt;
127 typedef SkNoncopyable INHERITED;
130 #ifdef SK_REF_CNT_MIXIN_INCLUDE
133 #include SK_REF_CNT_MIXIN_INCLUDE
139 void deref()
const { this->
unref(); }
150 #define SkRefCnt_SafeAssign(dst, src) \
152 if (src) src->ref(); \
153 if (dst) dst->unref(); \
160 template <
typename T>
static inline T* SkRef(T* obj) {
168 template <
typename T>
static inline T* SkSafeRef(T* obj) {
177 template <
typename T>
static inline void SkSafeUnref(T* obj) {
183 template<
typename T>
static inline void SkSafeSetNull(T*& obj) {
193 void operator()(T* t) { t->unref(); }
199 template <
typename T>
class SkAutoTUnref :
public std::unique_ptr<T, SkTUnref<T>> {
201 explicit SkAutoTUnref(T* obj =
nullptr) : std::unique_ptr<T, SkTUnref<T>>(obj) {}
203 operator T*()
const {
return this->
get(); }
205 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
207 T* detach() {
return this->release(); }
216 #define SkAutoUnref(...) SK_REQUIRE_LOCAL_VAR(SkAutoUnref)
220 template <
typename Derived>
224 ~
SkNVRefCnt() { SkASSERTF(1 == getRefCnt(),
"NVRefCnt was %d", getRefCnt()); }
231 bool unique()
const {
return 1 == fRefCnt.load(std::memory_order_acquire); }
232 void ref()
const { (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed); }
234 if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) {
236 SkDEBUGCODE(fRefCnt.store(1, std::memory_order_relaxed));
237 delete (
const Derived*)
this;
240 void deref()
const { this->unref(); }
243 mutable std::atomic<int32_t> fRefCnt;
244 int32_t getRefCnt()
const {
245 return fRefCnt.load(std::memory_order_relaxed);
264 constexpr
sk_sp() : fPtr(nullptr) {}
265 constexpr
sk_sp(std::nullptr_t) : fPtr(nullptr) {}
272 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
281 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
288 explicit sk_sp(T* obj) : fPtr(obj) {}
295 SkDEBUGCODE(fPtr =
nullptr);
298 sk_sp<T>& operator=(std::nullptr_t) { this->
reset();
return *
this; }
306 this->
reset(SkSafeRef(that.get()));
309 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
311 this->
reset(SkSafeRef(that.get()));
324 template <typename U, typename = skstd::enable_if_t<std::is_convertible<U*, T*>::value>>
330 T& operator*()
const {
331 SkASSERT(this->
get() !=
nullptr);
340 bool operator!()
const {
return this->
get() ==
nullptr; }
342 T*
get()
const {
return fPtr; }
343 T* operator->()
const {
return fPtr; }
371 swap(fPtr, that.fPtr);
382 template <
typename T,
typename U>
inline bool operator==(
const sk_sp<T>& a,
const sk_sp<U>& b) {
383 return a.get() == b.get();
385 template <
typename T>
inline bool operator==(
const sk_sp<T>& a, std::nullptr_t) {
388 template <
typename T>
inline bool operator==(std::nullptr_t,
const sk_sp<T>& b) {
392 template <
typename T,
typename U>
inline bool operator!=(
const sk_sp<T>& a,
const sk_sp<U>& b) {
393 return a.get() != b.get();
395 template <
typename T>
inline bool operator!=(
const sk_sp<T>& a, std::nullptr_t) {
396 return static_cast<bool>(a);
398 template <
typename T>
inline bool operator!=(std::nullptr_t,
const sk_sp<T>& b) {
399 return static_cast<bool>(b);
402 template <
typename T,
typename U>
inline bool operator<(const sk_sp<T>& a,
const sk_sp<U>& b) {
406 return std::less<skstd::common_type_t<T*, U*>>()(a.get(), b.get());
408 template <
typename T>
inline bool operator<(const sk_sp<T>& a, std::nullptr_t) {
409 return std::less<T*>()(a.get(),
nullptr);
411 template <
typename T>
inline bool operator<(std::nullptr_t, const sk_sp<T>& b) {
412 return std::less<T*>()(
nullptr, b.get());
415 template <
typename T,
typename U>
inline bool operator<=(const sk_sp<T>& a,
const sk_sp<U>& b) {
418 template <
typename T>
inline bool operator<=(const sk_sp<T>& a, std::nullptr_t) {
419 return !(
nullptr < a);
421 template <
typename T>
inline bool operator<=(std::nullptr_t, const sk_sp<T>& b) {
422 return !(b <
nullptr);
425 template <
typename T,
typename U>
inline bool operator>(
const sk_sp<T>& a,
const sk_sp<U>& b) {
428 template <
typename T>
inline bool operator>(
const sk_sp<T>& a, std::nullptr_t) {
431 template <
typename T>
inline bool operator>(std::nullptr_t,
const sk_sp<T>& b) {
435 template <
typename T,
typename U>
inline bool operator>=(
const sk_sp<T>& a,
const sk_sp<U>& b) {
438 template <
typename T>
inline bool operator>=(
const sk_sp<T>& a, std::nullptr_t) {
439 return !(a <
nullptr);
441 template <
typename T>
inline bool operator>=(std::nullptr_t,
const sk_sp<T>& b) {
442 return !(
nullptr < b);
445 template <
typename T,
typename... Args>
446 sk_sp<T> sk_make_sp(Args&&... args) {
447 return sk_sp<T>(
new T(std::forward<Args>(args)...));
450 #ifdef SK_SUPPORT_TRANSITION_TO_SP_INTERFACES
460 template <
typename T>
sk_sp<T> sk_ref_sp(T* obj) {
T *SK_WARN_UNUSED_RESULT release()
Return the bare pointer, and set the internal object pointer to nullptr.
Definition: SkRefCnt.h:363
void reset(T *ptr=nullptr)
Adopt the new bare pointer, and call unref() on any previously held object (if not null)...
Definition: SkRefCnt.h:349
sk_sp(T *obj)
Adopt the bare pointer into the newly created sk_sp.
Definition: SkRefCnt.h:288
SkPathEffect is the base class for objects in the SkPaint that affect the geometry of a drawing primi...
Definition: SkPathEffect.h:29
Definition: SkRefCnt.h:212
void internal_dispose_restore_refcnt_to_1() const
Allow subclasses to call this if they've overridden internal_dispose so they can reset fRefCnt before...
Definition: SkRefCnt.h:107
int32_t getRefCnt() const
Return the reference count.
Definition: SkRefCnt.h:49
Definition: SkRefCnt.h:135
sk_sp< T > & operator=(sk_sp< T > &&that)
Move the underlying object from the argument to the sk_sp.
Definition: SkRefCnt.h:320
void unref() const
Decrement the reference count.
Definition: SkRefCnt.h:91
bool unique() const
May return true if the caller is the only owner.
Definition: SkRefCnt.h:61
void ref() const
Increment the reference count.
Definition: SkRefCnt.h:73
sk_sp(sk_sp< T > &&that)
Move the underlying object from the argument to the newly created sk_sp.
Definition: SkRefCnt.h:280
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:258
Utility class that simply unref's its argument in the destructor.
Definition: SkRefCnt.h:199
SkPathEffect *sk_sp::* unspecified_bool_type
Supports safe bool idiom.
Definition: SkRefCnt.h:260
Definition: SkRefCnt.h:192
SkRefCntBase()
Default construct, initializing the reference count to 1.
Definition: SkRefCnt.h:35
sk_sp(const sk_sp< T > &that)
Shares the underlying object by calling ref(), so that both the argument and the newly created sk_sp ...
Definition: SkRefCnt.h:271
virtual void internal_dispose() const
Called when the ref count goes to 0.
Definition: SkRefCnt.h:116
virtual ~SkRefCntBase()
Destruct, asserting that the reference count is 1.
Definition: SkRefCnt.h:39
~sk_sp()
Calls unref() on the underlying object pointer.
Definition: SkRefCnt.h:293
SkRefCntBase is the base class for objects that may be shared by multiple objects.
Definition: SkRefCnt.h:31
Definition: SkRefCnt.h:221
sk_sp< T > & operator=(const sk_sp< T > &that)
Shares the underlying object referenced by the argument by calling ref() on it.
Definition: SkRefCnt.h:305