Imported Upstream version 14.32.2
[platform/upstream/libzypp.git] / zypp / base / Unit.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/Unit.h
10  *
11 */
12 #ifndef ZYPP_BASE_UNIT_H
13 #define ZYPP_BASE_UNIT_H
14
15 #include <iosfwd>
16 #include <string>
17
18 ///////////////////////////////////////////////////////////////////
19 namespace zypp
20 { /////////////////////////////////////////////////////////////////
21   ///////////////////////////////////////////////////////////////////
22   namespace base
23   { /////////////////////////////////////////////////////////////////
24
25     ///////////////////////////////////////////////////////////////////
26     //
27     //  CLASS NAME : Unit
28     //
29     /** Simple handling of Units.
30      *
31      * Unit stores factor and symbol, and a precision value for printing.
32      * \ref form builds a string from a value according to the format
33      * specification.
34      * \code
35      * static const Unit B( 1, "B", 0 );
36      * static const Unit K( 1024, "K", 1 );
37      * static const Unit M( 1048576, "M", 1 );
38      * static const Unit G( 1073741824, "G", 2 );
39      * static const Unit T( 1099511627776, "T", 3 );
40      * \endcode
41     */
42       class Unit
43       {
44       public:
45         typedef long long ValueType;
46
47         /** Default ctor */
48         Unit()
49         : _factor( 1 )
50         , _prec( 0 )
51         {}
52
53         /** ctor */
54         Unit( ValueType factor_r, std::string symbol_r, unsigned prec_r )
55         : _factor( factor_r )
56         , _symbol( symbol_r )
57         , _prec( prec_r )
58         {}
59
60         ValueType factor() const
61         { return _factor; }
62
63         const std::string & symbol() const
64         { return _symbol; }
65
66         unsigned prec() const
67         { return _prec; }
68
69         /** Build string representation of \a val_r. */
70         std::string form( ValueType val_r,
71                           unsigned field_width_r = 0,
72                           unsigned unit_width_r  = 1 ) const
73         { return form( val_r, field_width_r, unit_width_r, _prec ); }
74
75         std::string form( ValueType val_r,
76                           unsigned field_width_r,
77                           unsigned unit_width_r,
78                           unsigned prec_r ) const
79         { return form( double(val_r)/_factor, _symbol,
80                        field_width_r, unit_width_r, prec_r ); }
81
82
83         static std::string form( double val_r,
84                                  const std::string & symbol_r,
85                                  unsigned field_width_r,
86                                  unsigned unit_width_r,
87                                  unsigned prec_r );
88
89       private:
90         ValueType   _factor;
91         std::string _symbol;
92         unsigned    _prec;
93       };
94     ///////////////////////////////////////////////////////////////////
95
96
97     /////////////////////////////////////////////////////////////////
98   } // namespace base
99   ///////////////////////////////////////////////////////////////////
100   /////////////////////////////////////////////////////////////////
101 } // namespace zypp
102 ///////////////////////////////////////////////////////////////////
103 #endif // ZYPP_BASE_UNIT_H