YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
intrusive_ptr.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <yaclib/util/ref.hpp>
4
5#include <type_traits>
6
7namespace yaclib {
8
9struct NoRefTag final {};
10
11/**
12 * A intrusive pointer to objects with an embedded reference count
13 *
14 * https://www.boost.org/doc/libs/1_77_0/libs/smart_ptr/doc/html/smart_ptr.html#intrusive_ptr
15 */
16template <typename T>
18 static_assert(std::is_base_of_v<IRef, T>, "T must be derived class of IRef");
19
20 public:
21 IntrusivePtr() noexcept;
22
23 IntrusivePtr(T* other) noexcept;
25 IntrusivePtr(const IntrusivePtr& other) noexcept;
26
27 IntrusivePtr& operator=(T* other) noexcept;
29 IntrusivePtr& operator=(const IntrusivePtr& other) noexcept;
30
31 template <typename U>
33 template <typename U>
34 IntrusivePtr(const IntrusivePtr<U>& other) noexcept;
35
36 template <typename U>
38 template <typename U>
40
42
43 T* Get() const noexcept;
44 T* Release() noexcept;
45
47
49 T* operator->() const noexcept;
50
52
54 void Reset(NoRefTag, T* other) noexcept;
55
56 private:
59
60 T* _ptr;
61};
62
65 return lhs.Get() == rhs.Get();
66}
67
68template <typename T, typename U>
69inline bool operator!=(const IntrusivePtr<T>& lhs, const IntrusivePtr<U>& rhs) noexcept {
70 return lhs.Get() != rhs.Get();
71}
72
73template <typename T, typename U>
74inline bool operator==(const IntrusivePtr<T>& lhs, U* rhs) noexcept {
75 return lhs.Get() == rhs;
76}
77
78template <typename T, typename U>
79inline bool operator!=(const IntrusivePtr<T>& lhs, U* rhs) noexcept {
80 return lhs.Get() != rhs;
81}
82
83template <typename T, typename U>
84inline bool operator==(T* lhs, const IntrusivePtr<U>& rhs) noexcept {
85 return lhs == rhs.Get();
86}
87
88template <typename T, typename U>
89inline bool operator!=(T* lhs, const IntrusivePtr<U>& rhs) noexcept {
90 return lhs != rhs.Get();
91}
92
93template <typename T>
94inline bool operator==(const IntrusivePtr<T>& lhs, std::nullptr_t) noexcept {
95 return lhs.Get() == nullptr;
96}
97
98template <typename T>
99inline bool operator==(std::nullptr_t, const IntrusivePtr<T>& rhs) noexcept {
100 return rhs.Get() == nullptr;
101}
102
103template <typename T>
104inline bool operator!=(const IntrusivePtr<T>& lhs, std::nullptr_t) noexcept {
105 return lhs.Get() != nullptr;
106}
107
108template <typename T>
109inline bool operator!=(std::nullptr_t, const IntrusivePtr<T>& rhs) noexcept {
110 return rhs.Get() != nullptr;
111}
112
113template <typename T>
114inline bool operator<(const IntrusivePtr<T>& lhs, const IntrusivePtr<T>& rhs) noexcept {
115 return lhs.Get() < rhs.Get();
116}
117
118} // namespace yaclib
119
A intrusive pointer to objects with an embedded reference count.
friend class IntrusivePtr
IntrusivePtr & operator=(IntrusivePtr< U > &&other) noexcept
void Reset(NoRefTag, T *other) noexcept
IntrusivePtr & operator=(const IntrusivePtr< U > &other) noexcept
void Swap(IntrusivePtr &other) noexcept
T * Get() const noexcept
IntrusivePtr & operator=(T *other) noexcept
bool operator<(const IntrusivePtr< T > &lhs, const IntrusivePtr< T > &rhs) noexcept
bool operator==(const IntrusivePtr< T > &lhs, const IntrusivePtr< U > &rhs) noexcept
bool operator!=(const IntrusivePtr< T > &lhs, const IntrusivePtr< U > &rhs) noexcept
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25