1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ByteCount.h
12 #ifndef ZYPP_BYTECOUNT_H
13 #define ZYPP_BYTECOUNT_H
17 #include "zypp/base/Unit.h"
19 ///////////////////////////////////////////////////////////////////
21 { /////////////////////////////////////////////////////////////////
23 ///////////////////////////////////////////////////////////////////
25 // CLASS NAME : ByteCount
27 /** Store and operate with byte count.
32 friend std::ostream & operator<<( std::ostream & str, const ByteCount & obj );
36 typedef base::Unit Unit;
37 typedef Unit::ValueType SizeType;
39 /** \name Byte unit constants. */
46 static const Unit KiB;
49 static const Unit MiB;
52 static const Unit GiB;
55 static const Unit TiB;
73 /** Ctor taking 1 Unit. */
74 ByteCount( const Unit & unit_r )
75 : _count( unit_r.factor() )
77 /** Ctor taking a count and optinal Unit. */
78 ByteCount( const SizeType count_r, const Unit & unit_r = B )
79 : _count( count_r * unit_r.factor() )
84 /** Conversion to SizeType. */
85 operator SizeType() const
88 /** \name Arithmetic operations.
89 * \c + \c - \c * \c / are provided via conversion to SizeType.
92 ByteCount & operator+=( const SizeType rhs ) { _count += rhs; return *this; }
93 ByteCount & operator-=( const SizeType rhs ) { _count -= rhs; return *this; }
94 ByteCount & operator*=( const SizeType rhs ) { _count *= rhs; return *this; }
95 ByteCount & operator/=( const SizeType rhs ) { _count /= rhs; return *this; }
97 ByteCount & operator++(/*prefix*/) { _count += 1; return *this; }
98 ByteCount & operator--(/*prefix*/) { _count -= 1; return *this; }
100 ByteCount operator++(int/*postfix*/) { return _count++; }
101 ByteCount operator--(int/*postfix*/) { return _count--; }
104 /** Adjust count to multiple of \a blocksize_r (default 1K).
105 * Zero blocksize_r is treated as 1B.
107 ByteCount & fillBlock( ByteCount blocksize_r = K );
109 /** Return count adjusted to multiple of \a blocksize_r (default 1K). */
110 ByteCount fullBlocks( ByteCount blocksize_r = K ) const
111 { return ByteCount(*this).fillBlock( blocksize_r ); }
113 /** Return number of blocks of size \a blocksize_r (default 1K). */
114 SizeType blocks( ByteCount blocksize_r = K ) const
115 { return fullBlocks( blocksize_r ) / blocksize_r; }
119 /** Return the best Unit (B,K,M,G,T) for count. */
120 const Unit & bestUnit() const;
122 /** Return the best Unit (B,kB,MB,GB,TB) for count. */
123 const Unit & bestUnit1000() const;
125 /** \name Conversion to string.
126 * \li \a field_width_r Width for the number part (incl. precision)
127 * \li \a unit_width_r With for the unit symbol (without symbol if zero)
128 * \li \a prec_r Precision to use.
129 * \see zypp::base::Unit
132 /** Auto selected Unit and precision. */
133 std::string asString( unsigned field_width_r = 0,
134 unsigned unit_width_r = 1 ) const
135 { return asString( bestUnit(), field_width_r, unit_width_r ); }
136 /** Auto selected Unit. */
137 std::string asString( unsigned field_width_r,
138 unsigned unit_width_r,
139 unsigned prec_r ) const
140 { return asString( bestUnit(), field_width_r, unit_width_r, prec_r ); }
141 /** Auto selected precision. */
142 std::string asString( const Unit & unit_r,
143 unsigned field_width_r = 0,
144 unsigned unit_width_r = 1 ) const
145 { return asString( unit_r, field_width_r, unit_width_r, unit_r.prec() ); }
146 /** Nothing auto selected. */
147 std::string asString( const Unit & unit_r,
148 unsigned field_width_r,
149 unsigned unit_width_r,
150 unsigned prec_r ) const
151 { return unit_r.form( _count, field_width_r, unit_width_r, prec_r ); }
157 ///////////////////////////////////////////////////////////////////
159 /** \relates ByteCount Stream output */
160 inline std::ostream & operator<<( std::ostream & str, const ByteCount & obj )
161 { return str << obj.asString(); }
163 /////////////////////////////////////////////////////////////////
165 ///////////////////////////////////////////////////////////////////
166 #endif // ZYPP_BYTECOUNT_H