backup
[platform/upstream/libzypp.git] / zypp / ByteCount.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ByteCount.h
10  *
11 */
12 #ifndef ZYPP_BYTECOUNT_H
13 #define ZYPP_BYTECOUNT_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/Unit.h"
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   ///////////////////////////////////////////////////////////////////
24   //
25   //    CLASS NAME : ByteCount
26   //
27   /** Store and operate with byte count.
28    *
29   */
30   class ByteCount
31   {
32     friend std::ostream & operator<<( std::ostream & str, const ByteCount & obj );
33
34   public:
35
36     typedef base::Unit      Unit;
37     typedef Unit::ValueType SizeType;
38
39     /** \name Byte unit constants. */
40     //@{
41     /** 1 Byte */
42     static const Unit B;
43     /** 1024 Byte */
44     static const Unit K;
45     /** 1024^2 Byte */
46     static const Unit M;
47     /** 1024^3 Byte */
48     static const Unit G;
49     /** 1024^4 Byte */
50     static const Unit T;
51     /** 1000 Byte */
52     static const Unit kB;
53     /** 1000^2 Byte */
54     static const Unit MB;
55     /** 1000^3 Byte */
56     static const Unit GB;
57     /** 1000^4 Byte */
58     static const Unit TB;
59     //@}
60
61   public:
62
63     /** Default ctor */
64     ByteCount()
65     : _count( 0 )
66     {}
67     /** Ctor taking 1 Unit. */
68     ByteCount( const Unit & unit_r )
69     : _count( unit_r.factor() )
70     {}
71     /** Ctor taking a count and optinal Unit. */
72     ByteCount( const SizeType count_r, const Unit & unit_r = B )
73     : _count( count_r * unit_r.factor() )
74     {}
75
76   public:
77
78     /** Conversion to SizeType. */
79     operator SizeType() const
80     { return _count; }
81
82     /** \name Arithmetic operations.
83      * \c + \c - \c * \c / are provided via conversion to SizeType.
84     */
85     //@{
86     ByteCount & operator+=( const SizeType rhs ) { _count += rhs; return *this; }
87     ByteCount & operator-=( const SizeType rhs ) { _count -= rhs; return *this; }
88     ByteCount & operator*=( const SizeType rhs ) { _count *= rhs; return *this; }
89     ByteCount & operator/=( const SizeType rhs ) { _count /= rhs; return *this; }
90
91     ByteCount & operator++(/*prefix*/) { _count += 1; return *this; }
92     ByteCount & operator--(/*prefix*/) { _count -= 1; return *this; }
93
94     ByteCount operator++(int/*postfix*/) { return _count++; }
95     ByteCount operator--(int/*postfix*/) { return _count--; }
96     //@}
97
98     /** Adjust count to multiple of \a blocksize_r (default 1K).
99      * Zero blocksize_r is treated as 1B.
100     */
101     ByteCount & fillBlock( ByteCount blocksize_r = K );
102
103     /** Return count adjusted to multiple of \a blocksize_r (default 1K). */
104     ByteCount fullBlocks( ByteCount blocksize_r = K ) const
105     { return ByteCount(*this).fillBlock( blocksize_r ); }
106
107     /** Return number of blocks of size \a blocksize_r (default 1K). */
108     SizeType blocks( ByteCount blocksize_r = K ) const
109     { return fullBlocks( blocksize_r ) / blocksize_r; }
110
111   public:
112
113     /** Return the best Unit (B,K,M,G,T) for count. */
114     const Unit & bestUnit() const;
115
116     /** Return the best Unit (B,kB,MB,GB,TB) for count. */
117     const Unit & bestUnit1000() const;
118
119     /** \name Conversion to string.
120      * \li \a field_width_r Width for the number part (incl. precision)
121      * \li \a unit_width_r With for the unit symbol (without symbol if zero)
122      * \li \a prec_r Precision to use.
123      * \see zypp::base::Unit
124     */
125     //@{
126     /** Auto selected Unit and precision. */
127     std::string asString( unsigned field_width_r = 0,
128                           unsigned unit_width_r  = 1 ) const
129     { return asString( bestUnit(), field_width_r, unit_width_r ); }
130     /** Auto selected Unit. */
131     std::string asString( unsigned field_width_r,
132                           unsigned unit_width_r,
133                           unsigned prec_r ) const
134     { return asString( bestUnit(), field_width_r, unit_width_r, prec_r ); }
135     /** Auto selected precision. */
136     std::string asString( const Unit & unit_r,
137                           unsigned field_width_r = 0,
138                           unsigned unit_width_r  = 1 ) const
139     { return asString( unit_r, field_width_r, unit_width_r, unit_r.prec() ); }
140     /** Nothing auto selected. */
141     std::string asString( const Unit & unit_r,
142                           unsigned field_width_r,
143                           unsigned unit_width_r,
144                           unsigned prec_r ) const
145     { return unit_r.form( _count, field_width_r, unit_width_r, prec_r ); }
146     //@}
147
148   private:
149     SizeType _count;
150   };
151   ///////////////////////////////////////////////////////////////////
152
153   /** \relates ByteCount Stream output */
154   inline std::ostream & operator<<( std::ostream & str, const ByteCount & obj )
155   { return str << obj.asString(); }
156
157   /////////////////////////////////////////////////////////////////
158 } // namespace zypp
159 ///////////////////////////////////////////////////////////////////
160 #endif // ZYPP_BYTECOUNT_H