AutoDispose.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_AUTODISPOSE_H
00013 #define ZYPP_AUTODISPOSE_H
00014 
00015 #include <iosfwd>
00016 #include <boost/call_traits.hpp>
00017 
00018 #include "zypp/base/NonCopyable.h"
00019 #include "zypp/base/PtrTypes.h"
00020 #include "zypp/base/Function.h"
00021 
00023 namespace zypp
00024 { 
00025 
00027   //
00028   //    CLASS NAME : AutoDispose<_Tp>
00029   //
00091   template<class _Tp>
00092     class AutoDispose
00093     {
00094     public:
00095       typedef typename boost::call_traits<_Tp>::param_type       param_type;
00096       typedef typename boost::call_traits<_Tp>::reference        reference;
00097       typedef typename boost::call_traits<_Tp>::const_reference  const_reference;
00098       typedef _Tp                                                value_type;
00099       typedef typename boost::call_traits<_Tp>::value_type       result_type;
00100 
00101     public:
00103       typedef function<void ( param_type )> Dispose;
00104 
00105     public:
00107       AutoDispose()
00108       : _pimpl( new Impl( value_type() ) )
00109       {}
00110 
00112       AutoDispose( param_type value_r )
00113       : _pimpl( new Impl( value_r ) )
00114       {}
00115 
00117       AutoDispose( param_type value_r, const Dispose & dispose_r )
00118       : _pimpl( new Impl( value_r, dispose_r ) )
00119       {}
00120 
00121     public:
00122 
00124       operator reference() const
00125       { return _pimpl->_value; }
00126 
00128       reference value() const
00129       { return _pimpl->_value; }
00130 
00132       reference operator*() const
00133       { return _pimpl->_value; }
00134 
00136       value_type * operator->() const
00137       { return & _pimpl->_value; }
00138 
00140       void reset()
00141       { AutoDispose().swap( *this ); }
00142 
00144       void swap( AutoDispose & rhs )
00145       { _pimpl.swap( rhs._pimpl ); }
00146 
00147     public:
00149       const Dispose & getDispose() const
00150       { return _pimpl->_dispose; }
00151 
00153       void setDispose( const Dispose & dispose_r )
00154       { _pimpl->_dispose = dispose_r; }
00155 
00157       void resetDispose()
00158       { setDispose( Dispose() ); }
00159 
00161       void swapDispose( Dispose & dispose_r )
00162       { _pimpl->_dispose.swap( dispose_r ); }
00163 
00164     private:
00165       struct Impl : private base::NonCopyable
00166       {
00167         Impl( param_type value_r )
00168         : _value( value_r )
00169         {}
00170         Impl( param_type value_r, const Dispose & dispose_r )
00171         : _value( value_r )
00172         , _dispose( dispose_r )
00173         {}
00174         ~Impl()
00175         {
00176           if ( _dispose )
00177             try { _dispose( _value ); } catch(...) {}
00178         }
00179         value_type _value;
00180         Dispose    _dispose;
00181       };
00182 
00183       shared_ptr<Impl> _pimpl;
00184     };
00186 
00188   template<class _Tp>
00189     inline std::ostream & operator<<( std::ostream & str, const AutoDispose<_Tp> & obj )
00190     { return str << obj.value(); }
00191 
00193 } // namespace zypp
00195 #endif // ZYPP_AUTODISPOSE_H

Generated on Tue Sep 25 19:22:58 2007 for libzypp by  doxygen 1.5.3