8 #ifndef SkRefCnt_DEFINED
9 #define SkRefCnt_DEFINED
12 #include "include/private/base/SkDebug.h"
18 #include <type_traits>
41 SkASSERTF(this->getRefCnt() == 1,
"fRefCnt was %d", this->getRefCnt());
43 fRefCnt.store(0, std::memory_order_relaxed);
51 if (1 == fRefCnt.load(std::memory_order_acquire)) {
63 SkASSERT(this->getRefCnt() > 0);
65 (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed);
73 SkASSERT(this->getRefCnt() > 0);
75 if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) {
78 this->internal_dispose();
86 int32_t getRefCnt()
const {
87 return fRefCnt.load(std::memory_order_relaxed);
94 virtual void internal_dispose()
const {
96 SkASSERT(0 == this->getRefCnt());
97 fRefCnt.store(1, std::memory_order_relaxed);
104 friend class SkWeakRefCnt;
106 mutable std::atomic<int32_t> fRefCnt;
114 #ifdef SK_REF_CNT_MIXIN_INCLUDE
117 #include SK_REF_CNT_MIXIN_INCLUDE
121 #if defined(SK_BUILD_FOR_GOOGLE3)
123 void deref()
const { this->unref(); }
132 template <
typename T>
static inline T*
SkRef(T* obj) {
140 template <
typename T>
static inline T*
SkSafeRef(T* obj) {
159 template <
typename Derived>
165 int rc = fRefCnt.load(std::memory_order_relaxed);
166 SkASSERTF(rc == 1,
"NVRefCnt was %d", rc);
175 bool unique()
const {
return 1 == fRefCnt.load(std::memory_order_acquire); }
176 void ref()
const { (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed); }
178 if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) {
180 SkDEBUGCODE(fRefCnt.store(1, std::memory_order_relaxed));
181 delete (
const Derived*)
this;
192 int cnt = fRefCnt.load(std::memory_order_acquire);
194 SkASSERT(cnt >= threadIsolatedTestCnt);
195 return cnt > threadIsolatedTestCnt;
199 mutable std::atomic<int32_t> fRefCnt;
220 template <
typename T>
class SK_TRIVIAL_ABI
sk_sp {
224 constexpr
sk_sp() : fPtr(nullptr) {}
225 constexpr
sk_sp(std::nullptr_t) : fPtr(nullptr) {}
232 template <
typename U,
233 typename =
typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
242 template <
typename U,
243 typename =
typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
250 explicit sk_sp(T* obj) : fPtr(obj) {}
257 SkDEBUGCODE(fPtr =
nullptr);
273 template <
typename U,
274 typename =
typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
286 this->reset(that.release());
289 template <
typename U,
290 typename =
typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
292 this->reset(that.release());
297 SkASSERT(this->get() !=
nullptr);
301 explicit operator bool()
const {
return this->get() !=
nullptr; }
303 T*
get()
const {
return fPtr; }
332 swap(fPtr, that.fPtr);
346 return a.
get() == b.
get();
356 return a.
get() != b.
get();
359 return static_cast<bool>(a);
362 return static_cast<bool>(b);
365 template <
typename C,
typename CT,
typename T>
367 return os << sp.get();
370 template <
typename T,
typename... Args>
372 return sk_sp<T>(
new T(std::forward<Args>(args)...));
static T * SkSafeRef(T *obj)
Check if the argument is non-null, and if so, call obj->ref() and return obj.
Definition: SkRefCnt.h:140
static T * SkRef(T *obj)
Call obj->ref() and return obj.
Definition: SkRefCnt.h:132
bool operator!=(const sk_sp< T > &a, const sk_sp< U > &b)
Definition: SkRefCnt.h:355
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
static void SkSafeUnref(T *obj)
Check if the argument is non-null, and if so, call obj->unref()
Definition: SkRefCnt.h:149
sk_sp< T > sk_make_sp(Args &&... args)
Definition: SkRefCnt.h:371
auto operator<<(std::basic_ostream< C, CT > &os, const sk_sp< T > &sp) -> decltype(os<< sp.get())
Definition: SkRefCnt.h:366
bool operator==(const sk_sp< T > &a, const sk_sp< U > &b)
Definition: SkRefCnt.h:345
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:341
Definition: SkRefCnt.h:160
void unref() const
Definition: SkRefCnt.h:177
SkNVRefCnt()
Definition: SkRefCnt.h:162
void ref() const
Definition: SkRefCnt.h:176
bool unique() const
Definition: SkRefCnt.h:175
bool refCntGreaterThan(int32_t threadIsolatedTestCnt) const
Definition: SkRefCnt.h:191
void deref() const
Definition: SkRefCnt.h:184
~SkNVRefCnt()
Definition: SkRefCnt.h:163
SkRefCntBase is the base class for objects that may be shared by multiple objects.
Definition: SkRefCnt.h:31
SkRefCntBase()
Default construct, initializing the reference count to 1.
Definition: SkRefCnt.h:35
void ref() const
Increment the reference count.
Definition: SkRefCnt.h:62
virtual ~SkRefCntBase()
Destruct, asserting that the reference count is 1.
Definition: SkRefCnt.h:39
bool unique() const
May return true if the caller is the only owner.
Definition: SkRefCnt.h:50
void unref() const
Decrement the reference count.
Definition: SkRefCnt.h:72
Definition: SkRefCnt.h:119
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:220
T & operator*() const
Definition: SkRefCnt.h:296
void swap(sk_sp< T > &that)
Definition: SkRefCnt.h:330
sk_sp(T *obj)
Adopt the bare pointer into the newly created sk_sp.
Definition: SkRefCnt.h:250
sk_sp< T > & operator=(sk_sp< U > &&that)
Definition: SkRefCnt.h:291
std::true_type sk_is_trivially_relocatable
Definition: SkRefCnt.h:335
sk_sp(sk_sp< U > &&that)
Definition: SkRefCnt.h:244
T * get() const
Definition: SkRefCnt.h:303
sk_sp(const sk_sp< U > &that)
Definition: SkRefCnt.h:234
T * operator->() const
Definition: SkRefCnt.h:304
T * release()
Return the bare pointer, and set the internal object pointer to nullptr.
Definition: SkRefCnt.h:324
sk_sp< T > & operator=(const sk_sp< U > &that)
Definition: SkRefCnt.h:275
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:267
constexpr sk_sp(std::nullptr_t)
Definition: SkRefCnt.h:225
sk_sp< T > & operator=(std::nullptr_t)
Definition: SkRefCnt.h:260
constexpr sk_sp()
Definition: SkRefCnt.h:224
void reset(T *ptr=nullptr)
Adopt the new bare pointer, and call unref() on any previously held object (if not null).
Definition: SkRefCnt.h:310
sk_sp< T > & operator=(sk_sp< T > &&that)
Move the underlying object from the argument to the sk_sp.
Definition: SkRefCnt.h:285
T element_type
Definition: SkRefCnt.h:222
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:231
sk_sp(sk_sp< T > &&that)
Move the underlying object from the argument to the newly created sk_sp.
Definition: SkRefCnt.h:241
~sk_sp()
Calls unref() on the underlying object pointer.
Definition: SkRefCnt.h:255