libzypp  13.10.6
ByteCount.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BYTECOUNT_H
13 #define ZYPP_BYTECOUNT_H
14 
15 #include <iosfwd>
16 
17 #include "zypp/base/Unit.h"
18 
20 namespace zypp
21 {
22 
24  //
25  // CLASS NAME : ByteCount
26  //
30  class ByteCount
31  {
32  friend std::ostream & operator<<( std::ostream & str, const ByteCount & obj );
33 
34  public:
35 
36  typedef base::Unit Unit;
38 
42  static const Unit B;
43 
45  static const Unit K;
46  static const Unit KiB;
48  static const Unit M;
49  static const Unit MiB;
51  static const Unit G;
52  static const Unit GiB;
54  static const Unit T;
55  static const Unit TiB;
56 
58  static const Unit kB;
60  static const Unit MB;
62  static const Unit GB;
64  static const Unit TB;
66 
67  public:
68 
71  : _count( 0 )
72  {}
74  ByteCount( const Unit & unit_r )
75  : _count( unit_r.factor() )
76  {}
78  ByteCount( const SizeType count_r, const Unit & unit_r = B )
79  : _count( count_r * unit_r.factor() )
80  {}
81 
82  public:
83 
85  operator SizeType() const
86  { return _count; }
87 
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; }
96 
97  ByteCount & operator++(/*prefix*/) { _count += 1; return *this; }
98  ByteCount & operator--(/*prefix*/) { _count -= 1; return *this; }
99 
100  ByteCount operator++(int/*postfix*/) { return _count++; }
101  ByteCount operator--(int/*postfix*/) { return _count--; }
103 
107  ByteCount & fillBlock( ByteCount blocksize_r = K );
108 
110  ByteCount fullBlocks( ByteCount blocksize_r = K ) const
111  { return ByteCount(*this).fillBlock( blocksize_r ); }
112 
114  SizeType blocks( ByteCount blocksize_r = K ) const
115  { return fullBlocks( blocksize_r ) / blocksize_r; }
116 
117  public:
118 
120  const Unit & bestUnit() const;
121 
123  const Unit & bestUnit1000() const;
124 
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 ); }
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 ); }
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() ); }
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 ); }
153 
154  private:
156  };
158 
160  inline std::ostream & operator<<( std::ostream & str, const ByteCount & obj )
161  { return str << obj.asString(); }
162 
164 } // namespace zypp
166 #endif // ZYPP_BYTECOUNT_H
ByteCount operator++(int)
Definition: ByteCount.h:100
std::ostream & operator<<(std::ostream &str, const ByteCount &obj)
Definition: ByteCount.h:160
std::string asString(unsigned field_width_r, unsigned unit_width_r, unsigned prec_r) const
Auto selected Unit.
Definition: ByteCount.h:137
SizeType _count
Definition: ByteCount.h:155
ByteCount & operator-=(const SizeType rhs)
Definition: ByteCount.h:93
Unit::ValueType SizeType
Definition: ByteCount.h:37
ByteCount & operator++()
Definition: ByteCount.h:97
ByteCount & operator/=(const SizeType rhs)
Definition: ByteCount.h:95
Store and operate with byte count.
Definition: ByteCount.h:30
static const Unit GB
1000^3 Byte
Definition: ByteCount.h:62
static const Unit TiB
Definition: ByteCount.h:55
std::string form(ValueType val_r, unsigned field_width_r=0, unsigned unit_width_r=1) const
Build string representation of val_r.
Definition: Unit.h:70
std::string asString(const Unit &unit_r, unsigned field_width_r=0, unsigned unit_width_r=1) const
Auto selected precision.
Definition: ByteCount.h:142
std::string asString(unsigned field_width_r=0, unsigned unit_width_r=1) const
Auto selected Unit and precision.
Definition: ByteCount.h:133
static const Unit MB
1000^2 Byte
Definition: ByteCount.h:60
ByteCount fullBlocks(ByteCount blocksize_r=K) const
Return count adjusted to multiple of blocksize_r (default 1K).
Definition: ByteCount.h:110
base::Unit Unit
Definition: ByteCount.h:36
const Unit & bestUnit1000() const
Return the best Unit (B,kB,MB,GB,TB) for count.
Definition: ByteCount.cc:89
ByteCount & operator+=(const SizeType rhs)
Definition: ByteCount.h:92
ByteCount & fillBlock(ByteCount blocksize_r=K)
Adjust count to multiple of blocksize_r (default 1K).
Definition: ByteCount.cc:43
static const Unit G
1024^3 Byte
Definition: ByteCount.h:51
ByteCount & operator*=(const SizeType rhs)
Definition: ByteCount.h:94
static const Unit GiB
Definition: ByteCount.h:52
friend std::ostream & operator<<(std::ostream &str, const ByteCount &obj)
unsigned prec() const
Definition: Unit.h:66
ByteCount()
Default ctor.
Definition: ByteCount.h:70
static const Unit B
1 Byte
Definition: ByteCount.h:42
ByteCount(const Unit &unit_r)
Ctor taking 1 Unit.
Definition: ByteCount.h:74
ByteCount operator--(int)
Definition: ByteCount.h:101
Simple handling of Units.
Definition: Unit.h:42
static const Unit M
1024^2 Byte
Definition: ByteCount.h:48
ByteCount(const SizeType count_r, const Unit &unit_r=B)
Ctor taking a count and optinal Unit.
Definition: ByteCount.h:78
static const Unit K
1024 Byte
Definition: ByteCount.h:45
static const Unit MiB
Definition: ByteCount.h:49
static const Unit T
1024^4 Byte
Definition: ByteCount.h:54
static const Unit TB
1000^4 Byte
Definition: ByteCount.h:64
static const Unit kB
1000 Byte
Definition: ByteCount.h:58
const Unit & bestUnit() const
Return the best Unit (B,K,M,G,T) for count.
Definition: ByteCount.cc:70
long long ValueType
Definition: Unit.h:45
std::string asString(const Unit &unit_r, unsigned field_width_r, unsigned unit_width_r, unsigned prec_r) const
Nothing auto selected.
Definition: ByteCount.h:147
SizeType blocks(ByteCount blocksize_r=K) const
Return number of blocks of size blocksize_r (default 1K).
Definition: ByteCount.h:114
ByteCount & operator--()
Definition: ByteCount.h:98
static const Unit KiB
Definition: ByteCount.h:46