GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::UniquePointer< T[], Deleter > Class Template Reference

Nullable RAII data pointer with exclusive ownership of an array that is automatically destroyed on pointer destruction. More...

#include <GREM/core/data/UniquePointer.hpp>

Public Types

using pointer = typename detail::deleter_pointer_type<T, std::remove_reference_t<Deleter>>::type
 Pointer type.
using element_type = T
 Object type.
using deleter_type = Deleter
 Deleter type.

Public Member Functions

constexpr UniquePointer () noexcept=default
 Construct a null pointer without an associated array.
constexpr UniquePointer (std::nullptr_t pointer) noexcept
 Construct a null pointer without an associated array.
constexpr UniquePointer (pointer pointer, Deleter deleter=Deleter()) noexcept
 Construct a pointer that takes ownership of an existing array.
constexpr operator bool () const noexcept
 Check if this pointer has an associated array, i.e. if it is not null.
constexpr bool operator== (const UniquePointer &other) const noexcept
 Compare this pointer against another for equality of the underlying address.
constexpr bool operator== (std::nullptr_t other) const noexcept
 Check if this pointer is null and has no associated array.
constexpr void reset (pointer newPointer=nullptr) noexcept
 Destroy the array associated with this pointer, if any, and take ownership of a new array, which may be null.
constexpr pointer release () noexcept
 Relinquish ownership of the associated array.
constexpr pointer get () const noexcept
 Get the underlying array pointer.
constexpr Deleter & get_deleter () noexcept
 Get the current deleter.
constexpr const Deleter & get_deleter () const noexcept
 Get the current deleter.
constexpr decltype(auto) operator[] (std::size_t index) const noexcept
 Access an element of the associated array.
constexpr pointer operator-> () const noexcept
 Access the associated object by pointer.
constexpr decltype(auto) operator* () const noexcept
 Access the associated object.

Static Public Member Functions

static constexpr UniquePointer create (std::size_t size)
 Allocate an array of default-initialized elements of the associated type and wrap it in a pointer with unique ownership.

Detailed Description

template<typename T, typename Deleter>
class grem::UniquePointer< T[], Deleter >

Nullable RAII data pointer with exclusive ownership of an array that is automatically destroyed on pointer destruction.

Template Parameters
Tthe element type of the associated array.
Deleterfunction object type that destroys the associated array when called with a non-null pointer. Passing nullptr must be a no-op.

Member Typedef Documentation

◆ pointer

template<typename T, typename Deleter>
using grem::UniquePointer< T[], Deleter >::pointer = typename detail::deleter_pointer_type<T, std::remove_reference_t<Deleter>>::type

Pointer type.

◆ element_type

template<typename T, typename Deleter>
using grem::UniquePointer< T[], Deleter >::element_type = T

Object type.

◆ deleter_type

template<typename T, typename Deleter>
using grem::UniquePointer< T[], Deleter >::deleter_type = Deleter

Deleter type.

Constructor & Destructor Documentation

◆ UniquePointer() [1/3]

template<typename T, typename Deleter>
grem::UniquePointer< T[], Deleter >::UniquePointer ( )
constexprdefaultnoexcept

Construct a null pointer without an associated array.

◆ UniquePointer() [2/3]

template<typename T, typename Deleter>
grem::UniquePointer< T[], Deleter >::UniquePointer ( std::nullptr_t pointer)
inlineconstexprnoexcept

Construct a null pointer without an associated array.

Parameters
pointernull pointer.

◆ UniquePointer() [3/3]

template<typename T, typename Deleter>
grem::UniquePointer< T[], Deleter >::UniquePointer ( pointer pointer,
Deleter deleter = Deleter() )
inlineexplicitconstexprnoexcept

Construct a pointer that takes ownership of an existing array.

Parameters
pointerthe underlying array pointer to take ownership of, or nullptr to construct a null pointer without an associated array.
deleterthe deleter to use to destroy the array on destruction.

Member Function Documentation

◆ create()

template<typename T, typename Deleter>
constexpr UniquePointer grem::UniquePointer< T[], Deleter >::create ( std::size_t size)
inlinestaticnodiscardconstexpr

Allocate an array of default-initialized elements of the associated type and wrap it in a pointer with unique ownership.

