RAII wrapper for automatically reclaiming arbitrary resources.
Synopsis
Declared in header <boost/scope/unique_resource.hpp>
template<
typename Resource,
typename Deleter,
typename Traits = void>
class unique_resource;
Types
Name |
Description |
Deleter type |
|
Resource type |
|
Resource traits |
Member Functions
Name |
Description |
Returns |
|
Returns a reference to the resource object. |
|
Returns a reference to the deleter object. |
|
Marks the resource as unallocated. Does not call the deleter if the resource was previously allocated. |
|
If the resource is allocated, calls the deleter function on it and marks the resource as unallocated. |
|
Invokes |
Friends
Name |
Description |
Description
A unique_resource
object exclusively owns wrapped resource and invokes
the deleter function object on it on destruction. The wrapped resource can have
any type that is:
-
Move-constructible, where the move constructor is marked as
noexcept
, or -
Copy-constructible, or
-
An lvalue reference to an object type. The deleter must be a function object type that is callable on an lvalue of the resource type. The deleter must be copy-constructible. An optional resource traits template parameter may be specified. Resource traits can be used to optimize
unique_resource
implementation when the following conditions are met: -
There is at least one value of the resource type that is considered unallocated (that is, no allocated resource shall be equal to one of the unallocated resource values). The unallocated resource values need not be deallocated using the deleter.
-
One of the unallocated resource values can be considered the default. Constructing the default resource value and assigning it to a resource object (whether allocated or not) shall not throw exceptions.
-
Resource objects can be tested for being unallocated. Such a test shall not throw exceptions. If specified, the resource traits must be a class that has the following public static members:
-
Resource make_default() noexcept
- must return the default resource value. -
`bool is_allocated(Resource const & res) noexcept` - must return
true
ifres
is not one of the unallocated resource values andfalse
otherwise. Note thatis_allocated(make_default())
must always returnfalse.
When conforming resource traits are specified,unique_resource
will be able to avoid storing additional indication of whether the owned resource object needs to be deallocated with the deleter on destruction. It will use the default resource value to initialize the owned resource object whenunique_resource
is not in the allocated state. Additionally, it will be possible to constructunique_resource
with unallocated resource values, which will createunique_resource
objects in deallocated state (the deleter will not be called on unallocated resource values).