libzypp  10.5.0
ByteCount.h
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_BYTECOUNT_H
00013 #define ZYPP_BYTECOUNT_H
00014 
00015 #include <iosfwd>
00016 
00017 #include "zypp/base/Unit.h"
00018 
00020 namespace zypp
00021 { 
00022 
00024   //
00025   //    CLASS NAME : ByteCount
00026   //
00030   class ByteCount
00031   {
00032     friend std::ostream & operator<<( std::ostream & str, const ByteCount & obj );
00033 
00034   public:
00035 
00036     typedef base::Unit      Unit;
00037     typedef Unit::ValueType SizeType;
00038 
00042     static const Unit B;
00043 
00045     static const Unit K;
00046     static const Unit KiB;
00048     static const Unit M;
00049     static const Unit MiB;
00051     static const Unit G;
00052     static const Unit GiB;
00054     static const Unit T;
00055     static const Unit TiB;
00056 
00058     static const Unit kB;
00060     static const Unit MB;
00062     static const Unit GB;
00064     static const Unit TB;
00066 
00067   public:
00068 
00070     ByteCount()
00071     : _count( 0 )
00072     {}
00074     ByteCount( const Unit & unit_r )
00075     : _count( unit_r.factor() )
00076     {}
00078     ByteCount( const SizeType count_r, const Unit & unit_r = B )
00079     : _count( count_r * unit_r.factor() )
00080     {}
00081 
00082  public:
00083 
00085     operator SizeType() const
00086     { return _count; }
00087 
00092     ByteCount & operator+=( const SizeType rhs ) { _count += rhs; return *this; }
00093     ByteCount & operator-=( const SizeType rhs ) { _count -= rhs; return *this; }
00094     ByteCount & operator*=( const SizeType rhs ) { _count *= rhs; return *this; }
00095     ByteCount & operator/=( const SizeType rhs ) { _count /= rhs; return *this; }
00096 
00097     ByteCount & operator++(/*prefix*/) { _count += 1; return *this; }
00098     ByteCount & operator--(/*prefix*/) { _count -= 1; return *this; }
00099 
00100     ByteCount operator++(int/*postfix*/) { return _count++; }
00101     ByteCount operator--(int/*postfix*/) { return _count--; }
00103 
00107     ByteCount & fillBlock( ByteCount blocksize_r = K );
00108 
00110     ByteCount fullBlocks( ByteCount blocksize_r = K ) const
00111     { return ByteCount(*this).fillBlock( blocksize_r ); }
00112 
00114     SizeType blocks( ByteCount blocksize_r = K ) const
00115     { return fullBlocks( blocksize_r ) / blocksize_r; }
00116 
00117   public:
00118 
00120     const Unit & bestUnit() const;
00121 
00123     const Unit & bestUnit1000() const;
00124 
00133     std::string asString( unsigned field_width_r = 0,
00134                           unsigned unit_width_r  = 1 ) const
00135     { return asString( bestUnit(), field_width_r, unit_width_r ); }
00137     std::string asString( unsigned field_width_r,
00138                           unsigned unit_width_r,
00139                           unsigned prec_r ) const
00140     { return asString( bestUnit(), field_width_r, unit_width_r, prec_r ); }
00142     std::string asString( const Unit & unit_r,
00143                           unsigned field_width_r = 0,
00144                           unsigned unit_width_r  = 1 ) const
00145     { return asString( unit_r, field_width_r, unit_width_r, unit_r.prec() ); }
00147     std::string asString( const Unit & unit_r,
00148                           unsigned field_width_r,
00149                           unsigned unit_width_r,
00150                           unsigned prec_r ) const
00151     { return unit_r.form( _count, field_width_r, unit_width_r, prec_r ); }
00153 
00154   private:
00155     SizeType _count;
00156   };
00158 
00160   inline std::ostream & operator<<( std::ostream & str, const ByteCount & obj )
00161   { return str << obj.asString(); }
00162 
00164 } // namespace zypp
00166 #endif // ZYPP_BYTECOUNT_H