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 object 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 object.
constexpr UniquePointer (std::nullptr_t pointer) noexcept
 Construct a null pointer without an associated object.
template<typename U, typename OtherDeleter>
requires (!std::is_same_v<T, U> && std::is_convertible_v<typename UniquePointer<U, OtherDeleter>::pointer, pointer>)
constexpr UniquePointer (UniquePointer< U, OtherDeleter > &&other) noexcept
 Construct a pointer from another pointer of a derived type, taking ownership of its underlying object.
constexpr UniquePointer (pointer pointer, Deleter deleter=Deleter()) noexcept
 Construct a pointer that takes ownership of an existing object pointer.
constexpr operator bool () const noexcept
 Check if this pointer has an associated object, 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 object.
constexpr void reset (pointer newPointer=nullptr) noexcept
 Destroy the object associated with this pointer, if any, and take ownership of a new object pointer, which may be null.
constexpr pointer release () noexcept
 Relinquish ownership of the associated object.
constexpr pointer get () const noexcept
 Get the underlying pointer.
constexpr Deleter & get_deleter () noexcept
 Get the current deleter.
constexpr const Deleter & get_deleter () const noexcept
 Get the current deleter.
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

template<typename... Args>
static constexpr UniquePointer create (Args &&... args)
 Allocate and construct an object of the associated type and wrap it in a pointer with unique ownership.

Detailed Description

template<typename T, typename Deleter = DefaultDelete<T>>
class grem::UniquePointer< T, Deleter >

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

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

Member Typedef Documentation

◆ pointer

template<typename T, typename Deleter = DefaultDelete<T>>
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 = DefaultDelete<T>>
using grem::UniquePointer< T, Deleter >::element_type = T

Object type.

◆ deleter_type

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

Deleter type.

Constructor & Destructor Documentation

◆ UniquePointer() [1/4]

template<typename T, typename Deleter = DefaultDelete<T>>
grem::UniquePointer< T, Deleter >::UniquePointer ( )
constexprdefaultnoexcept

Construct a null pointer without an associated object.

◆ UniquePointer() [2/4]

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

Construct a null pointer without an associated object.

Parameters
pointernull pointer.

◆ UniquePointer() [3/4]

template<typename T, typename Deleter = DefaultDelete<T>>
template<typename U, typename OtherDeleter>
requires (!std::is_same_v<T, U> && std::is_convertible_v<typename UniquePointer<U, OtherDeleter>::pointer, pointer>)
grem::UniquePointer< T, Deleter >::UniquePointer ( UniquePointer< U, OtherDeleter > && other)
inlineconstexprnoexcept

Construct a pointer from another pointer of a derived type, taking ownership of its underlying object.

Parameters
otherother pointer whose object to take owership of.

◆ UniquePointer() [4/4]

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

Construct a pointer that takes ownership of an existing object pointer.

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

Member Function Documentation

◆ create()

template<typename T, typename Deleter = DefaultDelete<T>>
template<typename... Args>
constexpr UniquePointer grem::UniquePointer< T, Deleter >::create ( Args &&... args)
inlinestaticnodiscardconstexpr

Allocate and construct an object of the associated type and wrap it in a pointer with unique ownership.

Parameters
argsarguments to forward to the constructor of the underlying object.
Returns
a non-null pointer that owns the constructed object.
Exceptions
std::bad_allocon allocation failure.
anyexception thrown by the object's constructor.

◆ operator bool()

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

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

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

◆ operator==() [1/2]

template<typename T, typename Deleter = DefaultDelete<T>>
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 objects. It only compares the values of the pointers themselves.

◆ operator==() [2/2]

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

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

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

◆ reset()

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

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

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

◆ release()

template<typename T, typename Deleter = DefaultDelete<T>>
pointer grem::UniquePointer< T, Deleter >::release ( )
inlineconstexprnoexcept

Relinquish ownership of the associated object.

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

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

◆ get()

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

Get the underlying pointer.

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

◆ get_deleter() [1/2]

template<typename T, typename Deleter = DefaultDelete<T>>
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 = DefaultDelete<T>>
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 = DefaultDelete<T>>
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*()

template<typename T, typename Deleter = DefaultDelete<T>>
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: