libzypp  17.31.0
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 
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 
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  {}
200  {
201  if ( _dispose )
202  try { _dispose( _value ); } catch(...) {}
203  }
206  };
207 
208  shared_ptr<Impl> _pimpl;
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 
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 
267  {
268  if ( _dispose )
269  try { _dispose(); } catch(...) {}
270  }
272  };
273  shared_ptr<Impl> _pimpl;
274  };
275 
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
AutoFREE(Tp *ptr_r=nullptr)
Definition: AutoDispose.h:324
Impl(const Dispose &dispose_r)
Definition: AutoDispose.h:262
void reset()
Reset to default Ctor values.
Definition: AutoDispose.h:159
bool unique() const
Returns true if this is the only AutoDispose instance managing the current data object.
Definition: AutoDispose.h:167
AutoDispose(const value_type &value_r, const Dispose &dispose_r)
Ctor taking value and dispose function.
Definition: AutoDispose.h:126
void swapDispose(Dispose &dispose_r)
Exchange the dispose function.
Definition: AutoDispose.h:184
shared_ptr< Impl > _pimpl
Definition: AutoDispose.h:273
AutoDispose(const value_type &value_r)
Ctor taking value and no dispose function.
Definition: AutoDispose.h:121
void swap(AutoDispose &rhs)
Exchange the contents of two AutoDispose objects.
Definition: AutoDispose.h:236
String related utilities and Regular expression matching.
Definition: Arch.h:351
void reset()
Reset to default Ctor values.
Definition: AutoDispose.h:232
AutoDispose<int> calling ::close
Definition: AutoDispose.h:300
void swap(AutoDispose &rhs)
Exchange the contents of two AutoDispose objects.
Definition: AutoDispose.h:163
shared_ptr< Impl > _pimpl
Definition: AutoDispose.h:208
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:249
function< void(dispose_param_type)> Dispose
Dispose function signatue.
Definition: AutoDispose.h:107
void setDispose(const Dispose &dispose_r)
Set a new dispose function.
Definition: AutoDispose.h:245
AutoDispose(value_type &&value_r)
Ctor taking rvalue and no dispose function.
Definition: AutoDispose.h:131
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
AutoDispose()
Default Ctor using default constructed value and no dispose function.
Definition: AutoDispose.h:220
AutoFREE(void *ptr_r)
Definition: AutoDispose.h:325
const Dispose & getDispose() const
Return the current dispose function.
Definition: AutoDispose.h:172
Impl(T &&value_r, D &&dispose_r)
Definition: AutoDispose.h:195
reference operator*() const
Reference to the Tp object.
Definition: AutoDispose.h:151
AutoDispose(value_type &&value_r, const Dispose &dispose_r)
Ctor taking rvalue and dispose function.
Definition: AutoDispose.h:136
boost::call_traits< Tp >::const_reference const_reference
Definition: AutoDispose.h:98
AutoDispose()
Default Ctor using default constructed value and no dispose function.
Definition: AutoDispose.h:111
boost::call_traits< Tp >::param_type param_type
Definition: AutoDispose.h:96
boost::call_traits< Tp >::value_type result_type
Definition: AutoDispose.h:100
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:180
AutoDispose(const Dispose &dispose_r)
Ctor taking dispose function and using default constructed value.
Definition: AutoDispose.h:225
void setDispose(const Dispose &dispose_r)
Set a new dispose function.
Definition: AutoDispose.h:176
typename std::conditional< std::is_pointer< FILE * >::value||std::is_integral< FILE * >::value, FILE * const, reference >::type dispose_param_type
Definition: AutoDispose.h:103
AutoFILE(FILE *file_r=nullptr)
Definition: AutoDispose.h:313
AutoDispose(const Dispose &dispose_r)
Ctor taking dispose function and using default constructed value.
Definition: AutoDispose.h:116
AutoFREE(void *ptr_r=nullptr)
Definition: AutoDispose.h:331
reference value() const
Reference to the Tp object.
Definition: AutoDispose.h:147
boost::call_traits< Tp >::reference reference
Definition: AutoDispose.h:97
AutoFD(int fd_r=-1)
Definition: AutoDispose.h:302
void swapDispose(Dispose &dispose_r)
Exchange the dispose function.
Definition: AutoDispose.h:253
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:93
AutoDispose<FILE*> calling ::fclose
Definition: AutoDispose.h:311
value_type * operator->() const
Pointer to the Tp object (asserted to be != NULL).
Definition: AutoDispose.h:155
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
function< void()> Dispose
Dispose function signatue.
Definition: AutoDispose.h:216
const Dispose & getDispose() const
Return the current dispose function.
Definition: AutoDispose.h:241