libzypp 17.31.7
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 // bsc#1194597: Header is exposed in the public API, so it must be c++11:
102 // using dispose_param_type = std::conditional_t< std::is_pointer_v<Tp> || std::is_integral_v<Tp>, Tp const, reference >;
103 using dispose_param_type = typename std::conditional< std::is_pointer<Tp>::value || std::is_integral<Tp>::value, Tp const, reference >::type;
104
105 public:
107 using Dispose = function<void ( dispose_param_type )>;
108
109 public:
112 : _pimpl( new Impl( value_type() ) )
113 {}
114
116 explicit AutoDispose( const Dispose & dispose_r )
117 : _pimpl( new Impl( value_type(), dispose_r ) )
118 {}
119
121 explicit AutoDispose( const value_type & value_r )
122 : _pimpl( new Impl( value_r ) )
123 {}
124
126 AutoDispose( const value_type & value_r, const Dispose & dispose_r )
127 : _pimpl( new Impl( value_r, dispose_r ) )
128 {}
129
131 explicit AutoDispose( value_type &&value_r )
132 : _pimpl( new Impl( std::move(value_r) ) )
133 {}
134
136 AutoDispose( value_type &&value_r, const Dispose & dispose_r )
137 : _pimpl( new Impl( std::move(value_r), dispose_r ) )
138 {}
139
140 public:
141
143 operator reference() const
144 { return _pimpl->_value; }
145
147 reference value() const
148 { return _pimpl->_value; }
149
151 reference operator*() const
152 { return _pimpl->_value; }
153
155 value_type * operator->() const
156 { return & _pimpl->_value; }
157
159 void reset()
160 { AutoDispose().swap( *this ); }
161
163 void swap( AutoDispose & rhs )
164 { _pimpl.swap( rhs._pimpl ); }
165
167 bool unique () const
168 { return _pimpl.unique(); }
169
170 public:
172 const Dispose & getDispose() const
173 { return _pimpl->_dispose; }
174
176 void setDispose( const Dispose & dispose_r )
177 { _pimpl->_dispose = dispose_r; }
178
180 void resetDispose()
181 { setDispose( Dispose() ); }
182
184 void swapDispose( Dispose & dispose_r )
185 { _pimpl->_dispose.swap( dispose_r ); }
186
187 private:
188 struct Impl : private base::NonCopyable
189 {
190 template <typename T>
191 Impl( T &&value_r )
192 : _value( std::forward<T>(value_r) )
193 {}
194 template <typename T, typename D>
195 Impl( T &&value_r, D &&dispose_r )
196 : _value( std::forward<T>(value_r) )
197 , _dispose( std::forward<D>(dispose_r) )
198 {}
199 ~Impl()
200 {
201 if ( _dispose )
202 try { _dispose( _value ); } catch(...) {}
203 }
206 };
207
209 };
210
211 template<>
212 class AutoDispose<void>
213 {
214 public:
216 typedef function<void ()> Dispose;
217
218 public:
221 : _pimpl( new Impl() )
222 {}
223
225 explicit AutoDispose( const Dispose & dispose_r )
226 : _pimpl( new Impl( dispose_r ) )
227 {}
228
229 public:
230
232 void reset()
233 { AutoDispose().swap( *this ); }
234
236 void swap( AutoDispose & rhs )
237 { _pimpl.swap( rhs._pimpl ); }
238
239 public:
241 const Dispose & getDispose() const
242 { return _pimpl->_dispose; }
243
245 void setDispose( const Dispose & dispose_r )
246 { _pimpl->_dispose = dispose_r; }
247
249 void resetDispose()
250 { setDispose( Dispose() ); }
251
253 void swapDispose( Dispose & dispose_r )
254 { _pimpl->_dispose.swap( dispose_r ); }
255
256 private:
257 struct Impl : private base::NonCopyable
258 {
259 Impl( )
260 {}
261
262 Impl( const Dispose & dispose_r )
263 : _dispose( dispose_r )
264 {}
265
266 ~Impl()
267 {
268 if ( _dispose )
269 try { _dispose(); } catch(...) {}
270 }
272 };
274 };
275
285 using OnScopeExit = AutoDispose<void>;
286
288
290 template<class Tp>
291 inline std::ostream & operator<<( std::ostream & str, const AutoDispose<Tp> & obj )
292 { return str << obj.value(); }
293
294
300 struct AutoFD : public AutoDispose<int>
301 {
302 AutoFD( int fd_r = -1 ) : AutoDispose<int>( fd_r, [] ( int fd_r ) { if ( fd_r != -1 ) ::close( fd_r ); } ) {}
303 };
304
311 struct AutoFILE : public AutoDispose<FILE*>
312 {
313 AutoFILE( FILE* file_r = nullptr ) : AutoDispose<FILE*>( file_r, [] ( FILE* file_r ) { if ( file_r ) ::fclose( file_r ); } ) {}
314 };
315
321 template <typename Tp>
322 struct AutoFREE : public AutoDispose<Tp*>
323 {
324 AutoFREE( Tp* ptr_r = nullptr ) : AutoDispose<Tp*>( ptr_r, [] ( Tp* ptr_r ) { if ( ptr_r ) ::free( ptr_r ); } ) {}
325 AutoFREE( void* ptr_r ) : AutoFREE( static_cast<Tp*>(ptr_r) ) {}
326 };
327
328 template <>
329 struct AutoFREE<void> : public AutoDispose<void*>
330 {
331 AutoFREE( void* ptr_r = nullptr ) : AutoDispose<void*>( ptr_r, [] ( void* ptr_r ) { if ( ptr_r ) ::free( ptr_r ); } ) {}
332 };
334} // namespace zypp
336#endif // ZYPP_AUTODISPOSE_H
RepoManager implementation.
value_type * operator->() const
Pointer to the Tp object (asserted to be != NULL).
Definition: AutoDispose.h:155
reference value() const
Reference to the Tp object.
Definition: AutoDispose.h:147
const Dispose & getDispose() const
Return the current dispose function.
Definition: AutoDispose.h:172
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:180
function< void(dispose_param_type)> Dispose
Dispose function signatue.
Definition: AutoDispose.h:107
AutoDispose()
Default Ctor using default constructed value and no dispose function.
Definition: AutoDispose.h:111
reference operator*() const
Reference to the Tp object.
Definition: AutoDispose.h:151
void reset()
Reset to default Ctor values.
Definition: AutoDispose.h:159
shared_ptr< Impl > _pimpl
Definition: AutoDispose.h:208
void swap(AutoDispose &rhs)
Exchange the contents of two AutoDispose objects.
Definition: AutoDispose.h:163
void swapDispose(Dispose &dispose_r)
Exchange the dispose function.
Definition: AutoDispose.h:184
bool unique() const
Returns true if this is the only AutoDispose instance managing the current data object.
Definition: AutoDispose.h:167
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:176
boost::call_traits< Tp >::value_type result_type
Definition: AutoDispose.h:100
typename std::conditional< std::is_pointer< Tp >::value||std::is_integral< Tp >::value, Tp const, reference >::type dispose_param_type
Definition: AutoDispose.h:103
Definition: Arch.h:352
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 SerialNumber &obj)
Definition: SerialNumber.cc:52
AutoDispose< void > OnScopeExit
Definition: AutoDispose.h:285
AutoFD(int fd_r=-1)
Definition: AutoDispose.h:302
AutoFILE(FILE *file_r=nullptr)
Definition: AutoDispose.h:313
AutoFREE(Tp *ptr_r=nullptr)
Definition: AutoDispose.h:324