libcppwrap
A collection of C++ wrappers for native APIs
Loading...
Searching...
No Matches
Public Member Functions | List of all members
w::handle< Resource, Closed, CloseFunc, > Class Template Reference

An RAII handle type which owns an opaque resource of a specified type, and calls a designated function to close the resource upon destruction. More...

#include <handle.hpp>

Public Member Functions

 handle () noexcept
 Constructs a handle which does not own a resource. More...
 
 handle (Resource resource) noexcept
 Constructs a handle which owns the specified resource. More...
 
 handle (const handle &)=delete
 
handleoperator= (const handle &)=delete
 
 handle (handle &&other) noexcept
 Constructs a handle which takes ownership of a resource (if any) from another handle. More...
 
handleoperator= (handle &&other) noexcept
 Releases the resource (if any) owned by this handle and takes ownership of a resource (if any) from another handle. More...
 
 ~handle () noexcept
 Closes the resource (if any) owned by this handle. More...
 
Resource get () noexcept
 Returns the resource (if any) owned by this handle. More...
 
const Resource get () const noexcept
 Returns the resource (if any) owned by this handle. More...
 
 operator Resource () noexcept
 Returns the resource (if any) owned by this handle. More...
 
 operator const Resource () const noexcept
 Returns the resource (if any) owned by this handle. More...
 
 operator bool () const noexcept
 Tests whether this handle owns a resource. More...
 
void close () noexcept
 Closes the resource (if any) owned by this handle by calling CloseFunc. More...
 
Resource release () noexcept
 Releases (without closing) and returns the underlying resource. More...
 

Detailed Description

template<typename Resource, Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
class w::handle< Resource, Closed, CloseFunc, >

An RAII handle type which owns an opaque resource of a specified type, and calls a designated function to close the resource upon destruction.

The type is moveable but not copyable, and provides semantics similar to std::unique_ptr. Most members are noexcept for underlying resource types which are nothrow-swappable and nothrow-copy-constructible.

Template Parameters
ResourceThe type of the resource to own. This type must be nothrow-comparable, nothrow-copy-constructible and nothrow-swappable.
ClosedA special value which indicates a closed resource.
CloseFuncA function which closes a resource. The function must not throw exceptions, and preferably should be marked nothrow. The function must either take no arguments or a single argument of type Resource. Any value returned by the function is ignored.

Constructor & Destructor Documentation

◆ handle() [1/4]

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
w::handle< Resource, Closed, CloseFunc, >::handle ( )
inlinenoexcept

Constructs a handle which does not own a resource.

◆ handle() [2/4]

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
w::handle< Resource, Closed, CloseFunc, >::handle ( Resource  resource)
inlinenoexcept

Constructs a handle which owns the specified resource.

Parameters
resourceThe resource to own.

◆ handle() [3/4]

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
w::handle< Resource, Closed, CloseFunc, >::handle ( const handle< Resource, Closed, CloseFunc, > &  )
delete

◆ handle() [4/4]

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
w::handle< Resource, Closed, CloseFunc, >::handle ( handle< Resource, Closed, CloseFunc, > &&  other)
inlinenoexcept

Constructs a handle which takes ownership of a resource (if any) from another handle.

Parameters
otherThe other handle whose resource (if any) to take ownership of.

◆ ~handle()

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
w::handle< Resource, Closed, CloseFunc, >::~handle ( )
inlinenoexcept

Closes the resource (if any) owned by this handle.

Member Function Documentation

◆ close()

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
void w::handle< Resource, Closed, CloseFunc, >::close ( )
inlinenoexcept

Closes the resource (if any) owned by this handle by calling CloseFunc.

After the call, the handle no longer owns a resource. CloseFunc is only called if the handle owns a resource.

◆ get() [1/2]

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
const Resource w::handle< Resource, Closed, CloseFunc, >::get ( ) const
inlinenoexcept

Returns the resource (if any) owned by this handle.

Returns
The owned resource, or the special value Closed if this handle does not own a resource.

◆ get() [2/2]

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
Resource w::handle< Resource, Closed, CloseFunc, >::get ( )
inlinenoexcept

Returns the resource (if any) owned by this handle.

Returns
The owned resource, or the special value Closed if this handle does not own a resource.

◆ operator bool()

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
w::handle< Resource, Closed, CloseFunc, >::operator bool ( ) const
inlinenoexcept

Tests whether this handle owns a resource.

Returns
true if this handle owns a resource.

◆ operator const Resource()

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
w::handle< Resource, Closed, CloseFunc, >::operator const Resource ( ) const
inlinenoexcept

Returns the resource (if any) owned by this handle.

Returns
The owned resource, or the special value Closed if this handle does not own a resource.

◆ operator Resource()

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
w::handle< Resource, Closed, CloseFunc, >::operator Resource ( )
inlinenoexcept

Returns the resource (if any) owned by this handle.

Returns
The owned resource, or the special value Closed if this handle does not own a resource.

◆ operator=() [1/2]

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
handle & w::handle< Resource, Closed, CloseFunc, >::operator= ( const handle< Resource, Closed, CloseFunc, > &  )
delete

◆ operator=() [2/2]

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
handle & w::handle< Resource, Closed, CloseFunc, >::operator= ( handle< Resource, Closed, CloseFunc, > &&  other)
inlinenoexcept

Releases the resource (if any) owned by this handle and takes ownership of a resource (if any) from another handle.

Parameters
otherThe other handle whose resource (if any) to take ownership of.
Returns
A reference to this object.

◆ release()

template<typename Resource , Resource Closed, auto CloseFunc, std::enable_if_t< std::is_nothrow_copy_constructible_v< Resource > &&std::is_nothrow_swappable_v< Resource > &&std::is_function_v< std::remove_pointer_t< decltype(CloseFunc)> > &&(std::is_invocable_v< decltype(CloseFunc), Resource >||std::is_invocable_v< decltype(CloseFunc)>)> * = nullptr>
Resource w::handle< Resource, Closed, CloseFunc, >::release ( )
inlinenoexcept

Releases (without closing) and returns the underlying resource.

Returns
The underlying resource.

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