Scope
 All Classes Functions Pages
scope::unique_resource< R, D > Class Template Reference

A resource with unique-ownership semantics. More...

#include <scope.hpp>

Public Types

using resource_type = typename std::remove_reference< R >::type
 
using deleter_type = typename std::remove_reference< D >::type
 

Public Member Functions

template<typename RR , typename DD , typename = enable_constructor_t<R,D,RR,DD>>
 unique_resource (RR &&r, DD &&d) noexcept(std::is_nothrow_constructible< R, RR >::value &&std::is_nothrow_constructible< D, DD >::value)
 Constructs a unique_resource from a given r resource and the deleter. More...
 
 unique_resource (unique_resource &&other) noexcept(std::is_nothrow_move_constructible< R >::value &&std::is_nothrow_move_constructible< D >::value)
 Constructs a unique_resource by moving the contents of an existing resource. More...
 
 unique_resource (const unique_resource &other)=delete
 
 ~unique_resource ()
 Equivalent to invoking reset()
 
unique_resource & operator= (unique_resource &&other) noexcept(std::is_nothrow_assignable< R &, R >::value &&std::is_nothrow_assignable< D &, D >::value)
 
unique_resource & operator= (const unique_resource &other)=delete
 
void reset () noexcept
 Resets the resource managed by this unique_resource. More...
 
template<typename RR >
void reset (RR &&r)
 Resets the resource managed by this unique_resource to the newly specified r resource. More...
 
void release () noexcept
 Releases the underlying stored resource from being managed by this unique_resource class. More...
 
const resource_type & get () const noexcept
 Gets a reference to the underlying resource. More...
 
const deleter_type & get_deleter () const noexcept
 Gets a reference to the underlying deleter. More...
 
template<typename RR = R>
std::enable_if
< std::is_pointer< RR >::value,
typename std::remove_pointer
< R >::type & >::type 
operator* () const noexcept
 
template<typename RR = R>
std::enable_if
< std::is_pointer< RR >::value,
R >::type 
operator-> () const noexcept
 

Friends

template<typename T , typename U , typename S >
unique_resource< typename
std::decay< T >::type,
typename std::decay< U >::type > 
make_unique_resource_checked (T &&, const S &, U &&)
 

Detailed Description

template<typename R, typename D>
class scope::unique_resource< R, D >

A resource with unique-ownership semantics.

This wraps and owns the specified resource R and deletes it at the end of its lifetime with D.

This utility is extremely useful for wrapping old C-style functions that return raw resources like pointers, or integer handles, since you can couple the destruction of the resource safely inside this class. For example:

auto resource = scope::unique_resource{::open(...), &::close};

Or if you are pre-C++17, you can use:

auto resource = scope::make_unique_resource(::open(...), &::close);
Template Parameters
RThe resource type
DThe deleter type

Definition at line 709 of file scope.hpp.

Constructor & Destructor Documentation

template<typename R , typename D >
template<typename RR , typename DD , typename >
scope::unique_resource< R, D >::unique_resource ( RR &&  r,
DD &&  d 
)
inlineexplicitnoexcept

Constructs a unique_resource from a given r resource and the deleter.

Parameters
rthe resource to own uniquely
dthe deleter to destroy the resource

Definition at line 975 of file scope.hpp.

template<typename R , typename D >
scope::unique_resource< R, D >::unique_resource ( unique_resource< R, D > &&  other)
inlinenoexcept

Constructs a unique_resource by moving the contents of an existing resource.

The contents of the moved resource are now owned by this resource

Parameters
otherthe othe resource to move

Definition at line 984 of file scope.hpp.

Member Function Documentation

template<typename R, typename D>
const resource_type& scope::unique_resource< R, D >::get ( ) const
noexcept

Gets a reference to the underlying resource.

Returns
a reference to the underlying resource
template<typename R, typename D>
const deleter_type& scope::unique_resource< R, D >::get_deleter ( ) const
noexcept

Gets a reference to the underlying deleter.

Returns
a reference to the underlying deleter
template<typename R, typename D>
unique_resource& scope::unique_resource< R, D >::operator= ( unique_resource< R, D > &&  other)
noexcept
Parameters
other
Returns
reference to (*this)
template<typename R, typename D>
void scope::unique_resource< R, D >::release ( )
noexcept

Releases the underlying stored resource from being managed by this unique_resource class.

Note
by calling this function, the deleter of this class is no longer going to be invoked – making it the responsibility of the caller to take care of this instead.
template<typename R, typename D>
void scope::unique_resource< R, D >::reset ( )
noexcept

Resets the resource managed by this unique_resource.

This will invoke the stored deleter on the resource, if the resource has not already executed.

template<typename R, typename D>
template<typename RR >
void scope::unique_resource< R, D >::reset ( RR &&  r)

Resets the resource managed by this unique_resource to the newly specified r resource.

The old resource is deleted with the stored deleter and replaced by the new one.

Parameters
rthe new resource to store

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