CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CeresEngine::CopyOnWritePtr< T > Class Template Reference

A copy-on-write pointer type that shares a single instance of an object when copied but allows. More...

#include <CeresEngine/Foundation/SmartPtr.hpp>

Classes

struct  Data
 

Public Member Functions

 CopyOnWritePtr ()=default
 Create a new empty copy-on-write pointer.
 
template<typename U >
requires (std::is_constructible_v<T, U>)
 CopyOnWritePtr (U &&value)
 Creates a new copy-on-write pointer by constructing a new object of type U.
 
 CopyOnWritePtr (std::nullptr_t) noexcept
 Create a new empty copy-on-write pointer.
 
 CopyOnWritePtr (const CopyOnWritePtr &) noexcept=default
 Creates a copy of the CoW pointer.
 
CopyOnWritePtroperator= (const CopyOnWritePtr &) noexcept=default
 Assigns a copy of the CoW pointer.
 
 CopyOnWritePtr (CopyOnWritePtr &&) noexcept=default
 Creates a copy of the CoW pointer.
 
CopyOnWritePtroperator= (CopyOnWritePtr &&) noexcept=default
 Assigns a copy of the CoW pointer.
 
bool valid () const noexcept
 Checks if the pointer points to a valid object.
 
 operator bool () const noexcept
 Checks if the pointer points to a valid object.
 
const Tget () const noexcept
 Gets a const pointer to the object.
 
const Toperator* () const noexcept
 Gets a const pointer to the object.
 
const Toperator-> () const noexcept
 Gets a const pointer to the object.
 
void reset ()
 Resets the stored pointer and empties the CoW pointer.
 
