libzypp  17.30.2
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 
24 namespace zypp
25 {
26 
28  //
29  // CLASS NAME : AutoDispose<Tp>
30  //
92  template<class Tp>
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 
152  { return _pimpl->_value; }
153 
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 
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  {}
196  {
197  if ( _dispose )
198  try { _dispose( _value ); } catch(...) {}
199  }
202  };
203 
204  shared_ptr<Impl> _pimpl;
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 
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 
263  {
264  if ( _dispose )
265  try { _dispose(); } catch(...) {}
266  }
268  };
269  shared_ptr<Impl> _pimpl;
270  };
271 
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.
function< void()> Dispose
Dispose function signatue.
Definition: AutoDispose.h:212
void reset()
Reset to default Ctor values.
Definition: AutoDispose.h:228
void swapDispose(Dispose &dispose_r)
Exchange the dispose function.
Definition: AutoDispose.h:249
void swap(AutoDispose &rhs)
Exchange the contents of two AutoDispose objects.
Definition: AutoDispose.h:232
AutoDispose()
Default Ctor using default constructed value and no dispose function.
Definition: AutoDispose.h:216
AutoDispose(const Dispose &dispose_r)
Ctor taking dispose function and using default constructed value.
Definition: AutoDispose.h:221
void setDispose(const Dispose &dispose_r)
Set a new dispose function.
Definition: AutoDispose.h:241
shared_ptr< Impl > _pimpl
Definition: AutoDispose.h:269
const Dispose & getDispose() const
Return the current dispose function.
Definition: AutoDispose.h:237
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:245
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:94
AutoDispose(const value_type &value_r)
Ctor taking value and no dispose function.
Definition: AutoDispose.h:121
AutoDispose(value_type &&value_r)
Ctor taking rvalue and no dispose function.
Definition: AutoDispose.h:131
const Dispose & getDispose() const
Return the current dispose function.
Definition: AutoDispose.h:168
AutoDispose(const value_type &value_r, const Dispose &dispose_r)
Ctor taking value and dispose function.
Definition: AutoDispose.h:126
reference value() const
Reference to the Tp object.
Definition: AutoDispose.h:147
std::ostream & operator<<(std::ostream &str, const AutoDispose< Tp > &obj)
Stream output of the Tp object.
Definition: AutoDispose.h:287
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
AutoDispose(const Dispose &dispose_r)
Ctor taking dispose function and using default constructed value.
Definition: AutoDispose.h:116
value_type * operator->() const
Pointer to the Tp object (asserted to be != NULL).
Definition: AutoDispose.h:155
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
AutoDispose(value_type &&value_r, const Dispose &dispose_r)
Ctor taking rvalue and dispose function.
Definition: AutoDispose.h:136
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
Impl(T &&value_r, D &&dispose_r)
Definition: AutoDispose.h:191
Impl(const Dispose &dispose_r)
Definition: AutoDispose.h:258
AutoDispose<int> calling ::close
Definition: AutoDispose.h:297
AutoFD(int fd_r=-1)
Definition: AutoDispose.h:298
AutoDispose<FILE*> calling ::fclose
Definition: AutoDispose.h:308
AutoFILE(FILE *file_r=nullptr)
Definition: AutoDispose.h:309
AutoFREE(void *ptr_r=nullptr)
Definition: AutoDispose.h:327
AutoFREE(void *ptr_r)
Definition: AutoDispose.h:321
AutoFREE(Tp *ptr_r=nullptr)
Definition: AutoDispose.h:320