libzypp  17.36.1
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/Globals.h>
18 #include <zypp-core/base/Unit.h>
19 
21 namespace zypp
22 {
23 
25  //
26  // CLASS NAME : ByteCount
27  //
32  {
33  friend std::ostream & operator<<( std::ostream & str, const ByteCount & obj );
34 
35  public:
36 
37  using Unit = base::Unit;
39 
43  static const Unit B;
44 
46  static const Unit K;
47  static const Unit KiB;
49  static const Unit M;
50  static const Unit MiB;
52  static const Unit G;
53  static const Unit GiB;
55  static const Unit T;
56  static const Unit TiB;
57 
59  static const Unit kB;
61  static const Unit MB;
63  static const Unit GB;
65  static const Unit TB;
67 
68  public:
69 
72  : _count( 0 )
73  {}
75  ByteCount( const Unit & unit_r )
76  : _count( unit_r.factor() )
77  {}
79  ByteCount( const SizeType count_r, const Unit & unit_r = B )
80  : _count( count_r * unit_r.factor() )
81  {}
82 
83  public:
84 
86  operator SizeType() const
87  { return _count; }
88 
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  ByteCount & operator/=( const SizeType rhs ) { _count /= rhs; return *this; }
97 
98  ByteCount & operator++(/*prefix*/) { _count += 1; return *this; }
99  ByteCount & operator--(/*prefix*/) { _count -= 1; return *this; }
100 
101  ByteCount operator++(int/*postfix*/) { return _count++; }
102  ByteCount operator--(int/*postfix*/) { return _count--; }
104 
108  ByteCount & fillBlock( ByteCount blocksize_r = K );
109 
111  ByteCount fullBlocks( ByteCount blocksize_r = K ) const
112  { return ByteCount(*this).fillBlock( blocksize_r ); }
113 
115  SizeType blocks( ByteCount blocksize_r = K ) const
116  { return fullBlocks( blocksize_r ) / blocksize_r; }
117 
118  public:
119 
121  const Unit & bestUnit() const;
122 
124  const Unit & bestUnit1000() const;
125 
134  std::string asString( unsigned field_width_r = 0,
135  unsigned unit_width_r = 1 ) const
136  { return asString( bestUnit(), field_width_r, unit_width_r ); }
138  std::string asString( unsigned field_width_r,
139  unsigned unit_width_r,
140  unsigned prec_r ) const
141  { return asString( bestUnit(), field_width_r, unit_width_r, prec_r ); }
143  std::string asString( const Unit & unit_r,
144  unsigned field_width_r = 0,
145  unsigned unit_width_r = 1 ) const
146  { return asString( unit_r, field_width_r, unit_width_r, unit_r.prec() ); }
148  std::string asString( const Unit & unit_r,
149  unsigned field_width_r,
150  unsigned unit_width_r,
151  unsigned prec_r ) const
152  { return unit_r.form( _count, field_width_r, unit_width_r, prec_r ); }
154 
155  private:
157  };
159 
161  inline std::ostream & operator<<( std::ostream & str, const ByteCount & obj )
162  { return str << obj.asString(); }
163 
165 } // namespace zypp
167 #endif // ZYPP_BYTECOUNT_H
std::string asString(const Patch::Category &obj)
Definition: Patch.cc:122
ByteCount operator++(int)
Definition: ByteCount.h:101
std::ostream & operator<<(std::ostream &str, const ByteCount &obj)
Definition: ByteCount.h:161
SizeType _count
Definition: ByteCount.h:156
ByteCount & operator-=(const SizeType rhs)
Definition: ByteCount.h:94
ByteCount & operator++()
Definition: ByteCount.h:98
ByteCount & operator/=(const SizeType rhs)
Definition: ByteCount.h:96
Store and operate with byte count.
Definition: ByteCount.h:31
static const Unit GB
1000^3 Byte
Definition: ByteCount.h:63
static const Unit TiB
Definition: ByteCount.h:56
String related utilities and Regular expression matching.
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:73
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
std::string asString(const Unit &unit_r, unsigned field_width_r=0, unsigned unit_width_r=1) const
Auto selected precision.
Definition: ByteCount.h:143
static const Unit MB
1000^2 Byte
Definition: ByteCount.h:61
long long ValueType
Definition: Unit.h:48
ByteCount & operator+=(const SizeType rhs)
Definition: ByteCount.h:93
unsigned prec() const
Definition: Unit.h:69
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
Definition: ResTraits.h:93
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:52
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:148
std::string asString(unsigned field_width_r=0, unsigned unit_width_r=1) const
Auto selected Unit and precision.
Definition: ByteCount.h:134
ByteCount & operator*=(const SizeType rhs)
Definition: ByteCount.h:95
static const Unit GiB
Definition: ByteCount.h:53
SizeType blocks(ByteCount blocksize_r=K) const
Return number of blocks of size blocksize_r (default 1K).
Definition: ByteCount.h:115
ByteCount()
Default ctor.
Definition: ByteCount.h:71
static const Unit B
1 Byte
Definition: ByteCount.h:43
ByteCount(const Unit &unit_r)
Ctor taking 1 Unit.
Definition: ByteCount.h:75
ByteCount operator--(int)
Definition: ByteCount.h:102
Simple handling of Units.
Definition: Unit.h:45
static const Unit M
1024^2 Byte
Definition: ByteCount.h:49
ByteCount(const SizeType count_r, const Unit &unit_r=B)
Ctor taking a count and optinal Unit.
Definition: ByteCount.h:79
static const Unit K
1024 Byte
Definition: ByteCount.h:46
static const Unit MiB
Definition: ByteCount.h:50
static const Unit T
1024^4 Byte
Definition: ByteCount.h:55
static const Unit TB
1000^4 Byte
Definition: ByteCount.h:65
static const Unit kB
1000 Byte
Definition: ByteCount.h:59
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
Unit::ValueType SizeType
Definition: ByteCount.h:38
std::string asString(unsigned field_width_r, unsigned unit_width_r, unsigned prec_r) const
Auto selected Unit.
Definition: ByteCount.h:138
ByteCount fullBlocks(ByteCount blocksize_r=K) const
Return count adjusted to multiple of blocksize_r (default 1K).
Definition: ByteCount.h:111
ByteCount & operator--()
Definition: ByteCount.h:99
static const Unit KiB
Definition: ByteCount.h:47