libzypp  10.5.0
ProgressData.h
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_PROGRESSDATA_H
00013 #define ZYPP_PROGRESSDATA_H
00014 
00015 #include <iosfwd>
00016 #include <string>
00017 
00018 #include "zypp/base/PtrTypes.h"
00019 #include "zypp/base/Function.h"
00020 #include "zypp/base/ProvideNumericId.h"
00021 
00022 #include "zypp/Date.h"
00023 
00025 namespace zypp
00026 { 
00027 
00029   //
00030   //    CLASS NAME : ProgressData
00031   //
00135   class ProgressData : public base::ProvideNumericId<ProgressData,unsigned>
00136   {
00137     public:
00138       typedef long long value_type;
00144       typedef function<bool( const ProgressData & )> ReceiverFnc;
00145 
00146     private:
00147       enum State { INIT, RUN, END };
00148 
00149       class Data
00150       {
00151         public:
00152           Data( value_type min_r, value_type max_r, value_type val_r )
00153           : _state( INIT ), _min( min_r ), _max( max_r ), _val( val_r )
00154           , _last_val( 0 ), _last_send( 0 )
00155           {}
00156 
00157         public:
00158           State       _state;
00159           std::string _name;
00160           value_type  _min;
00161           value_type  _max;
00162           value_type  _val;
00163 
00164           ReceiverFnc _receiver;
00165           value_type  _last_val;
00166           Date        _last_send;
00167 
00168         private:
00170           friend Data * rwcowClone<Data>( const Data * rhs );
00171           Data * clone() const { return new Data( *this ); }
00172       };
00173 
00174     public:
00176       ProgressData()
00177       : _d( new Data( 0, 0, 0 ) )
00178       {}
00179 
00181       ProgressData( value_type max_r )
00182       : _d( new Data( 0, max_r, 0 ) )
00183       {}
00184 
00186       ProgressData( value_type min_r, value_type max_r )
00187       : _d( new Data( min_r, max_r, min_r ) )
00188       {}
00189 
00191       ProgressData( value_type min_r, value_type max_r, value_type val_r )
00192       : _d( new Data( min_r, max_r, val_r ) )
00193       {}
00194 
00195       ~ProgressData()
00196       {
00197         if ( _d->_state == RUN )
00198         {
00199           _d->_state = END;
00200           report();
00201         }
00202       }
00203 
00204     public:
00206       void min( value_type min_r )
00207       { _d->_min = min_r; }
00208 
00210       void max( value_type max_r )
00211       { _d->_max = max_r; }
00212 
00214       void noRange()
00215       { range( 0, 0 ); }
00216 
00218       void range( value_type max_r )
00219       { range( 0, max_r ); }
00220 
00222       void range( value_type min_r, value_type max_r )
00223       { min( min_r ); max( max_r ); }
00224 
00225     public:
00227       void name( const std::string & name_r )
00228       { _d->_name = name_r; }
00229 
00231       void sendTo( const ReceiverFnc & fnc_r )
00232       { _d->_receiver = fnc_r; }
00233 
00235       void noSend()
00236       { _d->_receiver = ReceiverFnc(); }
00237 
00238     public:
00249 
00251       bool set( value_type val_r )
00252       {
00253         _d->_val = val_r;
00254         return report();
00255       }
00256 
00258       bool incr( value_type val_r = 1 )
00259       { return set( val() + val_r ); }
00260 
00262       bool decr( value_type val_r = 1 )
00263       { return set( val() - val_r ); }
00264 
00266       bool toMin()
00267       { return set( min() ); }
00268 
00270       bool toMax()
00271       { return set( hasRange() ? max() : val() ); }
00272 
00274       bool tick()
00275       { return set( val() ); }
00276 
00278 
00279     public:
00284       value_type min() const
00285       { return _d->_min; }
00286 
00288       value_type max() const
00289       { return _d->_max; }
00290 
00292       value_type val() const
00293       { return _d->_val; }
00294 
00296       bool hasRange() const
00297       { return min() != max(); }
00298 
00303       bool reportPercent() const
00304       { return hasRange(); }
00305 
00310       bool reportAlive() const
00311       { return ! hasRange(); }
00312 
00316       value_type reportValue() const
00317       { return hasRange() ? val() * 100 / ( max() - min() ) : -1; }
00318 
00320       const std::string & name() const
00321       { return _d->_name; }
00322 
00324       const ReceiverFnc & receiver() const
00325       { return _d->_receiver; }
00326 
00330       bool finalReport() const
00331       { return( _d->_state == END ); }
00332 
00334 
00335     private:
00337       bool report();
00338 
00340       RWCOW_pointer<Data> _d;
00341   };
00343 
00345   std::ostream & operator<<( std::ostream & str, const ProgressData & obj );
00346 
00348 
00349   class InputStream;
00351   ProgressData makeProgressData( const InputStream & input_r );
00352 
00354 
00387   class CombinedProgressData
00388   {
00389   public:
00404     CombinedProgressData( ProgressData &pd,
00405                           ProgressData::value_type weight = 0 );
00406 
00411     bool operator()( const ProgressData &progress );
00412 
00413   private:
00414     ProgressData::value_type _weight;
00415     ProgressData::value_type _last_value;
00416     ProgressData &_pd;
00417   };
00418 
00420 } // namespace zypp
00422 #endif // ZYPP_PROGRESSDATA_H