template<typename Func >
decltype(automutate (Func &&func)
 Changes the pointer.
 

Private Member Functions

template<typename U , typename... Args>
 CopyOnWritePtr (std::in_place_type_t< U >, Args &&... args)
 Creates a new copy-on-write pointer by constructing a new object of type U.
 
void makeCopyIfNeeded ()
 Checks if we are the only user of the CoW pointer.
 

Private Attributes

Data mData
 A pointer to the object shared by all instances of the copy-on-write object.
 

Friends

bool operator== (const CopyOnWritePtr &lhs, const CopyOnWritePtr &rhs) noexcept
 Checks if the two pointers point to the same copy of the object.
 
bool operator!= (const CopyOnWritePtr &lhs, const CopyOnWritePtr &rhs) noexcept
 Checks if the two pointers do not point to the same copy of the object.
 
bool operator== (const CopyOnWritePtr &lhs, std::nullptr_t) noexcept
 Checks if the two pointers point to the same copy of the object.
 
bool operator!= (const CopyOnWritePtr &lhs, std::nullptr_t) noexcept
 Checks if the two pointers do not point to the same copy of the object.
 
bool operator== (std::nullptr_t, const CopyOnWritePtr &rhs) noexcept
 Checks if the two pointers point to the same copy of the object.
 
bool operator!= (std::nullptr_t, const CopyOnWritePtr &rhs) noexcept
 Checks if the two pointers do not point to the same copy of the object.
 
template<typename T1 , typename U , typename... Args>
CopyOnWritePtr< T1ce_cow_new (Args &&... args)
 Creates a new copy-on-write pointer by constructing a new object of type U.
 

Detailed Description

template<typename T>
class CeresEngine::CopyOnWritePtr< T >

A copy-on-write pointer type that shares a single instance of an object when copied but allows.

Template Parameters
T

Constructor & Destructor Documentation

◆ CopyOnWritePtr() [1/6]

template<typename T >
CeresEngine::CopyOnWritePtr< T >::CopyOnWritePtr ( )
default

Create a new empty copy-on-write pointer.

◆ CopyOnWritePtr() [2/6]

template<typename T >
template<typename U >
requires (std::is_constructible_v<T, U>)
CeresEngine::CopyOnWritePtr< T >::CopyOnWritePtr ( U &&  value)
inline

Creates a new copy-on-write pointer by constructing a new object of type U.

Parameters
valueThe object to be copied to the CoW pointer.

◆ CopyOnWritePtr() [3/6]

template<typename T >
CeresEngine::CopyOnWritePtr< T >::CopyOnWritePtr ( std::nullptr_t  )
inlinenoexcept

Create a new empty copy-on-write pointer.

◆ CopyOnWritePtr() [4/6]

template<typename T >
CeresEngine::CopyOnWritePtr< T >::CopyOnWritePtr ( const CopyOnWritePtr< T > &  )
defaultnoexcept

Creates a copy of the CoW pointer.

This won't copy the data, but will copy a reference to the original pointer.

◆ CopyOnWritePtr() [5/6]

template<typename T >
CeresEngine::CopyOnWritePtr< T >::CopyOnWritePtr ( CopyOnWritePtr< T > &&  )
defaultnoexcept

Creates a copy of the CoW pointer.

This won't copy the data, but will copy a reference to the original pointer.

◆ CopyOnWritePtr() [6/6]

template<typename T >
template<typename U , typename... Args>
CeresEngine::CopyOnWritePtr< T >::CopyOnWritePtr ( std::in_place_type_t< U ,
Args &&...  args 
)
inlineexplicitprivate

Creates a new copy-on-write pointer by constructing a new object of type U.

Template Parameters
ArgsThe object constructor argument types.
Parameters
argsThe object constructor arguments.

Member Function Documentation

◆ get()

template<typename T >
const T * CeresEngine::CopyOnWritePtr< T >::get ( ) const
inlinenoexcept

Gets a const pointer to the object.

◆ makeCopyIfNeeded()

template<typename T >
void CeresEngine::CopyOnWritePtr< T >::makeCopyIfNeeded ( )
inlineprivate

Checks if we are the only user of the CoW pointer.

If not, make a copy of the contents.

◆ mutate()

template<typename T >
template<typename Func >
decltype(auto) CeresEngine::CopyOnWritePtr< T >::mutate ( Func &&  func)
inline

Changes the pointer.

Makes a copy of it if required.

Template Parameters
FuncThe function to be called to make changes to the stored object.
Parameters
funcThe function to be called to make changes to the stored object.
Returns
The value returned by the func functor, if any.

◆ operator bool()

template<typename T >
CeresEngine::CopyOnWritePtr< T >::operator bool ( ) const
inlineexplicitnoexcept

Checks if the pointer points to a valid object.

◆ operator*()

template<typename T >
const T & CeresEngine::CopyOnWritePtr< T >::operator* ( ) const
inlinenoexcept

Gets a const pointer to the object.

◆ operator->()

template<typename T >
const T * CeresEngine::CopyOnWritePtr< T >::operator-> ( ) const
inlinenoexcept

Gets a const pointer to the object.

◆ operator=() [1/2]

Assigns a copy of the CoW pointer.

This won't copy the data, but will copy a reference to the original pointer.

◆ operator=() [2/2]

template<typename T >
CopyOnWritePtr & CeresEngine::CopyOnWritePtr< T >::operator= ( CopyOnWritePtr< T > &&  )
defaultnoexcept

Assigns a copy of the CoW pointer.

This won't copy the data, but will copy a reference to the original pointer.

◆ reset()

template<typename T >
void CeresEngine::CopyOnWritePtr< T >::reset ( )
inline

Resets the stored pointer and empties the CoW pointer.

◆ valid()

template<typename T >
bool CeresEngine::CopyOnWritePtr< T >::valid ( ) const
inlinenoexcept

Checks if the pointer points to a valid object.

Friends And Related Symbol Documentation

◆ ce_cow_new

template<typename T >
template<typename T1 , typename U , typename... Args>
CopyOnWritePtr< T1 > ce_cow_new ( Args &&...  args)
friend

Creates a new copy-on-write pointer by constructing a new object of type U.

Template Parameters
UThe type of object to created a new instance for.
ArgsThe constructor argument types for the type U.
Parameters
argsThe constructor arguments for the type U.
Returns
A newly allocated copy-on-write pointer.

◆ operator!= [1/3]

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

Checks if the two pointers do not point to the same copy of the object.

◆ operator!= [2/3]

template<typename T >
bool operator!= ( const CopyOnWritePtr< T > &  lhs,
std::nullptr_t   
)
friend

Checks if the two pointers do not point to the same copy of the object.

◆ operator!= [3/3]

template<typename T >
bool operator!= ( std::nullptr_t  ,
const CopyOnWritePtr< T > &  rhs 
)
friend

Checks if the two pointers do not point to the same copy of the object.

◆ operator== [1/3]

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

Checks if the two pointers point to the same copy of the object.

◆ operator== [2/3]

template<typename T >
bool operator== ( const CopyOnWritePtr< T > &  lhs,
std::nullptr_t   
)
friend

Checks if the two pointers point to the same copy of the object.

◆ operator== [3/3]

template<typename T >
bool operator== ( std::nullptr_t  ,
const CopyOnWritePtr< T > &  rhs 
)
friend

Checks if the two pointers point to the same copy of the object.

Member Data Documentation

◆ mData

template<typename T >
Data CeresEngine::CopyOnWritePtr< T >::mData
private

A pointer to the object shared by all instances of the copy-on-write object.


The documentation for this class was generated from the following file: