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

CRTP base class that embeds a non-atomic reference counter into the derived type. More...

#include <ref_counted.hpp>

Public Member Functions

 RcFromThis () noexcept=default
 Default-constructs with reference count zero.
 RcFromThis (const RcFromThis &) noexcept
 Copy-constructs; does not copy the reference count.
 RcFromThis (RcFromThis &&) noexcept
 Move-constructs; does not transfer the reference count.
 ~RcFromThis () noexcept=default
 Destroys the embedded counter; does not delete the derived object.
RcFromThisoperator= (const RcFromThis &) noexcept
 Copy-assigns; does not affect the reference count.
RcFromThisoperator= (RcFromThis &&) 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 RefCounted

Detailed Description

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

CRTP base class that embeds a non-atomic reference counter into the derived type.

Inherit from this class to make a type usable with RefCounted<T>. The counter tracks the number of RefCounted handles pointing to the object. The counter is NOT thread-safe; use ArcFromThis if thread-safety is required.

The counter starts at 0. RefCounted<T> increments it on construction and decrements it on destruction, deleting the object when it reaches 0.

Warning
Do not copy or move RcFromThis directly; copies share the same counter slot but should be managed exclusively through RefCounted<T>.
Template Parameters
DerivedThe concrete class inheriting from this base
class Mesh final : public helios::mem::RcFromThis<Mesh> {
public:
explicit Mesh(int id) : id_(id) {}
int Id() const { return id_; }
private:
int id_;
};
auto mesh = helios::mem::MakeRc<Mesh>(42);
CRTP base class that embeds a non-atomic reference counter into the derived type.
auto MakeRc(Args &&... args) -> Rc< T >
Allocates and constructs a T object, returning a Rc<T, std::allocator<T>> handle.

Definition at line 57 of file ref_counted.hpp.

Constructor & Destructor Documentation

◆ RcFromThis() [1/3]

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

Default-constructs with reference count zero.

◆ RcFromThis() [2/3]

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

Copy-constructs; does not copy the reference count.

Definition at line 63 of file ref_counted.hpp.

◆ RcFromThis() [3/3]

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

Move-constructs; does not transfer the reference count.

Definition at line 66 of file ref_counted.hpp.

◆ ~RcFromThis()

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

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

Member Function Documentation

◆ operator=() [1/2]

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

Copy-assigns; does not affect the reference count.

Definition at line 72 of file ref_counted.hpp.

◆ operator=() [2/2]

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

Move-assigns; does not affect the reference count.

Definition at line 75 of file ref_counted.hpp.

◆ RefCount()

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

Returns the current reference count.

Returns
Current number of RefCounted handles owning this object

Definition at line 81 of file ref_counted.hpp.

◆ RefCounted

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

Definition at line 86 of file ref_counted.hpp.