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;
00044     static const Unit K;
00046     static const Unit M;
00048     static const Unit G;
00050     static const Unit T;
00052     static const Unit kB;
00054     static const Unit MB;
00056     static const Unit GB;
00058     static const Unit TB;
00060 
00061   public:
00062 
00064     ByteCount()
00065     : _count( 0 )
00066     {}
00068     ByteCount( const Unit & unit_r )
00069     : _count( unit_r.factor() )
00070     {}
00072     ByteCount( const SizeType count_r, const Unit & unit_r = B )
00073     : _count( count_r * unit_r.factor() )
00074     {}
00075 
00076   public:
00077 
00079     operator SizeType() const
00080     { return _count; }
00081 
00086     ByteCount & operator+=( const SizeType rhs ) { _count += rhs; return *this; }
00087     ByteCount & operator-=( const SizeType rhs ) { _count -= rhs; return *this; }
00088     ByteCount & operator*=( const SizeType rhs ) { _count *= rhs; return *this; }
00089     ByteCount & operator/=( const SizeType rhs ) { _count /= rhs; return *this; }
00090 
00091     ByteCount & operator++(/*prefix*/) { _count += 1; return *this; }
00092     ByteCount & operator--(/*prefix*/) { _count -= 1; return *this; }
00093 
00094     ByteCount operator++(int/*postfix*/) { return _count++; }
00095     ByteCount operator--(int/*postfix*/) { return _count--; }
00097 
00101     ByteCount & fillBlock( ByteCount blocksize_r = K );
00102 
00104     ByteCount fullBlocks( ByteCount blocksize_r = K ) const
00105     { return ByteCount(*this).fillBlock( blocksize_r ); }
00106 
00108     SizeType blocks( ByteCount blocksize_r = K ) const
00109     { return fullBlocks( blocksize_r ) / blocksize_r; }
00110 
00111   public:
00112 
00114     const Unit & bestUnit() const;
00115 
00117     const Unit & bestUnit1000() const;
00118 
00127     std::string asString( unsigned field_width_r = 0,
00128                           unsigned unit_width_r  = 1 ) const
00129     { return asString( bestUnit(), field_width_r, unit_width_r ); }
00131     std::string asString( unsigned field_width_r,
00132                           unsigned unit_width_r,
00133                           unsigned prec_r ) const
00134     { return asString( bestUnit(), field_width_r, unit_width_r, prec_r ); }
00136     std::string asString( const Unit & unit_r,
00137                           unsigned field_width_r = 0,
00138                           unsigned unit_width_r  = 1 ) const
00139     { return asString( unit_r, field_width_r, unit_width_r, unit_r.prec() ); }
00141     std::string asString( const Unit & unit_r,
00142                           unsigned field_width_r,
00143                           unsigned unit_width_r,
00144                           unsigned prec_r ) const
00145     { return unit_r.form( _count, field_width_r, unit_width_r, prec_r ); }
00147 
00148   private:
00149     SizeType _count;
00150   };
00152 
00154   inline std::ostream & operator<<( std::ostream & str, const ByteCount & obj )
00155   { return str << obj.asString(); }
00156 
00158 } // namespace zypp
00160 #endif // ZYPP_BYTECOUNT_H

Generated on Thu Jul 6 00:07:20 2006 for zypp by  doxygen 1.4.6