libzypp: zypp-core/AutoDispose.h Source File

libzypp 17.36.6

AutoDispose.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_AUTODISPOSE_H
13#define ZYPP_AUTODISPOSE_H
14
15#include <iosfwd>
16#include <boost/call_traits.hpp>
17#include <utility>
18
19#include <zypp-core/base/NonCopyable.h>
20#include <zypp-core/base/PtrTypes.h>
21#include <zypp-core/base/Function.h>
22#include <zypp-core/Pathname.h>
23
25namespace zypp
26{
27
29 //
30 // CLASS NAME : AutoDispose<Tp>
31 //
93 template<class Tp>
94 class AutoDispose
95 {
96 public:
97 using param_type = typename boost::call_traits<Tp>::param_type;
98 using reference = typename boost::call_traits<Tp>::reference;
99 using const_reference = typename boost::call_traits<Tp>::const_reference;
100 using value_type = Tp;
101 using result_type = typename boost::call_traits<Tp>::value_type;
102 // bsc#1194597: Header is exposed in the public API, so it must be c++11:
103 // using dispose_param_type = std::conditional_t< std::is_pointer_v<Tp> || std::is_integral_v<Tp>, Tp const, reference >;
104 using dispose_param_type = std::conditional_t< std::is_pointer_v<Tp> || std::is_integral_v<Tp>, Tp const, reference >;
105
106 public:
108 using Dispose = function<void ( dispose_param_type )>;
109
110 public:
112 AutoDispose()
113 : _pimpl( new Impl( value_type() ) )
114 {}
115
117 explicit AutoDispose( Dispose dispose_r )
118 : _pimpl( new Impl( value_type(), std::move(dispose_r) ) )
119 {}
120
122 explicit AutoDispose( value_type value_r )
123 : _pimpl( new Impl( std::move(value_r) ) )
124 {}
125
127 AutoDispose( value_type value_r, Dispose dispose_r )
128 : _pimpl( new Impl( std::move(value_r), std::move(dispose_r) ) )
129 {}
130
131 public:
132
134 operator reference() const
135 { return _pimpl->_value; }
136
138 reference value() const
139 { return _pimpl->_value; }
140
142 reference operator*() const
143 { return _pimpl->_value; }
144
146 value_type * operator->() const
147 { return & _pimpl->_value; }
148
150 void reset()
151 { AutoDispose().swap( *this ); }
152
154 void swap( AutoDispose & rhs ) noexcept
155 { _pimpl.swap( rhs._pimpl ); }
156
158 bool unique () const
159 { return _pimpl.unique(); }
160
161 public:
163 const Dispose & getDispose() const
164 { return _pimpl->_dispose; }
165
167 void setDispose( const Dispose & dispose_r )
168 { _pimpl->_dispose = dispose_r; }
169
171 void resetDispose()
172 { setDispose( Dispose() ); }
173
175 void swapDispose( Dispose & dispose_r )
176 { _pimpl->_dispose.swap( dispose_r ); }
177
178 private:
179 struct Impl : private base::NonCopyable
180 {
181 template <typename T>
182 Impl( T &&value_r )
183 : _value( std::forward<T>(value_r) )
184 {}
185 template <typename T, typename D>
186 Impl( T &&value_r, D &&dispose_r )
187 : _value( std::forward<T>(value_r) )
188 , _dispose( std::forward<D>(dispose_r) )
189 {}
190 ~Impl()
191 {
192 if ( _dispose )
193 try { _dispose( _value ); } catch(...) {}
194 }
195 value_type _value;
196 Dispose _dispose;
197 };
198
199 shared_ptr<Impl> _pimpl;
200 };
201
202 template<>
203 class AutoDispose<void>
204 {
205 public:
207 using Dispose = function<void ()>;
208
209 public:
211 AutoDispose()
212 : _pimpl( new Impl() )
213 {}
214
216 explicit AutoDispose( const Dispose & dispose_r )
217 : _pimpl( new Impl( dispose_r ) )
218 {}
219
220 public:
221
223 void reset()
224 { AutoDispose().swap( *this ); }
225
227 void swap( AutoDispose & rhs ) noexcept
228 { _pimpl.swap( rhs._pimpl ); }
229
230 public:
232 const Dispose & getDispose() const
233 { return _pimpl->_dispose; }
234
236 void setDispose( const Dispose & dispose_r )
237 { _pimpl->_dispose = dispose_r; }
238
240 void resetDispose()
241 { setDispose( Dispose() ); }
242
244 void swapDispose( Dispose & dispose_r )
245 { _pimpl->_dispose.swap( dispose_r ); }
246
247 private:
248 struct Impl : private base::NonCopyable
249 {
250 Impl( )
251 {}
252
253 Impl( Dispose dispose_r )
254 : _dispose(std::move( dispose_r ))
255 {}
256
257 ~Impl()
258 {
259 if ( _dispose )
260 try { _dispose(); } catch(...) {}
261 }
262 Dispose _dispose;
263 };
264 shared_ptr<Impl> _pimpl;
265 };
266
276 using OnScopeExit = AutoDispose<void>;
277
278 struct Deferred : public AutoDispose<void>
279 {
280 template <typename F>
281 Deferred( F&&cb );
282 };
283
284 template<typename F>
285 Deferred::Deferred(F &&cb) : AutoDispose( std::forward<F>(cb) ){}
286
287#define __zypp_defer_concatenate(__lhs, __rhs) \
288 __lhs##__rhs
289
290#define __zypp_defer_declarator(__id) \
291 zypp::Deferred __zypp_defer_concatenate(__defer, __id) = [&]()
292
293#define zypp_defer \
294 __zypp_defer_declarator(__LINE__)
295
297
299 template<class Tp>
300 inline std::ostream & operator<<( std::ostream & str, const AutoDispose<Tp> & obj )
301 { return str << obj.value(); }
302
303
309 struct AutoFD : public AutoDispose<int>
310 {
311 AutoFD( int fd_r = -1 ) : AutoDispose<int>( fd_r, [] ( int fd_r ) { if ( fd_r != -1 ) ::close( fd_r ); } ) {}
312 };
313
320 struct AutoFILE : public AutoDispose<FILE*>
321 {
322 AutoFILE( FILE* file_r = nullptr ) : AutoDispose<FILE*>( file_r, [] ( FILE* file_r ) { if ( file_r ) ::fclose( file_r ); } ) {}
323 };
324
330 template <typename Tp>
331 struct AutoFREE : public AutoDispose<Tp*>
332 {
333 AutoFREE( Tp* ptr_r = nullptr ) : AutoDispose<Tp*>( ptr_r, [] ( Tp* ptr_r ) { if ( ptr_r ) ::free( ptr_r ); } ) {}
334 AutoFREE( void* ptr_r ) : AutoFREE( static_cast<Tp*>(ptr_r) ) {}
335 };
336
337 template <>
338 struct AutoFREE<void> : public AutoDispose<void*>
339 {
340 AutoFREE( void* ptr_r = nullptr ) : AutoDispose<void*>( ptr_r, [] ( void* ptr_r ) { if ( ptr_r ) ::free( ptr_r ); } ) {}
341 };
342
344} // namespace zypp
346#endif // ZYPP_AUTODISPOSE_H
Impl
shared_ptr
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::operator->
value_type * operator->() const
Pointer to the Tp object (asserted to be != NULL).
Definition AutoDispose.h:146
zypp::AutoDispose::swap
void swap(AutoDispose &rhs) noexcept
Exchange the contents of two AutoDispose objects.
Definition AutoDispose.h:154
zypp::AutoDispose::value
reference value() const
Reference to the Tp object.
Definition AutoDispose.h:138
zypp::AutoDispose::operator<<
std::ostream & operator<<(std::ostream &str, const AutoDispose< Tp > &obj)
Stream output of the Tp object.
Definition AutoDispose.h:300
zypp::AutoDispose::getDispose
const Dispose & getDispose() const
Return the current dispose function.
Definition AutoDispose.h:163
zypp::AutoDispose::resetDispose
void resetDispose()
Set no dispose function.
Definition AutoDispose.h:171
zypp::AutoDispose::Dispose
function< void(dispose_param_type)> Dispose
Dispose function signatue.
Definition AutoDispose.h:108
zypp::AutoDispose::AutoDispose
AutoDispose(Dispose dispose_r)
Ctor taking dispose function and using default constructed value.
Definition AutoDispose.h:117
zypp::AutoDispose::AutoDispose
AutoDispose()
Default Ctor using default constructed value and no dispose function.
Definition AutoDispose.h:112
zypp::AutoDispose::dispose_param_type
std::conditional_t< std::is_pointer_v< Tp >||std::is_integral_v< Tp >, Tp const, reference > dispose_param_type
Definition AutoDispose.h:104
zypp::AutoDispose::operator*
reference operator*() const
Reference to the Tp object.
Definition AutoDispose.h:142
zypp::AutoDispose::const_reference
typename boost::call_traits< Tp >::const_reference const_reference
Definition AutoDispose.h:99
zypp::AutoDispose::reset
void reset()
Reset to default Ctor values.
Definition AutoDispose.h:150
zypp::AutoDispose< const Pathname >::_pimpl
shared_ptr< Impl > _pimpl
Definition AutoDispose.h:199
zypp::AutoDispose::swapDispose
void swapDispose(Dispose &dispose_r)
Exchange the dispose function.
Definition AutoDispose.h:175
zypp::AutoDispose::unique
bool unique() const
Returns true if this is the only AutoDispose instance managing the current data object.
Definition AutoDispose.h:158
zypp::AutoDispose::setDispose
void setDispose(const Dispose &dispose_r)
Set a new dispose function.
Definition AutoDispose.h:167
zypp::AutoDispose::param_type
typename boost::call_traits< Tp >::param_type param_type
Definition AutoDispose.h:97
zypp::AutoDispose::reference
typename boost::call_traits< Tp >::reference reference
Definition AutoDispose.h:98
zypp::AutoDispose::AutoDispose
AutoDispose(value_type value_r, Dispose dispose_r)
Ctor taking value and dispose function.
Definition AutoDispose.h:127
zypp::AutoDispose::AutoDispose
AutoDispose(value_type value_r)
Ctor taking value and no dispose function.
Definition AutoDispose.h:122
zypp::AutoDispose::value_type
Tp value_type
Definition AutoDispose.h:100
zypp::AutoDispose::result_type
typename boost::call_traits< Tp >::value_type result_type
Definition AutoDispose.h:101
std
Definition Arch.h:364
std::is_integral_v
constexpr bool is_integral_v
Definition TypeTraits.h:30
std::conditional_t
typename conditional< B, T, F >::type conditional_t
Definition TypeTraits.h:39
str
String related utilities and Regular expression matching.
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::OnScopeExit
AutoDispose< void > OnScopeExit
Definition AutoDispose.h:276
zypp::AutoDispose::Impl::_dispose
Dispose _dispose
Definition AutoDispose.h:196
zypp::AutoDispose::Impl::_value
value_type _value
Definition AutoDispose.h:195
zypp::AutoDispose::Impl::Impl
Impl(T &&value_r, D &&dispose_r)
Definition AutoDispose.h:186
zypp::AutoDispose::Impl::~Impl
~Impl()
Definition AutoDispose.h:190
zypp::AutoDispose::Impl::Impl
Impl(T &&value_r)
Definition AutoDispose.h:182
zypp::AutoDispose< void >::Impl::~Impl
~Impl()
Definition AutoDispose.h:257
zypp::AutoDispose< void >::Impl::Impl
Impl()
Definition AutoDispose.h:250
zypp::AutoDispose< void >::Impl::_dispose
Dispose _dispose
Definition AutoDispose.h:262
zypp::AutoDispose< void >::Impl::Impl
Impl(Dispose dispose_r)
Definition AutoDispose.h:253
zypp::AutoFD::AutoFD
AutoFD(int fd_r=-1)
Definition AutoDispose.h:311
zypp::AutoFILE::AutoFILE
AutoFILE(FILE *file_r=nullptr)
Definition AutoDispose.h:322
zypp::AutoFREE< void >::AutoFREE
AutoFREE(void *ptr_r=nullptr)
Definition AutoDispose.h:340
zypp::AutoFREE::AutoFREE
AutoFREE(void *ptr_r)
Definition AutoDispose.h:334
zypp::AutoFREE::AutoFREE
AutoFREE(Tp *ptr_r=nullptr)
Definition AutoDispose.h:333
zypp::Deferred::Deferred
Deferred(F &&cb)
Definition AutoDispose.h:285
Pathname.h
Function.h
NonCopyable.h
PtrTypes.h