|
Scope
|
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 &&) |
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:
Or if you are pre-C++17, you can use:
| R | The resource type |
| D | The deleter type |
|
inlineexplicitnoexcept |
Constructs a unique_resource from a given r resource and the deleter.
| r | the resource to own uniquely |
| d | the deleter to destroy the resource |
|
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
| other | the othe resource to move |
|
noexcept |
Gets a reference to the underlying resource.
|
noexcept |
Gets a reference to the underlying deleter.
|
noexcept |
| other |
reference to (*this)
|
noexcept |
Releases the underlying stored resource from being managed by this unique_resource class.
|
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.
| 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.
| r | the new resource to store |