Cogs.Core
Public Member Functions | List of all members
Cogs::Pointer< T > Struct Template Reference

Provides scoped storage for a raw pointer and corresponding deletion logic. More...

#include <Pointer.h>

Public Member Functions

 Pointer ()=default
 Default construct an empty instance.
 
template<typename DeleterFunction >
 Pointer (T *ptr, DeleterFunction deleter)
 Constructs the instance from the given pointer and deleter function.
 
 Pointer (Pointer &&other) noexcept
 Move construct an instance from other.
 
Pointeroperator= (Pointer &&other) noexcept
 Move assign an instance from other.
 
 Pointer (Pointer &other)=delete
 Copy construction disabled.
 
Pointeroperator= (Pointer &other)=delete
 Copy assignment disabled.
 
 ~Pointer ()
 Destructs the instance, invoking cleanup logic if the instance is non-empty.
 
 operator bool () const
 Boolean conversion operator.
 
T * get ()
 Gets the raw pointer stored in this instance.
 
const T * get () const
 Gets the raw pointer stored in this instance.
 
T * operator-> ()
 Access the raw pointer in this instance.
 
const T * operator-> () const
 Access the raw pointer in this instance.
 

Detailed Description

template<typename T>
struct Cogs::Pointer< T >

Provides scoped storage for a raw pointer and corresponding deletion logic.

Typically used where raw new/delete is not applicable to store destruction logic along the raw pointer, instead of relying on manual cleanup code.

The ownership may be moved to another instance to facilitate relocating Pointer instances.

Example:

Resource * r = pool.allocate();
{
auto p = makePointer(r, [&pool](Resource * r){ pool.deallocate(); });
...
if (p) {
// Dostuff
}
// r is deallocated when p goes out of scope.
}
Pointer< T > makePointer(T *t, DeleterFunction deleterFunc)
Construct a Pointer<T> instance from the given pointer and deleter function.
Definition: Pointer.h:172
Template Parameters
TType of raw pointer stored.

Definition at line 65 of file Pointer.h.

Constructor & Destructor Documentation

◆ Pointer() [1/2]

template<typename T >
template<typename DeleterFunction >
Cogs::Pointer< T >::Pointer ( T *  ptr,
DeleterFunction  deleter 
)
inline

Constructs the instance from the given pointer and deleter function.

The pointer is stored and accessible via the get() function or operator->(). When the instance goes out of scope the deleter function is invoked.

Parameters
ptrRaw pointer to store.
deleterFunction to execute when this instance goes out of scope.

Definition at line 80 of file Pointer.h.

◆ Pointer() [2/2]

template<typename T >
Cogs::Pointer< T >::Pointer ( Pointer< T > &&  other)
inlinenoexcept

Move construct an instance from other.

Definition at line 89 of file Pointer.h.

◆ ~Pointer()

template<typename T >
Cogs::Pointer< T >::~Pointer ( )
inline

Destructs the instance, invoking cleanup logic if the instance is non-empty.

Definition at line 113 of file Pointer.h.

Member Function Documentation

◆ get() [1/2]

template<typename T >
T * Cogs::Pointer< T >::get ( )
inline

Gets the raw pointer stored in this instance.

Definition at line 138 of file Pointer.h.

◆ get() [2/2]

template<typename T >
const T * Cogs::Pointer< T >::get ( ) const
inline

Gets the raw pointer stored in this instance.

Definition at line 141 of file Pointer.h.

◆ operator bool()

template<typename T >
Cogs::Pointer< T >::operator bool ( ) const
inline

Boolean conversion operator.

Allows checking if the instance is empty similar to null-pointer checks.

auto p = makePointer(...);
if (p) {
// p is a valid non-empty pointer instance
p->...
}

Definition at line 135 of file Pointer.h.

◆ operator->() [1/2]

template<typename T >
T * Cogs::Pointer< T >::operator-> ( )
inline

Access the raw pointer in this instance.

Definition at line 144 of file Pointer.h.

◆ operator->() [2/2]

template<typename T >
const T * Cogs::Pointer< T >::operator-> ( ) const
inline

Access the raw pointer in this instance.

Definition at line 147 of file Pointer.h.

◆ operator=()

template<typename T >
Pointer & Cogs::Pointer< T >::operator= ( Pointer< T > &&  other)
inlinenoexcept

Move assign an instance from other.

Definition at line 97 of file Pointer.h.


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