libzypp 17.29.1
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
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>
22
24namespace zypp
25{
26
28 //
29 // CLASS NAME : AutoDispose<Tp>
30 //
92 template<class Tp>
93 class AutoDispose
94 {
95 public:
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;
99 typedef Tp value_type;
100 typedef typename boost::call_traits<Tp>::value_type result_type;
101 using dispose_param_type = std::conditional_t< std::is_pointer_v<Tp> || std::is_integral_v<Tp>, Tp const, reference >;
102
103 public:
105 using Dispose = function<void ( dispose_param_type )>;
106
107 public:
110 : _pimpl( new Impl( value_type() ) )
111 {}
112
114 explicit AutoDispose( const Dispose & dispose_r )
115 : _pimpl( new Impl( value_type(), dispose_r ) )
116 {}
117
119 explicit AutoDispose( const value_type & value_r )
120 : _pimpl( new Impl( value_r ) )
121 {}
122
124 AutoDispose( const value_type & value_r, const Dispose & dispose_r )
125 : _pimpl( new Impl( value_r, dispose_r ) )
126 {}
127
129 explicit AutoDispose( value_type &&value_r )
130 : _pimpl( new Impl( std::move(value_r) ) )
131 {}
132
134 AutoDispose( value_type &&value_r, const Dispose & dispose_r )
135 : _pimpl( new Impl( std::move(value_r), dispose_r ) )
136 {}
137
138 public:
139
141 operator reference() const
142 { return _pimpl->_value; }
143
145 reference value() const
146 { return _pimpl->_value; }
147
149 reference operator*() const
150 { return _pimpl->_value; }
151
153 value_type * operator->() const
154 { return & _pimpl->_value; }
155
157 void reset()
158 { AutoDispose().swap( *this ); }
159
161 void swap( AutoDispose & rhs )
162 { _pimpl.swap( rhs._pimpl ); }
163
164 public:
166 const Dispose & getDispose() const
167 { return _pimpl->_dispose; }
168
170 void setDispose( const Dispose & dispose_r )
171 { _pimpl->_dispose = dispose_r; }
172
174 void resetDispose()
175 { setDispose( Dispose() ); }
176
178 void swapDispose( Dispose & dispose_r )
179 { _pimpl->_dispose.swap( dispose_r ); }
180
181 private:
182 struct Impl : private base::NonCopyable
183 {
184 template <typename T>
185 Impl( T &&value_r )
186 : _value( std::forward<T>(value_r) )
187 {}
188 template <typename T, typename D>
189 Impl( T &&value_r, D &&dispose_r )
190 : _value( std::forward<T>(value_r) )
191 , _dispose( std::forward<D>(dispose_r) )
192 {}
193 ~Impl()
194 {
195 if ( _dispose )
196 try { _dispose( _value ); } catch(...) {}
197 }
200 };
201
203 };
204
205 template<>
206 class AutoDispose<void>
207 {
208 public:
210 typedef function<void ()> Dispose;
211
212 public:
215 : _pimpl( new Impl() )
216 {}
217
219 explicit AutoDispose( const Dispose & dispose_r )
220 : _pimpl( new Impl( dispose_r ) )
221 {}
222
223 public:
224
226 void reset()
227 { AutoDispose().swap( *this ); }
228
230 void swap( AutoDispose & rhs )
231 { _pimpl.swap( rhs._pimpl ); }
232
233 public:
235 const Dispose & getDispose() const
236 { return _pimpl->_dispose; }
237
239 void setDispose( const Dispose & dispose_r )
240 { _pimpl->_dispose = dispose_r; }
241
243 void resetDispose()
244 { setDispose( Dispose() ); }
245
247 void swapDispose( Dispose & dispose_r )
248 { _pimpl->_dispose.swap( dispose_r ); }
249
250 private:
251 struct Impl : private base::NonCopyable
252 {
253 Impl( )
254 {}
255
256 Impl( const Dispose & dispose_r )
257 : _dispose( dispose_r )
258 {}
259
260 ~Impl()
261 {
262 if ( _dispose )
263 try { _dispose(); } catch(...) {}
264 }
266 };
268 };
269
279 using OnScopeExit = AutoDispose<void>;
280
282
284 template<class Tp>
285 inline std::ostream & operator<<( std::ostream & str, const AutoDispose<Tp> & obj )
286 { return str << obj.value(); }
287
288
294 struct AutoFD : public AutoDispose<int>
295 {
296 AutoFD( int fd_r = -1 ) : AutoDispose<int>( fd_r, [] ( int fd_r ) { if ( fd_r != -1 ) ::close( fd_r ); } ) {}
297 };
298
305 struct AutoFILE : public AutoDispose<FILE*>
306 {
307 AutoFILE( FILE* file_r = nullptr ) : AutoDispose<FILE*>( file_r, [] ( FILE* file_r ) { if ( file_r ) ::fclose( file_r ); } ) {}
308 };
309
315 template <typename Tp>
316 struct AutoFREE : public AutoDispose<Tp*>
317 {
318 AutoFREE( Tp* ptr_r = nullptr ) : AutoDispose<Tp*>( ptr_r, [] ( Tp* ptr_r ) { if ( ptr_r ) ::free( ptr_r ); } ) {}
319 AutoFREE( void* ptr_r ) : AutoFREE( static_cast<Tp*>(ptr_r) ) {}
320 };
321
322 template <>
323 struct AutoFREE<void> : public AutoDispose<void*>
324 {
325 AutoFREE( void* ptr_r = nullptr ) : AutoDispose<void*>( ptr_r, [] ( void* ptr_r ) { if ( ptr_r ) ::free( ptr_r ); } ) {}
326 };
328} // namespace zypp
330#endif // ZYPP_AUTODISPOSE_H
value_type * operator->() const
Pointer to the Tp object (asserted to be != NULL).
Definition: AutoDispose.h:153
reference value() const
Reference to the Tp object.
Definition: AutoDispose.h:145
const Dispose & getDispose() const
Return the current dispose function.
Definition: AutoDispose.h:166
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:174
function< void(dispose_param_type)> Dispose
Dispose function signatue.
Definition: AutoDispose.h:105
AutoDispose()
Default Ctor using default constructed value and no dispose function.
Definition: AutoDispose.h:109
std::conditional_t< std::is_pointer_v< Tp >||std::is_integral_v< Tp >, Tp const, reference > dispose_param_type
Definition: AutoDispose.h:101
reference operator*() const
Reference to the Tp object.
Definition: AutoDispose.h:149
void reset()
Reset to default Ctor values.
Definition: AutoDispose.h:157
shared_ptr< Impl > _pimpl
Definition: AutoDispose.h:202
void swap(AutoDispose &rhs)
Exchange the contents of two AutoDispose objects.
Definition: AutoDispose.h:161
void swapDispose(Dispose &dispose_r)
Exchange the dispose function.
Definition: AutoDispose.h:178
boost::call_traits< Tp >::param_type param_type
Definition: AutoDispose.h:96
boost::call_traits< Tp >::reference reference
Definition: AutoDispose.h:97
boost::call_traits< Tp >::const_reference const_reference
Definition: AutoDispose.h:98
void setDispose(const Dispose &dispose_r)
Set a new dispose function.
Definition: AutoDispose.h:170
boost::call_traits< Tp >::value_type result_type
Definition: AutoDispose.h:100
Definition: Hash.h:38
String related utilities and Regular expression matching.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:167
AutoDispose< void > OnScopeExit
Definition: AutoDispose.h:279
AutoFD(int fd_r=-1)
Definition: AutoDispose.h:296
AutoFILE(FILE *file_r=nullptr)
Definition: AutoDispose.h:307
AutoFREE(Tp *ptr_r=nullptr)
Definition: AutoDispose.h:318