Parameters
sizenumber of elements to allocate.
Returns
a non-null pointer that owns the allocated array.
Exceptions
std::bad_allocon allocation failure.
std::bad_array_new_lengthif the given size is invalid.
anyexception thrown by the element's default constructor.

◆ operator bool()

template<typename T, typename Deleter>
grem::UniquePointer< T[], Deleter >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Check if this pointer has an associated array, i.e. if it is not null.

Returns
true if the pointer has an associated array, false if the pointer is null.

◆ operator==() [1/2]

template<typename T, typename Deleter>
bool grem::UniquePointer< T[], Deleter >::operator== ( const UniquePointer< T[], Deleter > & other) const
inlinenodiscardconstexprnoexcept

Compare this pointer against another for equality of the underlying address.

Parameters
otherthe pointer to compare this pointer to.
Returns
true if the pointers are equal, false otherwise.
Note
This does not compare the values of any associated arrays. It only compares the values of the pointers themselves.

◆ operator==() [2/2]

template<typename T, typename Deleter>
bool grem::UniquePointer< T[], Deleter >::operator== ( std::nullptr_t other) const
inlinenodiscardconstexprnoexcept

Check if this pointer is null and has no associated array.

Parameters
otherthe null pointer to compare this pointer to.
Returns
true if the pointers are equal, false otherwise.

◆ reset()

template<typename T, typename Deleter>
void grem::UniquePointer< T[], Deleter >::reset ( pointer newPointer = nullptr)
inlineconstexprnoexcept

Destroy the array associated with this pointer, if any, and take ownership of a new array, which may be null.

Parameters
newPointerthe new underlying array pointer to take ownership of, or nullptr to reset to a null pointer without an associated array.
See also
release()
get()

◆ release()

template<typename T, typename Deleter>
pointer grem::UniquePointer< T[], Deleter >::release ( )
inlineconstexprnoexcept

Relinquish ownership of the associated array.

This pointer will be reset to null, without destroying the associated array.

Returns
the pointer to the associated array that was released, or nullptr if the pointer did not have an associated array.
Warning
After calling this function, the associated array will no longer be destroyed automatically along with the pointer. It instead becomes the responsibility of the caller to ensure that the array is properly destroyed. If the intent is to reset the pointer to null while destroying the associated array in the process, use reset() instead.
See also
reset()
get()

◆ get()

template<typename T, typename Deleter>
pointer grem::UniquePointer< T[], Deleter >::get ( ) const
inlinenodiscardconstexprnoexcept

Get the underlying array pointer.

Returns
a non-owning copy of the underlying array pointer.
See also
reset()
release()
operator[]()

◆ get_deleter() [1/2]

template<typename T, typename Deleter>
Deleter & grem::UniquePointer< T[], Deleter >::get_deleter ( )
inlinenodiscardconstexprnoexcept

Get the current deleter.

Returns
a reference to the current deleter.

◆ get_deleter() [2/2]

template<typename T, typename Deleter>
const Deleter & grem::UniquePointer< T[], Deleter >::get_deleter ( ) const
inlinenodiscardconstexprnoexcept

Get the current deleter.

Returns
a read-only reference to the current deleter.

◆ operator[]()

template<typename T, typename Deleter>
decltype(auto) grem::UniquePointer< T[], Deleter >::operator[] ( std::size_t index) const
inlinenodiscardconstexprnoexcept

Access an element of the associated array.

Parameters
indexindex of the array element to access.
Returns
a reference to the given element of the associated array.
Warning
If the pointer is null and has no associated array, the behavior is undefined.
See also
get()

◆ operator->()

pointer grem::UniquePointer< T, Deleter >::operator-> ( ) const
inlinenodiscardconstexprnoexcept

Access the associated object by pointer.

Returns
a non-owning pointer to the associated object.
Warning
If the pointer is null and has no associated object, the behavior is undefined.
See also
get()

◆ operator*()

decltype(auto) grem::UniquePointer< T, Deleter >::operator* ( ) const
inlinenodiscardconstexprnoexcept

Access the associated object.

Returns
a reference to the associated object.
Warning
If the pointer is null and has no associated object, the behavior is undefined.
See also
get()

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