19 #ifndef LIB_CS_INTRUSIVE_POINTER_H
20 #define LIB_CS_INTRUSIVE_POINTER_H
27 enum class CsIntrusiveAction {
38 mutable std::atomic<std::size_t> m_count = 0;
40 void cs_inc_ref_count()
const noexcept {
44 void cs_dec_ref_count(CsIntrusiveAction action)
const {
45 std::size_t old_count = m_count.fetch_sub(1);
47 if (action != CsIntrusiveAction::NoDelete) {
54 std::size_t cs_get_ref_count()
const {
55 return m_count.load();
61 class CsIntrusiveBase_CM
64 CsIntrusiveBase_CM() =
default;
66 CsIntrusiveBase_CM(
const CsIntrusiveBase_CM &other)
noexcept {
70 CsIntrusiveBase_CM &operator=(
const CsIntrusiveBase_CM &other)
noexcept {
75 CsIntrusiveBase_CM(CsIntrusiveBase_CM &&other)
noexcept {
79 CsIntrusiveBase_CM &operator=(CsIntrusiveBase_CM &&other)
noexcept {
84 virtual ~CsIntrusiveBase_CM() =
default;
87 mutable std::atomic<std::size_t> m_count = 0;
89 void cs_inc_ref_count() const noexcept {
93 void cs_dec_ref_count(CsIntrusiveAction action)
const {
94 std::size_t old_count = m_count.fetch_sub(1);
96 if (action != CsIntrusiveAction::NoDelete) {
103 std::size_t cs_get_ref_count()
const {
104 return m_count.load();
107 friend class CsIntrusiveDefaultPolicy;
113 template <
typename T>
115 ptr->cs_inc_ref_count();
118 template <
typename T>
119 static void dec_ref_count(
const T *ptr, CsIntrusiveAction action = CsIntrusiveAction::Normal) {
120 ptr->cs_dec_ref_count(action);
123 template <
typename T>
125 return ptr->cs_get_ref_count();
129 template <
typename T,
typename Policy = CsIntrusiveDefaultPolicy>
149 template <
typename U>
155 if (m_ptr !=
nullptr) {
156 Policy::inc_ref_count(m_ptr);
162 if (m_ptr !=
nullptr) {
163 Policy::dec_ref_count(m_ptr);
171 if (m_ptr !=
nullptr) {
172 Policy::inc_ref_count(m_ptr);
177 return *
this = other.m_ptr;
184 other.m_ptr =
nullptr;
188 if (m_ptr == other.m_ptr) {
192 if (m_ptr !=
nullptr) {
193 Policy::dec_ref_count(m_ptr);
197 std::swap(m_ptr, other.m_ptr);
204 Policy::inc_ref_count(p);
207 if (m_ptr !=
nullptr) {
208 Policy::dec_ref_count(m_ptr);
217 template <
typename U>
221 if (m_ptr !=
nullptr) {
222 Policy::inc_ref_count(m_ptr);
226 template <
typename U>
232 template <
typename U>
239 template <
typename U>
241 if (m_ptr == p.m_ptr) {
245 if (m_ptr !=
nullptr) {
246 Policy::dec_ref_count(m_ptr);
264 return m_ptr ==
nullptr;
267 operator bool()
const {
268 return m_ptr !=
nullptr;
286 return m_ptr ==
nullptr;
292 Policy::dec_ref_count(m_ptr, CsIntrusiveAction::NoDelete);
305 if (m_ptr !=
nullptr) {
306 Policy::dec_ref_count(m_ptr);
312 template <
typename U>
318 std::swap(m_ptr, other.m_ptr);
322 if (m_ptr ==
nullptr) {
326 return Policy::get_ref_count(m_ptr);
333 template <
typename U,
typename OtherPolicy>
337 template <
typename T,
typename... Args>
344 template <
typename T1,
typename T2>
347 return ptr1.get() == ptr2.get();
350 template <
typename T1,
typename T2>
353 return ptr1.get() == ptr2;
356 template <
typename T1,
typename T2>
359 return ptr1 == ptr2.get();
362 template <
typename T>
365 return ptr1.get() ==
nullptr;
368 template <
typename T>
371 return nullptr == ptr2.get();
375 template <
typename T1,
typename T2>
378 return ptr1.get() != ptr2.get();
381 template <
typename T1,
typename T2>
384 return ptr1.get() != ptr2;
387 template <
typename T1,
typename T2>
390 return ptr1 != ptr2.get();
393 template <
typename T>
396 return ptr1.get() !=
nullptr;
399 template <
typename T>
402 return nullptr != ptr2.get();
406 template <
typename T1,
typename T2>
409 return ptr1.get() < ptr2.get();
412 template <
typename T1,
typename T2>
413 bool operator<=(
const CsIntrusivePointer<T1> &ptr1,
const CsIntrusivePointer<T2> &ptr2)
noexcept
415 return ptr1.get() <= ptr2.get();
418 template <
typename T1,
typename T2>
419 bool operator>(
const CsIntrusivePointer<T1> &ptr1,
const CsIntrusivePointer<T2> &ptr2)
noexcept
421 return ptr1.get() > ptr2.get();
424 template <
typename T1,
typename T2>
425 bool operator>=(
const CsIntrusivePointer<T1> &ptr1,
const CsIntrusivePointer<T2> &ptr2)
noexcept
427 return ptr1.get() >= ptr2.get();
430 template <
typename T>
437 template <
typename T,
typename U>
443 template <
typename T,
typename U>
449 template <
typename T,
typename U>
CsIntrusivePointer< T > static_pointer_cast(const CsIntrusivePointer< U > &ptr)
Definition: cs_intrusive_pointer.h:450
bool operator>=(const CsIntrusivePointer< T1 > &ptr1, const CsIntrusivePointer< T2 > ptr2)
bool operator>(const CsIntrusivePointer< T1 > &ptr1, const CsIntrusivePointer< T2 > ptr2)
bool operator<=(const CsIntrusivePointer< T1 > &ptr1, const CsIntrusivePointer< T2 > ptr2)
bool operator<(const CsIntrusivePointer< T1 > &ptr1, const CsIntrusivePointer< T2 > ptr2)
void reset(U *p)
Definition: cs_intrusive_pointer.h:313
bool is_null() const noexcept
Definition: cs_intrusive_pointer.h:285
bool operator==(const CsIntrusivePointer< T1 > &ptr1, const CsIntrusivePointer< T2 > &ptr2) noexcept
Definition: cs_intrusive_pointer.h:345
Pointer get() const noexcept
Definition: cs_intrusive_pointer.h:280
void reset()
Definition: cs_intrusive_pointer.h:304
CsIntrusivePointer< T > const_pointer_cast(const CsIntrusivePointer< U > &ptr)
Definition: cs_intrusive_pointer.h:438
void clear() noexcept
Definition: cs_intrusive_pointer.h:272
constexpr CsIntrusivePointer() noexcept
Definition: cs_intrusive_pointer.h:139
std::size_t use_count() const noexcept
Definition: cs_intrusive_pointer.h:321
element_type ElementType
Definition: cs_intrusive_pointer.h:137
CsIntrusivePointer & operator=(const CsIntrusivePointer< U > &p)
Definition: cs_intrusive_pointer.h:227
pointer Pointer
Definition: cs_intrusive_pointer.h:136
CsIntrusivePointer(const CsIntrusivePointer &other)
Definition: cs_intrusive_pointer.h:168
Pointer data() const noexcept
Definition: cs_intrusive_pointer.h:276
T * pointer
Definition: cs_intrusive_pointer.h:133
Namespace for the CsPointer library.
T * operator->() const noexcept
Definition: cs_intrusive_pointer.h:259
static void dec_ref_count(const T *ptr, CsIntrusiveAction action=CsIntrusiveAction::Normal)
Definition: cs_intrusive_pointer.h:119
CsIntrusivePointer< T > dynamic_pointer_cast(const CsIntrusivePointer< U > &ptr)
Definition: cs_intrusive_pointer.h:444
static void inc_ref_count(const T *ptr) noexcept
Definition: cs_intrusive_pointer.h:114
CsIntrusivePointer & operator=(T *p)
Definition: cs_intrusive_pointer.h:201
T element_type
Definition: cs_intrusive_pointer.h:134
CsIntrusivePointer(CsIntrusivePointer &&other) noexcept
Definition: cs_intrusive_pointer.h:181
virtual ~CsIntrusiveBase() = default
constexpr CsIntrusivePointer(std::nullptr_t) noexcept
Definition: cs_intrusive_pointer.h:144
bool operator!=(const CsIntrusivePointer< T1 > &ptr1, const CsIntrusivePointer< T2 > &ptr2) noexcept
Definition: cs_intrusive_pointer.h:376
Default implementation for incrementing or decrementing the reference count for CsIntrusivePointer...
Definition: cs_intrusive_pointer.h:110
Implements reference count functions for CsIntrusivePointer.
Definition: cs_intrusive_pointer.h:32
Pointer release_if() noexcept
Definition: cs_intrusive_pointer.h:289
CsIntrusivePointer(U *p)
Definition: cs_intrusive_pointer.h:150
T & operator*() const noexcept
Definition: cs_intrusive_pointer.h:255
~CsIntrusivePointer()
Definition: cs_intrusive_pointer.h:160
static std::size_t get_ref_count(const T *ptr) noexcept
Definition: cs_intrusive_pointer.h:124
CsIntrusivePointer & operator=(const CsIntrusivePointer &other)
Definition: cs_intrusive_pointer.h:176
CsIntrusivePointer & operator=(CsIntrusivePointer &&other) noexcept
Definition: cs_intrusive_pointer.h:187
CsIntrusivePointer(const CsIntrusivePointer< U > &p) noexcept
Definition: cs_intrusive_pointer.h:218
CsIntrusivePointer(CsIntrusivePointer< U > &&p) noexcept
Definition: cs_intrusive_pointer.h:233
CsIntrusivePointer< T > make_intrusive(Args &&...args)
Definition: cs_intrusive_pointer.h:338
Manages a pointer to an object, the reference count is stored in the object.
Definition: cs_intrusive_pointer.h:130
bool operator!() const noexcept
Definition: cs_intrusive_pointer.h:263
void swap(CsIntrusivePointer &other) noexcept
Definition: cs_intrusive_pointer.h:317
CsIntrusivePointer & operator=(CsIntrusivePointer< U > &&p)
Definition: cs_intrusive_pointer.h:240