SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Sleak::RefPtr< T > Class Template Reference

#include <RefPtr.hpp>

Inheritance diagram for Sleak::RefPtr< T >:

Public Member Functions

 RefPtr (const RefPtr &other)
template<typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
 RefPtr (const RefPtr< U > &other)
 RefPtr (RefPtr &&other) noexcept
 RefPtr (T *p=nullptr)
 ~RefPtr ()
T * get () const
RefPtroperator= (const RefPtr &other)
template<typename U>
RefPtroperator= (const RefPtr< U > &other)
RefPtroperator= (RefPtr &&other) noexcept
RefPtroperator= (std::nullptr_t) noexcept
void release ()
 Drops one reference, deleting the object and control block at zero.
void reset (T *newPtr=nullptr) override
size_t use_count () const
Public Member Functions inherited from Sleak::SmartPointer< T >
 SmartPointer (const SmartPointer &)=delete
virtual ~SmartPointer ()
T * get () const
 Raw pointer access; throws NullPointerException if unset.
virtual bool IsValid () const
 True if the pointer is non-null.
 operator bool () const
T & operator* () const
T * operator-> () const
SmartPointeroperator= (const SmartPointer &)=delete

Protected Member Functions

SharedControlBlockgetControlBlock () const
Protected Member Functions inherited from Sleak::SmartPointer< T >
 SmartPointer (T *p=nullptr)

Protected Attributes

SharedControlBlockcontrolBlock
Protected Attributes inherited from Sleak::SmartPointer< T >
T * ptr

Friends

template<typename U>
class RefPtr

Detailed Description

template<typename T>
class Sleak::RefPtr< T >

Atomic, intrusive-refcount smart pointer for engine resources. This is the standard owning pointer in the engine, used instead of shared_ptr.

Construct one from a raw pointer and it takes ownership, deleting the object when the last RefPtr to it goes away. Copies share the count; moves transfer it. Use this wherever the public API asks for an owning pointer, notably Material and the GPU buffer handles on MeshHandle.

The constructor is explicit, so ownership transfer is always visible at the call site. Converting RefPtr<Derived> to RefPtr<Base> is allowed and keeps the same control block. use_count() reports the current reference count.

The reference count is thread-safe. The object it guards is not: two threads calling into the same pointed-to object still need their own synchronization. Note also that get() on the SmartPointer base throws NullPointerException when the pointer is null, so test with IsValid() or the bool conversion before dereferencing an optional resource.

// Take ownership of a freshly created material
auto* raw = new Sleak::Material();
raw->SetRoughness(0.35f);
// Share it across several objects; each copy bumps the count
crate->AddComponent<Sleak::MaterialComponent>(material);
barrel->AddComponent<Sleak::MaterialComponent>(material);
if (material.IsValid()) {
material->SetMetallic(0.0f); // operator-> reaches the object
}
See also
SharedControlBlock, SmartPointer, ObjectPtr, WeakPtr

Definition at line 62 of file RefPtr.hpp.

Constructor & Destructor Documentation

◆ RefPtr() [1/4]

template<typename T>
Sleak::RefPtr< T >::RefPtr ( T * p = nullptr)
inlineexplicit

Definition at line 81 of file RefPtr.hpp.

◆ ~RefPtr()

template<typename T>
Sleak::RefPtr< T >::~RefPtr ( )
inline

Definition at line 85 of file RefPtr.hpp.

◆ RefPtr() [2/4]

template<typename T>
Sleak::RefPtr< T >::RefPtr ( const RefPtr< T > & other)
inline

Definition at line 88 of file RefPtr.hpp.

◆ RefPtr() [3/4]

template<typename T>
Sleak::RefPtr< T >::RefPtr ( RefPtr< T > && other)
inlinenoexcept

Definition at line 109 of file RefPtr.hpp.

◆ RefPtr() [4/4]

template<typename T>
template<typename U, typename = std::enable_if_t<std::is_base_of<T, U>::value>>
Sleak::RefPtr< T >::RefPtr ( const RefPtr< U > & other)
inline

Definition at line 129 of file RefPtr.hpp.

Member Function Documentation

◆ get()

template<typename T>
T * Sleak::RefPtr< T >::get ( ) const
inline

Definition at line 170 of file RefPtr.hpp.

◆ getControlBlock()

template<typename T>
SharedControlBlock * Sleak::RefPtr< T >::getControlBlock ( ) const
inlineprotected

Definition at line 174 of file RefPtr.hpp.

◆ operator=() [1/4]

template<typename T>
RefPtr & Sleak::RefPtr< T >::operator= ( const RefPtr< T > & other)
inline

Definition at line 96 of file RefPtr.hpp.

◆ operator=() [2/4]

template<typename T>
template<typename U>
RefPtr & Sleak::RefPtr< T >::operator= ( const RefPtr< U > & other)
inline

Definition at line 136 of file RefPtr.hpp.

◆ operator=() [3/4]

template<typename T>
RefPtr & Sleak::RefPtr< T >::operator= ( RefPtr< T > && other)
inlinenoexcept

Definition at line 116 of file RefPtr.hpp.

◆ operator=() [4/4]

template<typename T>
RefPtr & Sleak::RefPtr< T >::operator= ( std::nullptr_t )
inlinenoexcept

Definition at line 150 of file RefPtr.hpp.

◆ release()

template<typename T>
void Sleak::RefPtr< T >::release ( )
inline

Drops one reference, deleting the object and control block at zero.

Definition at line 74 of file RefPtr.hpp.

◆ reset()

template<typename T>
void Sleak::RefPtr< T >::reset ( T * newPtr = nullptr)
inlineoverridevirtual

Implements Sleak::SmartPointer< T >.

Definition at line 158 of file RefPtr.hpp.

◆ use_count()

template<typename T>
size_t Sleak::RefPtr< T >::use_count ( ) const
inline

Definition at line 165 of file RefPtr.hpp.

◆ RefPtr

template<typename T>
template<typename U>
friend class RefPtr
friend

Definition at line 65 of file RefPtr.hpp.

Member Data Documentation

◆ controlBlock

template<typename T>
SharedControlBlock* Sleak::RefPtr< T >::controlBlock
protected

Definition at line 68 of file RefPtr.hpp.