11 namespace intrusive_ptr {
29 friend void Ref(
const T*);
31 friend void Unref(
const T*);
32 mutable uint64_t _references = 1;
36 inline void Ref(
const T* m) {
42 inline void Unref(
const T* m) {
43 if ( m && --m->_references == 0 )
73 using const_pointer =
const T*;
75 using element_type = T;
79 using const_reference =
const T&;
118 template<
class U,
class = std::enable_if_t<std::is_convertible_v<U*, T*>>>
119 IntrusivePtr(IntrusivePtr<U> other) noexcept : ptr_(other.release()) {
128 void swap(IntrusivePtr& other) noexcept { std::swap(ptr_, other.ptr_); }
130 friend void swap(IntrusivePtr& a, IntrusivePtr& b) noexcept {
132 swap(a.ptr_, b.ptr_);
140 pointer
release() noexcept {
return std::exchange(ptr_,
nullptr); }
147 pointer get() const noexcept {
150 #pragma GCC diagnostic push
151 #pragma GCC diagnostic ignored "-Wpragmas"
152 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
153 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
155 #pragma GCC diagnostic pop
158 pointer operator->() const noexcept {
return ptr_; }
160 reference operator*() const noexcept {
return *ptr_; }
162 bool operator!() const noexcept {
return ! ptr_; }
164 explicit operator bool() const noexcept {
return ptr_ !=
nullptr; }
167 pointer ptr_ =
nullptr;
178 template<
class T,
class... Ts>
190 template<
class T,
class U>
209 bool operator==(std::nullptr_t,
const IntrusivePtr<T>& x) {
217 bool operator!=(
const IntrusivePtr<T>& x, std::nullptr_t) {
218 return static_cast<bool>(x);
225 bool operator!=(std::nullptr_t,
const IntrusivePtr<T>& x) {
226 return static_cast<bool>(x);
235 bool operator==(
const IntrusivePtr<T>& x,
const T* y) {
243 bool operator==(
const T* x,
const IntrusivePtr<T>& y) {
251 bool operator!=(
const IntrusivePtr<T>& x,
const T* y) {
259 bool operator!=(
const T* x,
const IntrusivePtr<T>& y) {
271 template<
class T,
class U>
272 auto operator==(
const IntrusivePtr<T>& x,
const IntrusivePtr<U>& y) -> decltype(x.get() == y.get()) {
273 return x.get() == y.get();
279 template<
class T,
class U>
280 auto operator!=(
const IntrusivePtr<T>& x,
const IntrusivePtr<U>& y) -> decltype(x.get() != y.get()) {
281 return x.get() != y.get();
Definition: intrusive-ptr.h:67
IntrusivePtr(intrusive_ptr::NewRef, pointer raw_ptr) noexcept
Definition: intrusive-ptr.h:107
constexpr IntrusivePtr(intrusive_ptr::AdoptRef, pointer raw_ptr) noexcept
Definition: intrusive-ptr.h:97
pointer release() noexcept
Definition: intrusive-ptr.h:140
IntrusivePtr< T > make_intrusive(Ts &&... args)
Definition: intrusive-ptr.h:179
Definition: intrusive-ptr.h:26
IntrusivePtr< T > cast_intrusive(IntrusivePtr< U > p) noexcept
Definition: intrusive-ptr.h:191
Definition: intrusive-ptr.h:17
Definition: intrusive-ptr.h:23