lib45d
45Drives C++ Library Development Documentation
Bytes.hpp
1 // -*- C++ -*-
2 /*
3  * Copyright (C) 2021 Joshua Boudreau <jboudreau@45drives.com>
4  *
5  * This file is part of lib45d.
6  *
7  * lib45d is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * lib45d is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with lib45d. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include <cmath>
24 #include <iostream>
25 #include <string>
26 
27 namespace ffd {
32  class Bytes {
33  public:
34  typedef off64_t bytes_type;
35  enum PrefixType { BINARY, SI };
36 
41  Bytes(const std::string &str);
47  Bytes(bytes_type bytes) : bytes_(bytes) {}
52  Bytes(void) : bytes_(0) {}
58  Bytes(const Bytes &other) : bytes_(other.get()) {}
64  Bytes(Bytes &&other) : bytes_(std::move(other.bytes_)) {}
71  Bytes &operator=(const Bytes &other) {
72  bytes_ = other.get();
73  return *this;
74  }
81  Bytes &operator=(Bytes &&other) {
82  bytes_ = std::move(other.bytes_);
83  return *this;
84  }
88  ~Bytes() = default;
94  virtual bytes_type get(void) const {
95  return bytes_;
96  }
105  std::string get_str(enum PrefixType prefix_type = BINARY, int precision = 2) const;
111  void set(bytes_type val) {
112  bytes_ = val;
113  }
119  void set(const std::string &str) {
120  bytes_ = parse_bytes(str);
121  }
129  friend std::ostream &operator<<(std::ostream &os, Bytes const &bytes) {
130  return os << bytes.get_str();
131  }
139  friend std::istream &operator>>(std::istream &is, Bytes &bytes) {
140  std::string str;
141  std::getline(is, str);
142  bytes.set(str);
143  return is;
144  }
145  Bytes &operator+=(const Bytes &other) {
146  bytes_ += other.get();
147  return *this;
148  }
149  Bytes &operator-=(const Bytes &other) {
150  bytes_ -= other.get();
151  return *this;
152  }
153  friend Bytes operator+(const Bytes &a, const Bytes &b) {
154  return Bytes(a.get() + b.get());
155  }
156  friend Bytes operator-(const Bytes &a, const Bytes &b) {
157  return Bytes(a.get() - b.get());
158  }
159  friend Bytes operator*(const Bytes &a, int b) {
160  return Bytes(a.get() * b);
161  }
162  friend Bytes operator*(int a, const Bytes &b) {
163  return b * a;
164  }
165  friend Bytes operator/(const Bytes &a, int b) {
166  return Bytes(a.get() / b);
167  }
168  friend double operator/(const Bytes &a, const Bytes &b) {
169  return double(a.get()) / double(b.get());
170  }
171  friend bool operator==(const Bytes &a, const Bytes &b) {
172  return a.get() == b.get();
173  }
174  friend bool operator!=(const Bytes &a, const Bytes &b) {
175  return !(operator==(a, b));
176  }
177  friend bool operator<(const Bytes &a, const Bytes &b) {
178  return a.get() < b.get();
179  }
180  friend bool operator>(const Bytes &a, const Bytes &b) {
181  return b.get() < a.get();
182  }
183  friend bool operator<=(const Bytes &a, const Bytes &b) {
184  return !(operator>(a, b));
185  }
186  friend bool operator>=(const Bytes &a, const Bytes &b) {
187  return !(operator<(a, b));
188  }
189  protected:
191 
197  bytes_type parse_bytes(const std::string &str) const;
198  };
199 } // namespace ffd
ffd::Bytes::operator=
Bytes & operator=(const Bytes &other)
Copy assignment.
Definition: Bytes.hpp:71
ffd::Bytes::Bytes
Bytes(bytes_type bytes)
Construct a new Bytes object from integral type.
Definition: Bytes.hpp:47
ffd::Bytes::Bytes
Bytes(const Bytes &other)
Copy construct a new Bytes object.
Definition: Bytes.hpp:58
ffd::Bytes::operator=
Bytes & operator=(Bytes &&other)
Assignment move constructor.
Definition: Bytes.hpp:81
ffd::Bytes
Use this class for byte-formatted values. e.g.: "123 KiB".
Definition: Bytes.hpp:32
ffd::Bytes::bytes_
bytes_type bytes_
The actual value.
Definition: Bytes.hpp:190
ffd::Bytes::operator>>
friend std::istream & operator>>(std::istream &is, Bytes &bytes)
Stream extraction operator.
Definition: Bytes.hpp:139
ffd::Bytes::get
virtual bytes_type get(void) const
Get value in bytes.
Definition: Bytes.hpp:94
ffd
45Drives namespace
Definition: Bytes.hpp:27
ffd::Bytes::bytes_type
off64_t bytes_type
Type to store number of bytes.
Definition: Bytes.hpp:34
ffd::Bytes::get_str
std::string get_str(enum PrefixType prefix_type=BINARY, int precision=2) const
Get value as formatted string.
Definition: bytes.cpp:33
ffd::Bytes::~Bytes
~Bytes()=default
Destroy the Bytes object.
ffd::Bytes::parse_bytes
bytes_type parse_bytes(const std::string &str) const
Parse bytes from string.
Definition: bytes.cpp:63
ffd::Bytes::set
void set(bytes_type val)
Set value from integral type.
Definition: Bytes.hpp:111
ffd::Bytes::set
void set(const std::string &str)
Set value from formatted string /\d+(.\d*)?\s*[kmgtpezy]?i?b/i.
Definition: Bytes.hpp:119
ffd::Bytes::Bytes
Bytes(void)
Construct a new empty Bytes object.
Definition: Bytes.hpp:52
ffd::Bytes::Bytes
Bytes(Bytes &&other)
Move constructor.
Definition: Bytes.hpp:64
ffd::Bytes::operator<<
friend std::ostream & operator<<(std::ostream &os, Bytes const &bytes)
Stream insertion operator.
Definition: Bytes.hpp:129