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

Deleter type

resource_type

Resource type

traits_type

Resource traits

Member Functions

Name

Description

allocated

Returns true if the resource is allocated and to be reclaimed by the deleter, otherwise false.

get

Returns a reference to the resource object.

get_deleter

Returns a reference to the deleter object.

operator*

operator->

operator=

release

Marks the resource as unallocated. Does not call the deleter if the resource was previously allocated.

reset

If the resource is allocated, calls the deleter function on it and marks the resource as unallocated.

swap

unique_resource

~unique_resource

Invokes reset() and destroys the resource.

Friends

Name

Description

swap

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 if res is not one of the unallocated resource values and false otherwise. Note that is_allocated(make_default()) must always return false. 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 when unique_resource is not in the allocated state. Additionally, it will be possible to construct unique_resource with unallocated resource values, which will create unique_resource objects in deallocated state (the deleter will not be called on unallocated resource values).