libzypp 17.30.3
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
166 public:
168 const Dispose & getDispose() const
169 { return _pimpl->_dispose; }
170
172 void setDispose( const Dispose & dispose_r )
173 { _pimpl->_dispose = dispose_r; }
174
176 void resetDispose()
177 { setDispose( Dispose() ); }
178
180 void swapDispose( Dispose & dispose_r )
181 { _pimpl->_dispose.swap( dispose_r ); }
182
183 private:
184 struct Impl : private base::NonCopyable
185 {
186 template <typename T>
187 Impl( T &&value_r )
188 : _value( std::forward<T>(value_r) )
189 {}
190 template <typename T, typename D>
191 Impl( T &&value_r, D &&dispose_r )
192 : _value( std::forward<T>(value_r) )
193 , _dispose( std::forward<D>(dispose_r) )
194 {}
195 ~Impl()
196 {
197 if ( _dispose )
198 try { _dispose( _value ); } catch(...) {}
199 }
202 };
203
205 };
206
207 template<>
208 class AutoDispose<void>
209 {
210 public:
212 typedef function<void ()> Dispose;
213
214 public:
217 : _pimpl( new Impl() )
218 {}
219
221 explicit AutoDispose( const Dispose & dispose_r )
222 : _pimpl( new Impl( dispose_r ) )
223 {}
224
225 public:
226
228 void reset()
229 { AutoDispose().swap( *this ); }
230
232 void swap( AutoDispose & rhs )
233 { _pimpl.swap( rhs._pimpl ); }
234
235 public:
237 const Dispose & getDispose() const
238 { return _pimpl->_dispose; }
239
241 void setDispose( const Dispose & dispose_r )
242 { _pimpl->_dispose = dispose_r; }
243
245 void resetDispose()
246 { setDispose( Dispose() ); }
247
249 void swapDispose( Dispose & dispose_r )
250 { _pimpl->_dispose.swap( dispose_r ); }
251
252 private:
253 struct Impl : private base::NonCopyable
254 {
255 Impl( )
256 {}
257
258 Impl( const Dispose & dispose_r )
259 : _dispose( dispose_r )
260 {}
261
262 ~Impl()
263 {
264 if ( _dispose )
265 try { _dispose(); } catch(...) {}
266 }
268 };
270 };
271
281 using OnScopeExit = AutoDispose<void>;
282
284
286 template<class Tp>
287 inline std::ostream & operator<<( std::ostream & str, const AutoDispose<Tp> & obj )
288 { return str << obj.value(); }
289
290
296 struct AutoFD : public AutoDispose<int>
297 {
298 AutoFD( int fd_r = -1 ) : AutoDispose<int>( fd_r, [] ( int fd_r ) { if ( fd_r != -1 ) ::close( fd_r ); } ) {}
299 };
300
307 struct AutoFILE : public AutoDispose<FILE*>
308 {
309 AutoFILE( FILE* file_r = nullptr ) : AutoDispose<FILE*>( file_r, [] ( FILE* file_r ) { if ( file_r ) ::fclose( file_r ); } ) {}
310 };
311
317 template <typename Tp>
318 struct AutoFREE : public AutoDispose<Tp*>
319 {
320 AutoFREE( Tp* ptr_r = nullptr ) : AutoDispose<Tp*>( ptr_r, [] ( Tp* ptr_r ) { if ( ptr_r ) ::free( ptr_r ); } ) {}
321 AutoFREE( void* ptr_r ) : AutoFREE( static_cast<Tp*>(ptr_r) ) {}
322 };
323
324 template <>
325 struct AutoFREE<void> : public AutoDispose<void*>
326 {
327 AutoFREE( void* ptr_r = nullptr ) : AutoDispose<void*>( ptr_r, [] ( void* ptr_r ) { if ( ptr_r ) ::free( ptr_r ); } ) {}
328 };
330} // namespace zypp
332#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:168
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:176
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:204
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:180
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:172
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:281
AutoFD(int fd_r=-1)
Definition: AutoDispose.h:298
AutoFILE(FILE *file_r=nullptr)
Definition: AutoDispose.h:309
AutoFREE(Tp *ptr_r=nullptr)
Definition: AutoDispose.h:320