Generic nullable RAII resource handle with exclusive ownership of a resource that is automatically destroyed on handle destruction.
More...
#include <donut/UniqueHandle.hpp>
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
-
Handle | the underlying handle type. Usually an integer or pointer type. |
Deleter | stateless 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. |
NullHandle | constant value representing a null handle that does not have an associated resource. Usually 0 or nullptr. |
◆ UniqueHandle() [1/4]
template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
Construct a null handle without an associated resource.
◆ UniqueHandle() [2/4]
template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
|
inlineexplicitconstexprnoexcept |
Construct a handle that takes ownership of an existing resource handle.
- Parameters
-
handle | the 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{}>
Destroy the handle and its associated resource if it has one.
◆ UniqueHandle() [3/4]
template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
Copying a resource handle is disallowed to enforce exclusive ownership.
◆ UniqueHandle() [4/4]
template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
Move constructor.
Takes ownership of the resource associated with the other handle and sets the other handle to null.
- Parameters
-
other | resource handle to take ownership of. |
◆ operator=() [1/2]
template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
Copying a resource handle is disallowed to enforce exclusive ownership.
◆ operator=() [2/2]
template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
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
-
other | resource handle to take ownership of. |
- Returns
*this
, for chaining.
◆ operator bool()
template<typename Handle , typename Deleter , Handle NullHandle = Handle{}>
|
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{}>
Compare this resource handle against another for equality of the underlying handle value.
- Parameters
-
other | the 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
-
newHandle | the 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{}>
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{}>
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: