libdonut  2.3.2
Application framework for cross-platform game development in C++20
Public Member Functions | List of all members
donut::UniqueHandle< Handle, Deleter, NullHandle > Class Template Reference

Generic nullable RAII resource handle with exclusive ownership of a resource that is automatically destroyed on handle destruction. More...

#include <donut/UniqueHandle.hpp>

Public Member Functions

constexpr UniqueHandle () noexcept=default
 Construct a null handle without an associated resource. More...
 
constexpr UniqueHandle (Handle handle) noexcept
 Construct a handle that takes ownership of an existing resource handle. More...
 
 ~UniqueHandle ()
 Destroy the handle and its associated resource if it has one. More...
 
 UniqueHandle (const UniqueHandle &)=delete
 Copying a resource handle is disallowed to enforce exclusive ownership. More...
 
constexpr UniqueHandle (UniqueHandle &&other) noexcept
 Move constructor. More...
 
UniqueHandleoperator= (const UniqueHandle &)=delete
 Copying a resource handle is disallowed to enforce exclusive ownership. More...
 
constexpr UniqueHandleoperator= (UniqueHandle &&other) noexcept
 Move assignment. More...
 
constexpr operator bool () const noexcept
 Check if this handle has an associated resource, i.e. More...
 
constexpr bool operator== (const UniqueHandle &other) const noexcept
 Compare this resource handle against another for equality of the underlying handle value. More...
 
constexpr void reset (Handle newHandle=NullHandle) noexcept
 Destroy the resource associated with this handle, if any, and take ownership of a new resource handle, which may be null. More...
 
constexpr Handle release () noexcept
 Relinquish ownership of the associated resource. More...
 
constexpr Handle get () const noexcept
 Get the value of the underlying resource handle. More...
 

Detailed Description

template<typename Handle, typename Deleter, Handle NullHandle = Handle{}>
class donut::UniqueHandle< Handle, Deleter, NullHandle >

Generic nullable RAII resource handle with exclusive ownership of a resource that is automatically destroyed on handle destruction.

Template Parameters
Handlethe underlying handle type. Usually an integer or pointer type.
Deleterstateless functor type that destroys the associated resource when default-constructed and called with a non-null handle. Passing a handle equal to NullHandle must be a no-op.
NullHandleconstant value representing a null handle that does not have an associated resource. Usually 0 or nullptr.

Constructor & Destructor Documentation

◆ UniqueHandle() [1/4]

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
constexpr donut::UniqueHandle< Handle, Deleter, NullHandle >::UniqueHandle ( )
constexprdefaultnoexcept

Construct a null handle without an associated resource.

◆ UniqueHandle() [2/4]

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
constexpr donut::UniqueHandle< Handle, Deleter, NullHandle >::UniqueHandle ( Handle  handle)
inlineexplicitconstexprnoexcept

Construct a handle that takes ownership of an existing resource handle.

Parameters
handlethe underlying handle to take ownership of, or NullHandle to construct a null handle without an associated resource.

◆ ~UniqueHandle()

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
donut::UniqueHandle< Handle, Deleter, NullHandle >::~UniqueHandle ( )
inline

Destroy the handle and its associated resource if it has one.

◆ UniqueHandle() [3/4]

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
donut::UniqueHandle< Handle, Deleter, NullHandle >::UniqueHandle ( const UniqueHandle< Handle, Deleter, NullHandle > &  )
delete

Copying a resource handle is disallowed to enforce exclusive ownership.

◆ UniqueHandle() [4/4]

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
constexpr donut::UniqueHandle< Handle, Deleter, NullHandle >::UniqueHandle ( UniqueHandle< Handle, Deleter, NullHandle > &&  other)
inlineconstexprnoexcept

Move constructor.

Takes ownership of the resource associated with the other handle and sets the other handle to null.

Parameters
otherresource handle to take ownership of.

Member Function Documentation

◆ operator=() [1/2]

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
UniqueHandle& donut::UniqueHandle< Handle, Deleter, NullHandle >::operator= ( const UniqueHandle< Handle, Deleter, NullHandle > &  )
delete

Copying a resource handle is disallowed to enforce exclusive ownership.

◆ operator=() [2/2]

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
constexpr UniqueHandle& donut::UniqueHandle< Handle, Deleter, NullHandle >::operator= ( UniqueHandle< Handle, Deleter, NullHandle > &&  other)
inlineconstexprnoexcept

Move assignment.

Destroys the resource associated with the current handle, if any. Then takes ownership of the resource associated with the other handle and sets the other handle to null.

Parameters
otherresource handle to take ownership of.
Returns
*this, for chaining.

◆ operator bool()

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
constexpr donut::UniqueHandle< Handle, Deleter, NullHandle >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Check if this handle has an associated resource, i.e.

if it is not null.

Returns
true if the handle has an associated resource, false if the handle is null.

◆ operator==()

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
constexpr bool donut::UniqueHandle< Handle, Deleter, NullHandle >::operator== ( const UniqueHandle< Handle, Deleter, NullHandle > &  other) const
inlineconstexprnoexcept

Compare this resource handle against another for equality of the underlying handle value.

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

◆ reset()

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
constexpr void donut::UniqueHandle< Handle, Deleter, NullHandle >::reset ( Handle  newHandle = NullHandle)
inlineconstexprnoexcept

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

Parameters
newHandlethe new underlying handle to take ownership of, or NullHandle to reset to a null handle without an associated resource.
See also
release()
get()

◆ release()

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
constexpr Handle donut::UniqueHandle< Handle, Deleter, NullHandle >::release ( )
inlineconstexprnoexcept

Relinquish ownership of the associated resource.

This handle will be reset to null, without destroying the associated resource.

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

◆ get()

template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
constexpr Handle donut::UniqueHandle< Handle, Deleter, NullHandle >::get ( ) const
inlineconstexprnoexcept

Get the value of the underlying resource handle.

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

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