56 #ifndef WPIUTIL_WPI_INTRUSIVEREFCNTPTR_H
57 #define WPIUTIL_WPI_INTRUSIVEREFCNTPTR_H
72 mutable unsigned RefCount = 0;
78 void Retain()
const { ++RefCount; }
80 void Release()
const {
81 assert(RefCount > 0 &&
"Reference count is already zero.");
83 delete static_cast<const Derived *
>(
this);
89 mutable std::atomic<int> RefCount;
95 void Retain()
const { RefCount.fetch_add(1, std::memory_order_relaxed); }
97 void Release()
const {
98 int NewRefCount = RefCount.fetch_sub(1, std::memory_order_acq_rel) - 1;
99 assert(NewRefCount >= 0 &&
"Reference count was already zero.");
100 if (NewRefCount == 0)
101 delete static_cast<const Derived *
>(
this);
126 static void retain(T *obj) { obj->Retain(); }
127 static void release(T *obj) { obj->Release(); }
140 using element_type = T;
164 T &operator*()
const {
return *Obj; }
165 T *operator->()
const {
return Obj; }
166 T *
get()
const {
return Obj; }
167 explicit operator bool()
const {
return Obj; }
180 void resetWithoutRelease() { Obj =
nullptr; }
196 template <
class T,
class U>
199 return A.get() == B.get();
202 template <
class T,
class U>
203 inline bool operator!=(
const IntrusiveRefCntPtr<T> &A,
204 const IntrusiveRefCntPtr<U> &B) {
205 return A.get() != B.get();
208 template <
class T,
class U>
209 inline bool operator==(
const IntrusiveRefCntPtr<T> &A, U *B) {
213 template <
class T,
class U>
214 inline bool operator!=(
const IntrusiveRefCntPtr<T> &A, U *B) {
218 template <
class T,
class U>
219 inline bool operator==(T *A,
const IntrusiveRefCntPtr<U> &B) {
223 template <
class T,
class U>
224 inline bool operator!=(T *A,
const IntrusiveRefCntPtr<U> &B) {
229 bool operator==(std::nullptr_t A,
const IntrusiveRefCntPtr<T> &B) {
234 bool operator==(
const IntrusiveRefCntPtr<T> &A, std::nullptr_t B) {
239 bool operator!=(std::nullptr_t A,
const IntrusiveRefCntPtr<T> &B) {
244 bool operator!=(
const IntrusiveRefCntPtr<T> &A, std::nullptr_t B) {
253 using SimpleType = T *;
261 using SimpleType = T *;
270 #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
Definition: IntrusiveRefCntPtr.h:136
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Definition: IntrusiveRefCntPtr.h:250
Class you can specialize to provide custom retain/release functionality for a type.
Definition: IntrusiveRefCntPtr.h:125
A thread-safe version of RefCountedBase.
Definition: IntrusiveRefCntPtr.h:88
A CRTP mixin class that adds reference counting to a type.
Definition: IntrusiveRefCntPtr.h:71