libzypp  10.5.0
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       explicit AutoDispose( const Dispose & dispose_r )
00113       : _pimpl( new Impl( value_type(), dispose_r ) )
00114       {}
00115 
00117       explicit AutoDispose( param_type value_r )
00118       : _pimpl( new Impl( value_r ) )
00119       {}
00120 
00122       AutoDispose( param_type value_r, const Dispose & dispose_r )
00123       : _pimpl( new Impl( value_r, dispose_r ) )
00124       {}
00125 
00126     public:
00127 
00129       operator reference() const
00130       { return _pimpl->_value; }
00131 
00133       reference value() const
00134       { return _pimpl->_value; }
00135 
00137       reference operator*() const
00138       { return _pimpl->_value; }
00139 
00141       value_type * operator->() const
00142       { return & _pimpl->_value; }
00143 
00145       void reset()
00146       { AutoDispose().swap( *this ); }
00147 
00149       void swap( AutoDispose & rhs )
00150       { _pimpl.swap( rhs._pimpl ); }
00151 
00152     public:
00154       const Dispose & getDispose() const
00155       { return _pimpl->_dispose; }
00156 
00158       void setDispose( const Dispose & dispose_r )
00159       { _pimpl->_dispose = dispose_r; }
00160 
00162       void resetDispose()
00163       { setDispose( Dispose() ); }
00164 
00166       void swapDispose( Dispose & dispose_r )
00167       { _pimpl->_dispose.swap( dispose_r ); }
00168 
00169     private:
00170       struct Impl : private base::NonCopyable
00171       {
00172         Impl( param_type value_r )
00173         : _value( value_r )
00174         {}
00175         Impl( param_type value_r, const Dispose & dispose_r )
00176         : _value( value_r )
00177         , _dispose( dispose_r )
00178         {}
00179         ~Impl()
00180         {
00181           if ( _dispose )
00182             try { _dispose( _value ); } catch(...) {}
00183         }
00184         value_type _value;
00185         Dispose    _dispose;
00186       };
00187 
00188       shared_ptr<Impl> _pimpl;
00189     };
00191 
00193   template<class _Tp>
00194     inline std::ostream & operator<<( std::ostream & str, const AutoDispose<_Tp> & obj )
00195     { return str << obj.value(); }
00196 
00198 } // namespace zypp
00200 #endif // ZYPP_AUTODISPOSE_H