libzypp 17.37.10

zypp::AutoDispose< Tp > Class Template Reference

Reference counted access to a `Tp` object calling a custom `Dispose` function when the last AutoDispose handle to it is destroyed or reset. More...

`#include <zypp-core/AutoDispose.h>`

Inheritance diagram for zypp::AutoDispose< Tp >:

## Classes
struct | Impl

## Public Types
using | param_type = typename boost::call_traits<Tp>::param_type
using | reference = typename boost::call_traits<Tp>::reference
using | const_reference = typename boost::call_traits<Tp>::const_reference
using | value_type = Tp
using | result_type = typename boost::call_traits<Tp>::value_type
using | dispose_param_type = std::conditional_t< std::is_pointer_v<Tp> || std::is_integral_v<Tp>, Tp const, reference >
using | Dispose = function<void ( dispose_param_type )>
| Dispose function signatue.

## Public Member Functions
| AutoDispose ()
| Default Ctor using default constructed value and no dispose function.
| AutoDispose (Dispose dispose_r)
| Ctor taking dispose function and using default constructed value.
| AutoDispose (value_type value_r)
| Ctor taking value and no dispose function.
| AutoDispose (value_type value_r, Dispose dispose_r)
| Ctor taking value and dispose function.
| operator reference () const
| Provide implicit conversion to `Tp&`.
reference | value () const
| Reference to the `Tp` object.
reference | operator* () const
| Reference to the `Tp` object.
value_type * | operator-> () const
| Pointer to the `Tp` object (asserted to be != NULL).
void | reset ()
| Reset to default Ctor values.
void | swap (AutoDispose &rhs) noexcept
| Exchange the contents of two AutoDispose objects.
bool | unique () const
| Returns true if this is the only AutoDispose instance managing the current data object.
const Dispose & | getDispose () const
| Return the current dispose function.
void | setDispose (const Dispose &dispose_r)
| Set a new dispose function.
void | resetDispose ()
| Set no dispose function.
void | swapDispose (Dispose &dispose_r)
| Exchange the dispose function.

## Private Attributes
shared_ptr< Impl > | _pimpl

## Related Symbols
(Note that these are not member symbols.)
template<class Tp>
std::ostream & | operator<< (std::ostream &str, const AutoDispose< Tp > &obj)
| Stream output of the `Tp` object.

## Detailed Description

template<class Tp>
class zypp::AutoDispose< Tp >

Reference counted access to a `Tp` object calling a custom `Dispose` function when the last AutoDispose handle to it is destroyed or reset.

Note
  As with pointers, constness of an `AutoDispose` object does not apply to the stored `Tp` object. If the stored `Tp` object should be immutable, you should use AutoDispose<const Tp>.

Pass a filename to the application and provide the appropriate code to be executed when the file is no longer needed:

struct FileCache
{
Pathname getFile();
void releaseFile( const Pathname & );
};
static FileCache cache;
void unlink( const Pathname & file_r );
AutoDispose<const Pathname> provideFile( ... )
{
if ( file_is_in_cache )
{
// will call 'cache.releaseFile( file )'
return AutoDispose<const Pathname>( cache.getFile(),
bind( &FileCache::releaseFile, ref(cache), _1 ) );
}
else if ( file_is_temporary )
{
// will call 'unlink( file )'
return AutoDispose<const Pathname>( file, unlink );
}
else if ( file_is_permanent )
{
// will do nothing.
return AutoDispose<const Pathname>( file );
}
else
{
// will do nothing.
return AutoDispose<const Pathname>();
}
}
zypp::AutoDispose
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
zypp::AutoDispose::AutoDispose
AutoDispose()
Default Ctor using default constructed value and no dispose function.
Definition AutoDispose.h:112
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47

Exception safe handling of temporary files:

void provideFileAt( const Pathname & destination )
{
AutoDispose<const Pathname> guard( destination, unlink );
// Any exception here will lead to 'unlink( destination )'
// ...
// On success: reset the dispose function to NOOP.
guard.resetDispose();
}

Definition at line 94 of file AutoDispose.h.

## Member Typedef Documentation

## ◆ param_type

template<class Tp>

using zypp::AutoDispose< Tp >::param_type = typename boost::call_traits<Tp>::param_type

Definition at line 97 of file AutoDispose.h.

## ◆ reference

template<class Tp>

using zypp::AutoDispose< Tp >::reference = typename boost::call_traits<Tp>::reference

Definition at line 98 of file AutoDispose.h.

## ◆ const_reference

template<class Tp>

using zypp::AutoDispose< Tp >::const_reference = typename boost::call_traits<Tp>::const_reference

Definition at line 99 of file AutoDispose.h.

## ◆ value_type

template<class Tp>

using zypp::AutoDispose< Tp >::value_type = Tp

Definition at line 100 of file AutoDispose.h.

## ◆ result_type

template<class Tp>

