12#ifndef ZYPP_AUTODISPOSE_H
13#define ZYPP_AUTODISPOSE_H
16#include <boost/call_traits.hpp>
18#include <zypp-core/base/NonCopyable.h>
19#include <zypp-core/base/PtrTypes.h>
20#include <zypp-core/base/Function.h>
21#include <zypp-core/Pathname.h>
96 typedef typename boost::call_traits<Tp>::param_type
param_type;
97 typedef typename boost::call_traits<Tp>::reference
reference;
98 typedef typename boost::call_traits<Tp>::const_reference
const_reference;
100 typedef typename boost::call_traits<Tp>::value_type
result_type;
132 {
return _pimpl->_value; }
136 {
return _pimpl->_value; }
140 {
return _pimpl->_value; }
144 {
return &
_pimpl->_value; }
152 {
_pimpl.swap( rhs._pimpl ); }
157 {
return _pimpl->_dispose; }
161 {
_pimpl->_dispose = dispose_r; }
169 {
_pimpl->_dispose.swap( dispose_r ); }
194 class AutoDispose<void>
198 typedef function<void ()>
Dispose;
219 {
_pimpl.swap( rhs._pimpl ); }
224 {
return _pimpl->_dispose; }
228 {
_pimpl->_dispose = dispose_r; }
236 {
_pimpl->_dispose.swap( dispose_r ); }
273 inline std::ostream &
operator<<( std::ostream &
str,
const AutoDispose<Tp> & obj )
274 {
return str << obj.value(); }
282 struct AutoFD :
public AutoDispose<int>
284 AutoFD(
int fd_r = -1 ) :
AutoDispose<int>( fd_r, [] ( int fd_r ) {
if ( fd_r != -1 ) ::close( fd_r ); } ) {}
293 struct AutoFILE :
public AutoDispose<FILE*>
295 AutoFILE( FILE* file_r =
nullptr ) :
AutoDispose<FILE*>( file_r, [] ( FILE* file_r ) {
if ( file_r ) ::fclose( file_r ); } ) {}
303 template <
typename Tp>
304 struct AutoFREE :
public AutoDispose<Tp*>
306 AutoFREE( Tp* ptr_r =
nullptr ) :
AutoDispose<Tp*>( ptr_r, [] ( Tp* ptr_r ) {
if ( ptr_r ) ::free( ptr_r ); } ) {}
311 struct AutoFREE<void> :
public AutoDispose<void*>
313 AutoFREE(
void* ptr_r =
nullptr ) :
AutoDispose<void*>( ptr_r, [] ( void* ptr_r ) {
if ( ptr_r ) ::free( ptr_r ); } ) {}
value_type * operator->() const
Pointer to the Tp object (asserted to be != NULL).
reference value() const
Reference to the Tp object.
const Dispose & getDispose() const
Return the current dispose function.
void resetDispose()
Set no dispose function.
function< void(dispose_param_type)> Dispose
Dispose function signatue.
AutoDispose()
Default Ctor using default constructed value and no dispose function.
std::conditional_t< std::is_pointer_v< Tp >||std::is_integral_v< Tp >, Tp const, reference > dispose_param_type
reference operator*() const
Reference to the Tp object.
void reset()
Reset to default Ctor values.
shared_ptr< Impl > _pimpl
void swap(AutoDispose &rhs)
Exchange the contents of two AutoDispose objects.
void swapDispose(Dispose &dispose_r)
Exchange the dispose function.
boost::call_traits< Tp >::param_type param_type
boost::call_traits< Tp >::reference reference
boost::call_traits< Tp >::const_reference const_reference
void setDispose(const Dispose &dispose_r)
Set a new dispose function.
boost::call_traits< Tp >::value_type result_type
String related utilities and Regular expression matching.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & operator<<(std::ostream &str, const Exception &obj)
AutoDispose< void > OnScopeExit
AutoFILE(FILE *file_r=nullptr)
AutoFREE(Tp *ptr_r=nullptr)