Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
helios::mem::ArcFromThis< Derived > Class Template Reference

CRTP base class that embeds an atomic reference counter into the derived type. More...

#include <ref_counted.hpp>

Public Member Functions

 ArcFromThis () noexcept=default
 Default-constructs with reference count zero.
 ArcFromThis (const ArcFromThis &) noexcept
 Copy-constructs; does not copy the reference count.
 ArcFromThis (ArcFromThis &&) noexcept
 Move-constructs; does not transfer the reference count.
 ~ArcFromThis () noexcept=default
 Destroys the embedded counter; does not delete the derived object.
ArcFromThisoperator= (const ArcFromThis &) noexcept
 Copy-assigns; does not affect the reference count.
ArcFromThisoperator= (ArcFromThis &&) noexcept
 Move-assigns; does not affect the reference count.
uint32_t RefCount () const noexcept
 Returns the current reference count.

Friends

template<typename D, typename A>
class AtomicRefCounted

Detailed Description

template<typename Derived>
class helios::mem::ArcFromThis< Derived >

CRTP base class that embeds an atomic reference counter into the derived type.

Inherit from this class to make a type usable with AtomicRefCounted<T>. The counter tracks the number of AtomicRefCounted handles pointing to the object. All counter operations use std::memory_order_acq_rel / std::memory_order_acquire to ensure correct visibility across threads.

Note
The counter starts at 0. AtomicRefCounted<T> increments it on construction and decrements it on destruction, deleting the object when it reaches 0.
Warning
Do not manage the same raw pointer from multiple threads without using AtomicRefCounted<T> handles exclusively.
Template Parameters
DerivedThe concrete class inheriting from this base
class Texture final : public helios::mem::ArcFromThis<Texture> {
public:
explicit Texture(std::string_view name) : name_(name) {}
std::string_view Name() const { return name_; }
private:
std::string name_;
};
auto tex = helios::mem::MakeArc<Texture>("diffuse");
CRTP base class that embeds an atomic reference counter into the derived type.
auto MakeArc(Args &&... args) -> Arc< T >
Allocates and constructs a T object, returning an Arc<T, std::allocator<T>> handle.

Definition at line 137 of file ref_counted.hpp.

Constructor & Destructor Documentation

◆ ArcFromThis() [1/3]

template<typename Derived>
helios::mem::ArcFromThis< Derived >::ArcFromThis ( )
defaultnoexcept

Default-constructs with reference count zero.

◆ ArcFromThis() [2/3]

template<typename Derived>
helios::mem::ArcFromThis< Derived >::ArcFromThis ( const ArcFromThis< Derived > & )
inlinenoexcept

Copy-constructs; does not copy the reference count.

Definition at line 143 of file ref_counted.hpp.

◆ ArcFromThis() [3/3]

template<typename Derived>
helios::mem::ArcFromThis< Derived >::ArcFromThis ( ArcFromThis< Derived > && )
inlinenoexcept

Move-constructs; does not transfer the reference count.

Definition at line 146 of file ref_counted.hpp.

◆ ~ArcFromThis()

template<typename Derived>
helios::mem::ArcFromThis< Derived >::~ArcFromThis ( )
defaultnoexcept

Destroys the embedded counter; does not delete the derived object.

Member Function Documentation

◆ operator=() [1/2]

template<typename Derived>
ArcFromThis & helios::mem::ArcFromThis< Derived >::operator= ( ArcFromThis< Derived > && )
inlinenoexcept

Move-assigns; does not affect the reference count.

Definition at line 157 of file ref_counted.hpp.

◆ operator=() [2/2]

template<typename Derived>
ArcFromThis & helios::mem::ArcFromThis< Derived >::operator= ( const ArcFromThis< Derived > & )
inlinenoexcept

Copy-assigns; does not affect the reference count.

Definition at line 152 of file ref_counted.hpp.

◆ RefCount()

template<typename Derived>
uint32_t helios::mem::ArcFromThis< Derived >::RefCount ( ) const
inlinenodiscardnoexcept

Returns the current reference count.

Note
This is a snapshot; the value may change immediately after the call in a multithreaded context.
Returns
Current number of AtomicRefCounted handles owning this object

Definition at line 165 of file ref_counted.hpp.

◆ AtomicRefCounted

template<typename Derived>
template<typename D, typename A>
friend class AtomicRefCounted
friend

Definition at line 172 of file ref_counted.hpp.