libzypp  17.25.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/base/NonCopyable.h>
19 #include <zypp/base/PtrTypes.h>
20 #include <zypp/base/Function.h>
21 
23 namespace zypp
24 {
25 
27  //
28  // CLASS NAME : AutoDispose<Tp>
29  //
91  template<class Tp>
93  {
94  public:
95  typedef typename boost::call_traits<Tp>::param_type param_type;
96  typedef typename boost::call_traits<Tp>::reference reference;
97  typedef typename boost::call_traits<Tp>::const_reference const_reference;
98  typedef Tp value_type;
99  typedef typename boost::call_traits<Tp>::value_type result_type;
100 
101  public:
103  typedef function<void ( param_type )> Dispose;
104 
105  public:
108  : _pimpl( new Impl( value_type() ) )
109  {}
110 
112  explicit AutoDispose( const Dispose & dispose_r )
113  : _pimpl( new Impl( value_type(), dispose_r ) )
114  {}
115 
117  explicit AutoDispose( param_type value_r )
118  : _pimpl( new Impl( value_r ) )
119  {}
120 
122  AutoDispose( param_type value_r, const Dispose & dispose_r )
123  : _pimpl( new Impl( value_r, dispose_r ) )
124  {}
125 
126  public:
127 
129  operator reference() const
130  { return _pimpl->_value; }
131 
133  reference value() const
134  { return _pimpl->_value; }
135 
138  { return _pimpl->_value; }
139 
142  { return & _pimpl->_value; }
143 
145  void reset()
146  { AutoDispose().swap( *this ); }
147 
149  void swap( AutoDispose & rhs )
150  { _pimpl.swap( rhs._pimpl ); }
151 
152  public:
154  const Dispose & getDispose() const
155  { return _pimpl->_dispose; }
156 
158  void setDispose( const Dispose & dispose_r )
159  { _pimpl->_dispose = dispose_r; }
160 
163  { setDispose( Dispose() ); }
164 
166  void swapDispose( Dispose & dispose_r )
167  { _pimpl->_dispose.swap( dispose_r ); }
168 
169  private:
170  struct Impl : private base::NonCopyable
171  {
172  Impl( param_type value_r )
173  : _value( value_r )
174  {}
175  Impl( param_type value_r, const Dispose & dispose_r )
176  : _value( value_r )
177  , _dispose( dispose_r )
178  {}
180  {
181  if ( _dispose )
182  try { _dispose( _value ); } catch(...) {}
183  }
186  };
187 
188  shared_ptr<Impl> _pimpl;
189  };
190 
191  template<>
192  class AutoDispose<void>
193  {
194  public:
196  typedef function<void ()> Dispose;
197 
198  public:
201  : _pimpl( new Impl() )
202  {}
203 
205  explicit AutoDispose( const Dispose & dispose_r )
206  : _pimpl( new Impl( dispose_r ) )
207  {}
208 
209  public:
210 
212  void reset()
213  { AutoDispose().swap( *this ); }
214 
216  void swap( AutoDispose & rhs )
217  { _pimpl.swap( rhs._pimpl ); }
218 
219  public:
221  const Dispose & getDispose() const
222  { return _pimpl->_dispose; }
223 
225  void setDispose( const Dispose & dispose_r )
226  { _pimpl->_dispose = dispose_r; }
227 
230  { setDispose( Dispose() ); }
231 
233  void swapDispose( Dispose & dispose_r )
234  { _pimpl->_dispose.swap( dispose_r ); }
235 
236  private:
237  struct Impl : private base::NonCopyable
238  {
239  Impl( )
240  {}
241 
242  Impl( const Dispose & dispose_r )
243  : _dispose( dispose_r )
244  {}
245 
247  {
248  if ( _dispose )
249  try { _dispose(); } catch(...) {}
250  }
252  };
253  shared_ptr<Impl> _pimpl;
254  };
255 
266 
268 
270  template<class Tp>
271  inline std::ostream & operator<<( std::ostream & str, const AutoDispose<Tp> & obj )
272  { return str << obj.value(); }
273 
274 
280  struct AutoFD : public AutoDispose<int>
281  {
282  AutoFD( int fd_r = -1 ) : AutoDispose<int>( fd_r, [] ( int fd_r ) { if ( fd_r != -1 ) ::close( fd_r ); } ) {}
283  };
284 
291  struct AutoFILE : public AutoDispose<FILE*>
292  {
293  AutoFILE( FILE* file_r = nullptr ) : AutoDispose<FILE*>( file_r, [] ( FILE* file_r ) { if ( file_r ) ::fclose( file_r ); } ) {}
294  };
295 
301  template <typename Tp>
302  struct AutoFREE : public AutoDispose<Tp*>
303  {
304  AutoFREE( Tp* ptr_r = nullptr ) : AutoDispose<Tp*>( ptr_r, [] ( Tp* ptr_r ) { if ( ptr_r ) ::free( ptr_r ); } ) {}
305  AutoFREE( void* ptr_r ) : AutoFREE( static_cast<Tp*>(ptr_r) ) {}
306  };
307 
308  template <>
309  struct AutoFREE<void> : public AutoDispose<void*>
310  {
311  AutoFREE( void* ptr_r = nullptr ) : AutoDispose<void*>( ptr_r, [] ( void* ptr_r ) { if ( ptr_r ) ::free( ptr_r ); } ) {}
312  };
314 } // namespace zypp
316 #endif // ZYPP_AUTODISPOSE_H
zypp::AutoDispose< void >
Definition: AutoDispose.h:192
zypp::AutoDispose::swap
void swap(AutoDispose &rhs)
Exchange the contents of two AutoDispose objects.
Definition: AutoDispose.h:149
zypp::AutoDispose< void >::AutoDispose
AutoDispose(const Dispose &dispose_r)
Ctor taking dispose function and using default constructed value.
Definition: AutoDispose.h:205
zypp::AutoDispose::Impl
Definition: AutoDispose.h:170
zypp::AutoDispose::Impl::~Impl
~Impl()
Definition: AutoDispose.h:179
zypp::AutoFREE::AutoFREE
AutoFREE(void *ptr_r)
Definition: AutoDispose.h:305
zypp::AutoDispose::operator->
value_type * operator->() const
Pointer to the Tp object (asserted to be != NULL).
Definition: AutoDispose.h:141
zypp::AutoDispose< void >::Impl::Impl
Impl()
Definition: AutoDispose.h:239
PtrTypes.h
zypp::AutoDispose::Impl::Impl
Impl(param_type value_r)
Definition: AutoDispose.h:172
zypp::AutoDispose::param_type
boost::call_traits< Tp >::param_type param_type
Definition: AutoDispose.h:95
zypp::AutoDispose< void >::swapDispose
void swapDispose(Dispose &dispose_r)
Exchange the dispose function.
Definition: AutoDispose.h:233
zypp::AutoDispose::_pimpl
shared_ptr< Impl > _pimpl
Definition: AutoDispose.h:188
zypp::AutoDispose< void >::setDispose
void setDispose(const Dispose &dispose_r)
Set a new dispose function.
Definition: AutoDispose.h:225
zypp::AutoDispose< void >::getDispose
const Dispose & getDispose() const
Return the current dispose function.
Definition: AutoDispose.h:221
zypp::AutoDispose< void >::AutoDispose
AutoDispose()
Default Ctor using default constructed value and no dispose function.
Definition: AutoDispose.h:200
zypp::AutoFD
AutoDispose<int> calling ::close
Definition: AutoDispose.h:280
zypp::AutoDispose::AutoDispose
AutoDispose(param_type value_r, const Dispose &dispose_r)
Ctor taking value and dispose function.
Definition: AutoDispose.h:122
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
zypp::AutoDispose::getDispose
const Dispose & getDispose() const
Return the current dispose function.
Definition: AutoDispose.h:154
zypp::AutoDispose< void >::swap
void swap(AutoDispose &rhs)
Exchange the contents of two AutoDispose objects.
Definition: AutoDispose.h:216
zypp::AutoDispose::Impl::Impl
Impl(param_type value_r, const Dispose &dispose_r)
Definition: AutoDispose.h:175
zypp::AutoFILE::AutoFILE
AutoFILE(FILE *file_r=nullptr)
Definition: AutoDispose.h:293
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::AutoDispose< void >::Impl::Impl
Impl(const Dispose &dispose_r)
Definition: AutoDispose.h:242
zypp::AutoDispose< void >::resetDispose
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:229
zypp::AutoDispose::Impl::_value
value_type _value
Definition: AutoDispose.h:184
zypp::AutoDispose::reference
boost::call_traits< Tp >::reference reference
Definition: AutoDispose.h:96
zypp::AutoDispose< void >::reset
void reset()
Reset to default Ctor values.
Definition: AutoDispose.h:212
zypp::AutoDispose::reset
void reset()
Reset to default Ctor values.
Definition: AutoDispose.h:145
zypp::AutoFREE
Definition: AutoDispose.h:302
zypp::AutoDispose::value_type
Tp value_type
Definition: AutoDispose.h:98
zypp::AutoDispose::setDispose
void setDispose(const Dispose &dispose_r)
Set a new dispose function.
Definition: AutoDispose.h:158
zypp::AutoFILE
AutoDispose<FILE*> calling ::fclose
Definition: AutoDispose.h:291
Impl
zypp::AutoDispose::Dispose
function< void(param_type)> Dispose
Dispose function signatue.
Definition: AutoDispose.h:103
zypp::AutoFREE< void >::AutoFREE
AutoFREE(void *ptr_r=nullptr)
Definition: AutoDispose.h:311
zypp::AutoDispose
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:92
zypp::AutoDispose< void >::Impl::_dispose
Dispose _dispose
Definition: AutoDispose.h:251
zypp::AutoDispose< void >::_pimpl
shared_ptr< Impl > _pimpl
Definition: AutoDispose.h:253
zypp::AutoDispose::value
reference value() const
Reference to the Tp object.
Definition: AutoDispose.h:133
Function.h
zypp::AutoDispose::Impl::_dispose
Dispose _dispose
Definition: AutoDispose.h:185
zypp::AutoDispose::AutoDispose
AutoDispose()
Default Ctor using default constructed value and no dispose function.
Definition: AutoDispose.h:107
str
String related utilities and Regular expression matching.
zypp::AutoDispose::resetDispose
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:162
zypp::AutoDispose::operator<<
std::ostream & operator<<(std::ostream &str, const AutoDispose< Tp > &obj)
Definition: AutoDispose.h:271
zypp::AutoDispose< void >::Impl::~Impl
~Impl()
Definition: AutoDispose.h:246
zypp::AutoDispose::operator*
reference operator*() const
Reference to the Tp object.
Definition: AutoDispose.h:137
zypp::AutoDispose::swapDispose
void swapDispose(Dispose &dispose_r)
Exchange the dispose function.
Definition: AutoDispose.h:166
zypp::AutoDispose::result_type
boost::call_traits< Tp >::value_type result_type
Definition: AutoDispose.h:99
zypp::AutoDispose::const_reference
boost::call_traits< Tp >::const_reference const_reference
Definition: AutoDispose.h:97
zypp::AutoDispose::AutoDispose
AutoDispose(param_type value_r)
Ctor taking value and no dispose function.
Definition: AutoDispose.h:117
zypp::AutoDispose< void >::Dispose
function< void()> Dispose
Dispose function signatue.
Definition: AutoDispose.h:196
zypp::AutoFD::AutoFD
AutoFD(int fd_r=-1)
Definition: AutoDispose.h:282
zypp::AutoDispose::AutoDispose
AutoDispose(const Dispose &dispose_r)
Ctor taking dispose function and using default constructed value.
Definition: AutoDispose.h:112
NonCopyable.h
zypp::AutoFREE::AutoFREE
AutoFREE(Tp *ptr_r=nullptr)
Definition: AutoDispose.h:304