aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libs/kstd/kstd/bits/shared_ptr.hpp196
1 files changed, 98 insertions, 98 deletions
diff --git a/libs/kstd/kstd/bits/shared_ptr.hpp b/libs/kstd/kstd/bits/shared_ptr.hpp
index f4aed34e..03c107c0 100644
--- a/libs/kstd/kstd/bits/shared_ptr.hpp
+++ b/libs/kstd/kstd/bits/shared_ptr.hpp
@@ -48,19 +48,19 @@ namespace kstd
* @brief Constructs a null weak_ptr.
*/
weak_ptr() noexcept
- : pointer(nullptr)
- , control(nullptr)
+ : m_storage(nullptr)
+ , m_control_block(nullptr)
{}
template<typename U>
requires(std::is_convertible_v<U *, T *>)
weak_ptr(shared_ptr<U> const & other)
- : pointer(other.pointer)
- , control(other.control)
+ : m_storage(other.m_storage)
+ , m_control_block(other.m_control_block)
{
- if (control != nullptr)
+ if (m_control_block != nullptr)
{
- ++(control->weak_count);
+ ++(m_control_block->weak_count);
}
}
@@ -68,12 +68,12 @@ namespace kstd
* @brief Copy constructor. Constructs a weak_ptr which shares ownership of the object managed by other.
*/
weak_ptr(weak_ptr const & other)
- : pointer(other.pointer)
- , control(other.control)
+ : m_storage(other.m_storage)
+ , m_control_block(other.m_control_block)
{
- if (control != nullptr)
+ if (m_control_block != nullptr)
{
- ++(control->weak_count);
+ ++(m_control_block->weak_count);
}
}
@@ -81,11 +81,11 @@ namespace kstd
* @brief Move constructor. Constructs a weak_ptr which takes ownership of the object managed by other.
*/
weak_ptr(weak_ptr && other) noexcept
- : pointer(other.pointer)
- , control(other.control)
+ : m_storage(other.m_storage)
+ , m_control_block(other.m_control_block)
{
- other.pointer = nullptr;
- other.control = nullptr;
+ other.m_storage = nullptr;
+ other.m_control_block = nullptr;
}
/**
@@ -96,11 +96,11 @@ namespace kstd
if (this != &other)
{
cleanup();
- pointer = other.pointer;
- control = other.control;
- if (control != nullptr)
+ m_storage = other.m_storage;
+ m_control_block = other.m_control_block;
+ if (m_control_block != nullptr)
{
- ++(control->weak_count);
+ ++(m_control_block->weak_count);
}
}
@@ -115,10 +115,10 @@ namespace kstd
if (this != &other)
{
cleanup();
- pointer = other.pointer;
- control = other.control;
- other.pointer = nullptr;
- other.control = nullptr;
+ m_storage = other.m_storage;
+ m_control_block = other.m_control_block;
+ other.m_storage = nullptr;
+ other.m_control_block = nullptr;
}
return *this;
@@ -144,17 +144,17 @@ namespace kstd
private:
auto cleanup() -> void
{
- if (control != nullptr)
+ if (m_control_block != nullptr)
{
- if (--(control->weak_count) == 0)
+ if (--(m_control_block->weak_count) == 0)
{
- delete control;
+ delete m_control_block;
}
}
}
- T * pointer;
- shared_control_block * control;
+ T * m_storage;
+ shared_control_block * m_control_block;
};
/**
@@ -178,7 +178,7 @@ namespace kstd
*/
auto shared_from_this() -> shared_ptr<T>
{
- return shared_ptr<T>(weak_this);
+ return shared_ptr<T>(m_weak_this);
}
/**
@@ -186,7 +186,7 @@ namespace kstd
*/
auto shared_from_this() const -> shared_ptr<T const>
{
- return shared_ptr<T const>(weak_this);
+ return shared_ptr<T const>(m_weak_this);
}
private:
@@ -197,10 +197,10 @@ namespace kstd
void internal_assign_ptr(shared_ptr<T> const & ptr) const
{
- weak_this = ptr;
+ m_weak_this = ptr;
}
- mutable weak_ptr<T> weak_this{}; ///< Weak pointer to the object, used for shared_from_this functionality.
+ mutable weak_ptr<T> m_weak_this{}; ///< Weak pointer to the object, used for shared_from_this functionality.
};
/**
@@ -228,16 +228,16 @@ namespace kstd
* @brief Construct an empty shared_ptr.
*/
shared_ptr() noexcept
- : pointer(nullptr)
- , control(nullptr)
+ : m_storage(nullptr)
+ , m_control_block(nullptr)
{}
/**
* @brief Construct an empty shared_ptr from nullptr.
*/
shared_ptr(std::nullptr_t) noexcept
- : pointer(nullptr)
- , control(nullptr)
+ : m_storage(nullptr)
+ , m_control_block(nullptr)
{}
/**
@@ -248,8 +248,8 @@ namespace kstd
template<typename U>
requires(std::is_convertible_v<U *, T *>)
explicit shared_ptr(U * pointer = nullptr)
- : pointer(pointer)
- , control(pointer != nullptr ? new shared_control_block() : nullptr)
+ : m_storage(pointer)
+ , m_control_block(pointer != nullptr ? new shared_control_block() : nullptr)
{
assign_enable_shared_from_this(pointer);
}
@@ -263,20 +263,20 @@ namespace kstd
template<typename U>
requires(std::is_convertible_v<U *, T *>)
explicit shared_ptr(weak_ptr<U> const & other)
- : pointer(nullptr)
- , control(nullptr)
+ : m_storage(nullptr)
+ , m_control_block(nullptr)
{
- if (other.control)
+ if (other.m_control_block)
{
- auto count = other.control->shared_count.load(std::memory_order::relaxed);
- while (count != 0 && !other.control->shared_count.compare_exchange_weak(
+ auto count = other.m_control_block->shared_count.load(std::memory_order::relaxed);
+ while (count != 0 && !other.m_control_block->shared_count.compare_exchange_weak(
count, count + 1, std::memory_order::acquire, std::memory_order::relaxed))
;
if (count != 0)
{
- pointer = other.pointer;
- control = other.control;
+ m_storage = other.m_storage;
+ m_control_block = other.m_control_block;
}
}
}
@@ -287,12 +287,12 @@ namespace kstd
* @param other The shared_ptr to copy from.
*/
shared_ptr(shared_ptr const & other)
- : pointer(other.pointer)
- , control(other.control)
+ : m_storage(other.m_storage)
+ , m_control_block(other.m_control_block)
{
- if (control != nullptr)
+ if (m_control_block != nullptr)
{
- ++(control->shared_count);
+ ++(m_control_block->shared_count);
}
}
@@ -305,12 +305,12 @@ namespace kstd
template<typename U>
requires(std::is_convertible_v<U *, T *>)
shared_ptr(shared_ptr<U> const & other)
- : pointer(other.pointer)
- , control(other.control)
+ : m_storage(other.m_storage)
+ , m_control_block(other.m_control_block)
{
- if (control != nullptr)
+ if (m_control_block != nullptr)
{
- ++(control->shared_count);
+ ++(m_control_block->shared_count);
}
}
@@ -320,11 +320,11 @@ namespace kstd
* @param other The shared_ptr to move from.
*/
shared_ptr(shared_ptr && other) noexcept
- : pointer(other.pointer)
- , control(other.control)
+ : m_storage(other.m_storage)
+ , m_control_block(other.m_control_block)
{
- other.pointer = nullptr;
- other.control = nullptr;
+ other.m_storage = nullptr;
+ other.m_control_block = nullptr;
}
/**
@@ -336,11 +336,11 @@ namespace kstd
template<typename U>
requires(std::is_convertible_v<U *, T *>)
shared_ptr(shared_ptr<U> && other) noexcept
- : pointer(other.pointer)
- , control(other.control)
+ : m_storage(other.m_storage)
+ , m_control_block(other.m_control_block)
{
- other.pointer = nullptr;
- other.control = nullptr;
+ other.m_storage = nullptr;
+ other.m_control_block = nullptr;
}
/**
@@ -356,12 +356,12 @@ namespace kstd
if (this != &other)
{
cleanup();
- pointer = other.pointer;
- control = other.control;
+ m_storage = other.m_storage;
+ m_control_block = other.m_control_block;
- if (control != nullptr)
+ if (m_control_block != nullptr)
{
- ++(control->shared_count);
+ ++(m_control_block->shared_count);
}
}
@@ -380,12 +380,12 @@ namespace kstd
auto operator=(shared_ptr<U> const & other) -> shared_ptr &
{
cleanup();
- pointer = other.pointer;
- control = other.control;
+ m_storage = other.m_storage;
+ m_control_block = other.m_control_block;
- if (control != nullptr)
+ if (m_control_block != nullptr)
{
- ++(control->shared_count);
+ ++(m_control_block->shared_count);
}
return *this;
@@ -403,10 +403,10 @@ namespace kstd
if (this != &other)
{
cleanup();
- pointer = other.pointer;
- control = other.control;
- other.pointer = nullptr;
- other.control = nullptr;
+ m_storage = other.m_storage;
+ m_control_block = other.m_control_block;
+ other.m_storage = nullptr;
+ other.m_control_block = nullptr;
}
return *this;
@@ -424,10 +424,10 @@ namespace kstd
auto operator=(shared_ptr<U> && other) noexcept -> shared_ptr &
{
cleanup();
- pointer = other.pointer;
- control = other.control;
- other.pointer = nullptr;
- other.control = nullptr;
+ m_storage = other.m_storage;
+ m_control_block = other.m_control_block;
+ other.m_storage = nullptr;
+ other.m_control_block = nullptr;
return *this;
}
@@ -438,8 +438,8 @@ namespace kstd
auto operator=(std::nullptr_t) noexcept -> shared_ptr &
{
cleanup();
- pointer = nullptr;
- control = nullptr;
+ m_storage = nullptr;
+ m_control_block = nullptr;
return *this;
}
@@ -459,8 +459,8 @@ namespace kstd
void reset(T * ptr = nullptr)
{
cleanup();
- pointer = ptr;
- control = ptr != nullptr ? new shared_control_block() : nullptr;
+ m_storage = ptr;
+ m_control_block = ptr != nullptr ? new shared_control_block() : nullptr;
assign_enable_shared_from_this(ptr);
}
@@ -472,8 +472,8 @@ namespace kstd
*/
void swap(shared_ptr & other)
{
- std::swap(pointer, other.pointer);
- std::swap(control, other.control);
+ std::swap(m_storage, other.m_storage);
+ std::swap(m_control_block, other.m_control_block);
}
/**
@@ -483,7 +483,7 @@ namespace kstd
*/
[[nodiscard]] auto operator*() const -> T &
{
- return *pointer;
+ return *m_storage;
}
/**
@@ -493,7 +493,7 @@ namespace kstd
*/
[[nodiscard]] auto operator->() const -> T *
{
- return pointer;
+ return m_storage;
}
/**
@@ -503,7 +503,7 @@ namespace kstd
*/
[[nodiscard]] auto get() const -> T *
{
- return pointer;
+ return m_storage;
}
/**
@@ -519,9 +519,9 @@ namespace kstd
*/
[[nodiscard]] auto use_count() const -> std::size_t
{
- if (control != nullptr)
+ if (m_control_block != nullptr)
{
- return control->shared_count;
+ return m_control_block->shared_count;
}
return 0;
@@ -534,7 +534,7 @@ namespace kstd
*/
[[nodiscard]] explicit operator bool() const
{
- return pointer != nullptr;
+ return m_storage != nullptr;
}
/**
@@ -542,7 +542,7 @@ namespace kstd
*/
[[nodiscard]] auto operator==(std::nullptr_t) const -> bool
{
- return pointer == nullptr;
+ return m_storage == nullptr;
}
/**
@@ -550,7 +550,7 @@ namespace kstd
*/
[[nodiscard]] friend auto operator==(std::nullptr_t, shared_ptr const & ptr) -> bool
{
- return ptr.pointer == nullptr;
+ return ptr.m_storage == nullptr;
}
private:
@@ -579,23 +579,23 @@ namespace kstd
*/
auto cleanup() -> void
{
- if (control != nullptr)
+ if (m_control_block != nullptr)
{
- if (--(control->shared_count) == 0)
+ if (--(m_control_block->shared_count) == 0)
{
- delete pointer;
- pointer = nullptr;
+ delete m_storage;
+ m_storage = nullptr;
- if (--(control->weak_count) == 0)
+ if (--(m_control_block->weak_count) == 0)
{
- delete control;
+ delete m_control_block;
}
}
}
}
- T * pointer; ///< The managed object.
- shared_control_block * control; ///< Shared control block.
+ T * m_storage; ///< The managed object.
+ shared_control_block * m_control_block; ///< Shared control block.
};
/**