Cogs.Foundation
Loading...
Searching...
No Matches
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.
 
Tget ()
 Gets the raw pointer stored in this instance.
 
const Tget () const
 Gets the raw pointer stored in this instance.
 
Toperator-> ()
 Access the raw pointer in this instance.
 
const Toperator-> () 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.

Constructor & Destructor Documentation

◆ Pointer() [1/4]

template<typename T >
Cogs::Pointer< T >::Pointer ( )
default

Default construct an empty instance.

◆ Pointer() [2/4]

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.

◆ Pointer() [3/4]

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

Move construct an instance from other.

◆ Pointer() [4/4]

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

Copy construction disabled.

◆ ~Pointer()

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

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

Member Function Documentation

◆ get() [1/2]

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

Gets the raw pointer stored in this instance.

◆ get() [2/2]

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

Gets the raw pointer stored in this instance.

◆ 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->...
}

◆ operator->() [1/2]

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

Access the raw pointer in this instance.

◆ operator->() [2/2]

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

Access the raw pointer in this instance.

◆ operator=() [1/2]

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

Move assign an instance from other.

◆ operator=() [2/2]

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

Copy assignment disabled.


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