 |
Skia
2D Graphics Library
|
Go to the documentation of this file.
8 #ifndef SkRefCnt_DEFINED
9 #define SkRefCnt_DEFINED
17 #include <type_traits>
40 SkASSERTF(this->getRefCnt() == 1,
"fRefCnt was %d", this->getRefCnt());
42 fRefCnt.store(0, std::memory_order_relaxed);
50 if (1 == fRefCnt.load(std::memory_order_acquire)) {
64 (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed);
74 if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) {
77 this->internal_dispose();
85 int32_t getRefCnt()
const {
86 return fRefCnt.load(std::memory_order_relaxed);
93 virtual void internal_dispose()
const {
96 fRefCnt.store(1, std::memory_order_relaxed);
103 friend class SkWeakRefCnt;
105 mutable std::atomic<int32_t> fRefCnt;
113 #ifdef SK_REF_CNT_MIXIN_INCLUDE
116 #include SK_REF_CNT_MIXIN_INCLUDE
120 #if defined(SK_BUILD_FOR_GOOGLE3)
122 void deref()
const { this->
unref(); }
131 template <
typename T>
static inline T*
SkRef(T* obj) {
139 template <
typename T>
static inline T*
SkSafeRef(T* obj) {
158 template <
typename Derived>
164 int rc = fRefCnt.load(std::memory_order_relaxed);
165 SkASSERTF(rc == 1,
"NVRefCnt was %d", rc);
174 bool unique()
const {
return 1 == fRefCnt.load(std::memory_order_acquire); }
175 void ref()
const { (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed); }
177 if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) {
179 SkDEBUGCODE(fRefCnt.store(1, std::memory_order_relaxed));
180 delete (
const Derived*)
this;
191 int cnt = fRefCnt.load(std::memory_order_acquire);
193 SkASSERT(cnt >= threadIsolatedTestCnt);
194 return cnt > threadIsolatedTestCnt;
198 mutable std::atomic<int32_t> fRefCnt;
219 constexpr
sk_sp() : fPtr(nullptr) {}
220 constexpr
sk_sp(std::nullptr_t) : fPtr(nullptr) {}
227 template <
typename U,
228 typename =
typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
237 template <
typename U,
238 typename =
typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
245 explicit sk_sp(T* obj) : fPtr(obj) {}
268 template <
typename U,
269 typename =
typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
281 this->
reset(that.release());
284 template <
typename U,
285 typename =
typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
287 this->
reset(that.release());
296 explicit operator bool()
const {
return this->
get() !=
nullptr; }
298 T*
get()
const {
return fPtr; }
327 swap(fPtr, that.fPtr);
339 return a.
get() == b.
get();
349 return a.
get() != b.
get();
352 return static_cast<bool>(a);
355 return static_cast<bool>(b);
358 template <
typename C,
typename CT,
typename T>
360 return os << sp.get();
363 template <
typename T,
typename... Args>
365 return sk_sp<T>(
new T(std::forward<Args>(args)...));
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:262
virtual ~SkRefCntBase()
Destruct, asserting that the reference count is 1.
Definition: SkRefCnt.h:38
sk_sp< T > & operator=(sk_sp< T > &&that)
Move the underlying object from the argument to the sk_sp.
Definition: SkRefCnt.h:280
void unref() const
Definition: SkRefCnt.h:176
bool operator==(const sk_sp< T > &a, const sk_sp< U > &b)
Definition: SkRefCnt.h:338
Definition: SkRefCnt.h:118
constexpr sk_sp(std::nullptr_t)
Definition: SkRefCnt.h:220
~SkNVRefCnt()
Definition: SkRefCnt.h:162
Definition: SkRefCnt.h:30
T *SK_WARN_UNUSED_RESULT release()
Return the bare pointer, and set the internal object pointer to nullptr.
Definition: SkRefCnt.h:319
sk_sp< T > & operator=(sk_sp< U > &&that)
Definition: SkRefCnt.h:286
Definition: SkRefCnt.h:159
bool unique() const
Definition: SkRefCnt.h:174
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:374
void reset(T *ptr=nullptr)
Adopt the new bare pointer, and call unref() on any previously held object (if not null).
Definition: SkRefCnt.h:305
void deref() const
Definition: SkRefCnt.h:183
SkRefCntBase()
Default construct, initializing the reference count to 1.
Definition: SkRefCnt.h:34
void ref() const
Increment the reference count.
Definition: SkRefCnt.h:61
static void SkSafeUnref(T *obj)
Check if the argument is non-null, and if so, call obj->unref()
Definition: SkRefCnt.h:148
void swap(sk_sp< T > &that)
Definition: SkRefCnt.h:325
static T * SkRef(T *obj)
Call obj->ref() and return obj.
Definition: SkRefCnt.h:131
bool operator!=(const sk_sp< T > &a, const sk_sp< U > &b)
Definition: SkRefCnt.h:348
sk_sp< T > sk_make_sp(Args &&... args)
Definition: SkRefCnt.h:364
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:215
static T * SkSafeRef(T *obj)
Check if the argument is non-null, and if so, call obj->ref() and return obj.
Definition: SkRefCnt.h:139
sk_sp(T *obj)
Adopt the bare pointer into the newly created sk_sp.
Definition: SkRefCnt.h:245
bool unique() const
May return true if the caller is the only owner.
Definition: SkRefCnt.h:49
SkNVRefCnt()
Definition: SkRefCnt.h:161
#define SkDEBUGCODE(...)
Definition: SkTypes.h:467
#define SkASSERTF(cond, fmt,...)
Definition: SkTypes.h:461
#define SK_API
Definition: SkTypes.h:181
#define SkASSERT(cond)
Definition: SkTypes.h:460
void unref() const
Decrement the reference count.
Definition: SkRefCnt.h:71
sk_sp(sk_sp< U > &&that)
Definition: SkRefCnt.h:239
~sk_sp()
Calls unref() on the underlying object pointer.
Definition: SkRefCnt.h:250
sk_sp(sk_sp< T > &&that)
Move the underlying object from the argument to the newly created sk_sp.
Definition: SkRefCnt.h:236
T * get() const
Definition: SkRefCnt.h:298
constexpr sk_sp()
Definition: SkRefCnt.h:219
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:334
sk_sp< T > & operator=(std::nullptr_t)
Definition: SkRefCnt.h:255
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:226
SkMarkerStack element_type
Definition: SkRefCnt.h:217
auto operator<<(std::basic_ostream< C, CT > &os, const sk_sp< T > &sp) -> decltype(os<< sp.get())
Definition: SkRefCnt.h:359
T * operator->() const
Definition: SkRefCnt.h:299
T & operator*() const
Definition: SkRefCnt.h:291
#define SK_WARN_UNUSED_RESULT
Definition: SkTypes.h:60
bool refCntGreaterThan(int32_t threadIsolatedTestCnt) const
Definition: SkRefCnt.h:190
sk_sp< T > & operator=(const sk_sp< U > &that)
Definition: SkRefCnt.h:270
sk_sp(const sk_sp< U > &that)
Definition: SkRefCnt.h:229
void ref() const
Definition: SkRefCnt.h:175