Raven Engine v0.1
Loading...
Searching...
No Matches
Raven::Ref< T > Class Template Reference

Atomic reference-counted pointer. More...

#include <Ref.h>

Public Member Functions

 Ref (T *ptr=nullptr) noexcept
 Constructs a Ref from a raw pointer.
 Ref (T *ptr, std::atomic< int > *refCount)
 Constructs a Ref by sharing an existing ref count.
 Ref (const Ref &other) noexcept
 Copy constructor: increments reference count.
template<typename U, typename = std::enable_if_t<std::is_base_of_v<T, U>>>
 Ref (const Ref< U > &other) noexcept
 Templated copy constructor for base/derived conversions.
 Ref (Ref &&other) noexcept
 Move constructor: takes ownership without altering count.
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
 Ref (Ref< U > &&other) noexcept
 Templated move constructor for convertible types.
template<typename T2>
Ref< T2 > As ()
 Casts this Ref to another Ref type if compatible.
Refoperator= (const Ref &other) noexcept
 Copy assignment: releases current and increments new count.
Refoperator= (Ref &&other) noexcept
 Move assignment: releases current and takes ownership.
 ~Ref ()
 Destructor: releases reference and deletes if last.
T * Get () const noexcept
 Returns the raw pointer.
T & operator* () const
 Dereference operator.
T * operator-> () const
 Arrow operator.
 operator bool () const noexcept
 Boolean conversion: true if non-null.
int GetRefCount () const
 Gets the current reference count.
void Release ()
 Releases the managed pointer, deleting if last reference.
void Delete ()
 Deletes the managed pointer without reference counting.
void Reset (T *ptr=nullptr) noexcept
 Resets the Ref to manage a new pointer.

Friends

template<typename T2>
class Ref
bool operator== (const Ref< T > &lhs, const Ref< T > &rhs) noexcept
bool operator!= (const Ref< T > &lhs, const Ref< T > &rhs) noexcept

Detailed Description

template<typename T>
class Raven::Ref< T >

Atomic reference-counted pointer.

Manages a pointer through an atomic reference count. Automatically deletes the managed object when the last Ref goes out of scope. Supports copying, moving, and cross-casting via As<T>().

Template Parameters
TThe type of the managed object.

Constructor & Destructor Documentation

◆ Ref() [1/6]

template<typename T>
Raven::Ref< T >::Ref ( T * ptr = nullptr)
inlinenoexcept

Constructs a Ref from a raw pointer.

If ptr is non-null, a new atomic reference count is allocated and set to 1.

Parameters
ptrRaw pointer to manage.

◆ Ref() [2/6]

template<typename T>
Raven::Ref< T >::Ref ( T * ptr,
std::atomic< int > * refCount )
inline

Constructs a Ref by sharing an existing ref count.

Increments the provided reference count if non-null.

Parameters
ptrRaw pointer to manage.
refCountExisting atomic reference count to share.

◆ Ref() [3/6]

template<typename T>
Raven::Ref< T >::Ref ( const Ref< T > & other)
inlinenoexcept

Copy constructor: increments reference count.

◆ Ref() [4/6]

template<typename T>
template<typename U, typename = std::enable_if_t<std::is_base_of_v<T, U>>>
Raven::Ref< T >::Ref ( const Ref< U > & other)
inlinenoexcept

Templated copy constructor for base/derived conversions.

◆ Ref() [5/6]

template<typename T>
Raven::Ref< T >::Ref ( Ref< T > && other)
inlinenoexcept

Move constructor: takes ownership without altering count.

◆ Ref() [6/6]

template<typename T>
template<typename U, typename = std::enable_if_t<std::is_convertible_v<U*, T*>>>
Raven::Ref< T >::Ref ( Ref< U > && other)
inlinenoexcept

Templated move constructor for convertible types.

◆ ~Ref()

template<typename T>
Raven::Ref< T >::~Ref ( )
inline

Destructor: releases reference and deletes if last.

Member Function Documentation

◆ As()

template<typename T>
template<typename T2>
Ref< T2 > Raven::Ref< T >::As ( )
inline

Casts this Ref to another Ref type if compatible.

Template Parameters
T2The target type to cast to.
Returns
Ref<T2> instance sharing the same reference count.

◆ Delete()

template<typename T>
void Raven::Ref< T >::Delete ( )
inline

Deletes the managed pointer without reference counting.

Warning
Use with caution: this does not decrement the reference count. But instead sets it to 0 and deletes the pointer as well as the reference count.

◆ Get()

template<typename T>
T * Raven::Ref< T >::Get ( ) const
inlinenoexcept

Returns the raw pointer.

◆ GetRefCount()

template<typename T>
int Raven::Ref< T >::GetRefCount ( ) const
inline

Gets the current reference count.

◆ operator bool()

template<typename T>
Raven::Ref< T >::operator bool ( ) const
inlineexplicitnoexcept

Boolean conversion: true if non-null.

◆ operator*()

template<typename T>
T & Raven::Ref< T >::operator* ( ) const
inline

Dereference operator.

◆ operator->()

template<typename T>
T * Raven::Ref< T >::operator-> ( ) const
inline

Arrow operator.

◆ operator=() [1/2]

template<typename T>
Ref & Raven::Ref< T >::operator= ( const Ref< T > & other)
inlinenoexcept

Copy assignment: releases current and increments new count.

◆ operator=() [2/2]

template<typename T>
Ref & Raven::Ref< T >::operator= ( Ref< T > && other)
inlinenoexcept

Move assignment: releases current and takes ownership.

◆ Release()

template<typename T>
void Raven::Ref< T >::Release ( )
inline

Releases the managed pointer, deleting if last reference.

◆ Reset()

template<typename T>
void Raven::Ref< T >::Reset ( T * ptr = nullptr)
inlinenoexcept

Resets the Ref to manage a new pointer.

Releases the old object (if any) and starts a new reference count.

Parameters
ptrNew raw pointer to manage.

◆ operator!=

template<typename T>
bool operator!= ( const Ref< T > & lhs,
const Ref< T > & rhs )
friend

◆ operator==

template<typename T>
bool operator== ( const Ref< T > & lhs,
const Ref< T > & rhs )
friend

◆ Ref

template<typename T>
template<typename T2>
friend class Ref
friend

Allow Ref<T2> access.


The documentation for this class was generated from the following file:
  • Engine/src/Raven/Memory/Ref.h