using zypp::AutoDispose< Tp >::result_type = typename boost::call_traits<Tp>::value_type

Definition at line 101 of file AutoDispose.h.

## ◆ dispose_param_type

template<class Tp>

using zypp::AutoDispose< Tp >::dispose_param_type = std::conditional_t< std::is_pointer_v<Tp> || std::is_integral_v<Tp>, Tp const, reference >

Definition at line 104 of file AutoDispose.h.

## ◆ Dispose

template<class Tp>

using zypp::AutoDispose< Tp >::Dispose = function<void ( dispose_param_type )>

Dispose function signatue.

Definition at line 108 of file AutoDispose.h.

## Constructor & Destructor Documentation

## ◆ AutoDispose() [1/4]

template<class Tp>

zypp::AutoDispose< Tp >::AutoDispose | ( |  | ) |
| inline

Default Ctor using default constructed value and no dispose function.

Definition at line 112 of file AutoDispose.h.

## ◆ AutoDispose() [2/4]

template<class Tp>

zypp::AutoDispose< Tp >::AutoDispose | ( | Dispose | dispose_r | ) |
| inline explicit

Ctor taking dispose function and using default constructed value.

Definition at line 117 of file AutoDispose.h.

## ◆ AutoDispose() [3/4]

template<class Tp>

zypp::AutoDispose< Tp >::AutoDispose | ( | value_type | value_r | ) |
| inline explicit

Ctor taking value and no dispose function.

Definition at line 122 of file AutoDispose.h.

## ◆ AutoDispose() [4/4]

template<class Tp>

zypp::AutoDispose< Tp >::AutoDispose | ( | value_type | value_r,
|  | Dispose | dispose_r )
| inline

Ctor taking value and dispose function.

Definition at line 127 of file AutoDispose.h.

## Member Function Documentation

## ◆ operator reference()

template<class Tp>

zypp::AutoDispose< Tp >::operator reference | ( |  | ) | const
| inline

Provide implicit conversion to `Tp&`.

Definition at line 134 of file AutoDispose.h.

## ◆ value()

template<class Tp>

reference zypp::AutoDispose< Tp >::value | ( |  | ) | const
| inline

Reference to the `Tp` object.

Definition at line 138 of file AutoDispose.h.

## ◆ operator*()

template<class Tp>

reference zypp::AutoDispose< Tp >::operator* | ( |  | ) | const
| inline

Reference to the `Tp` object.

Definition at line 142 of file AutoDispose.h.

## ◆ operator->()

template<class Tp>

value_type * zypp::AutoDispose< Tp >::operator-> | ( |  | ) | const
| inline

Pointer to the `Tp` object (asserted to be != NULL).

Definition at line 146 of file AutoDispose.h.

## ◆ reset()

template<class Tp>

void zypp::AutoDispose< Tp >::reset | ( |  | ) |
| inline

Reset to default Ctor values.

Definition at line 150 of file AutoDispose.h.

## ◆ swap()

template<class Tp>

void zypp::AutoDispose< Tp >::swap | ( | AutoDispose< Tp > & | rhs | ) |
| inline noexcept

Exchange the contents of two AutoDispose objects.

Definition at line 154 of file AutoDispose.h.

## ◆ unique()

template<class Tp>

bool zypp::AutoDispose< Tp >::unique | ( |  | ) | const
| inline

Returns true if this is the only AutoDispose instance managing the current data object.

Definition at line 158 of file AutoDispose.h.

## ◆ getDispose()

template<class Tp>

const Dispose & zypp::AutoDispose< Tp >::getDispose | ( |  | ) | const
| inline

Return the current dispose function.

Definition at line 163 of file AutoDispose.h.

## ◆ setDispose()

template<class Tp>

void zypp::AutoDispose< Tp >::setDispose | ( | const Dispose & | dispose_r | ) |
| inline

Set a new dispose function.

Definition at line 167 of file AutoDispose.h.

## ◆ resetDispose()

template<class Tp>

void zypp::AutoDispose< Tp >::resetDispose | ( |  | ) |
| inline

Set no dispose function.

Definition at line 171 of file AutoDispose.h.

## ◆ swapDispose()

template<class Tp>

void zypp::AutoDispose< Tp >::swapDispose | ( | Dispose & | dispose_r | ) |
| inline

Exchange the dispose function.

Definition at line 175 of file AutoDispose.h.

## Friends And Related Symbol Documentation

## ◆ operator<<()

template<class Tp>

std::ostream & operator<< | ( | std::ostream & | str,
|  | const AutoDispose< Tp > & | obj )
| related

Stream output of the `Tp` object.

Definition at line 300 of file AutoDispose.h.

## Member Data Documentation

## ◆ _pimpl

template<class Tp>

shared_ptr<Impl> zypp::AutoDispose< Tp >::_pimpl
| private

Definition at line 199 of file AutoDispose.h.

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

- zypp-core/AutoDispose.h