lib45d
45Drives C++ Library API Documentation
Quota.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 <45d/Bytes.hpp>
24 
25 namespace ffd {
30  class Quota : public Bytes {
31  public:
38  DOWN,
39  UP
40  };
47  Quota(const Bytes &max, const std::string &str, RoundingMethod method = NEAREST);
55  Quota(const Bytes &max, double fraction = 1.0, RoundingMethod method = NEAREST)
56  : Bytes(max)
57  , fraction_(fraction)
58  , rounding_method_(method) {}
59  Quota(const Bytes &max, const Bytes &allotted, RoundingMethod method = NEAREST)
60  : Bytes(max)
61  , fraction_(allotted / max)
62  , rounding_method_(method) {}
67  Quota(void) : Bytes(), fraction_(0), rounding_method_(NEAREST) {}
73  Quota(const Quota &other)
74  : Bytes(other.bytes_)
75  , fraction_(other.fraction_)
76  , rounding_method_(other.rounding_method_) {}
82  Quota(Quota &&other)
83  : Bytes(std::move(other))
84  , fraction_(std::move(other.fraction_))
85  , rounding_method_(std::move(other.rounding_method_)) {}
92  Quota &operator=(const Quota &other) {
93  bytes_ = other.bytes_;
94  fraction_ = other.fraction_;
95  rounding_method_ = other.rounding_method_;
96  return *this;
97  }
104  Quota &operator=(Quota &&other) {
105  bytes_ = std::move(other.bytes_);
106  fraction_ = std::move(other.fraction_);
107  rounding_method_ = std::move(other.rounding_method_);
108  return *this;
109  }
113  ~Quota() = default;
114  void set_rounding_method(RoundingMethod method) {
115  rounding_method_ = method;
116  }
122  bytes_type get(void) const {
123  return round(double(bytes_) * fraction_);
124  }
130  bytes_type get_max(void) const {
131  return bytes_;
132  }
138  double get_fraction(void) const {
139  return fraction_;
140  }
146  void set_fraction(double fraction) {
147  fraction_ = fraction;
148  }
154  void set_fraction(const std::string &str) {
155  fraction_ = parse_fraction(str);
156  }
163  double parse_fraction(const std::string &str);
164  private:
165  double fraction_;
166  RoundingMethod rounding_method_;
167 
173  bytes_type round(double x) const {
174  switch (rounding_method_) {
175  case RoundingMethod::NEAREST:
176  return bytes_type(::round(x));
177  case RoundingMethod::DOWN:
178  return bytes_type(x);
179  case RoundingMethod::UP:
180  return bytes_type(ceil(x));
181  default:
182  return bytes_type(::round(x));
183  }
184  }
185  };
186 } // namespace ffd
ffd::Quota::set_fraction
void set_fraction(double fraction)
Set the fraction.
Definition: Quota.hpp:146
ffd::Quota::Quota
Quota(Quota &&other)
Move constructor.
Definition: Quota.hpp:82
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::Quota::get_max
bytes_type get_max(void) const
Get the max number of bytes from parent.
Definition: Quota.hpp:130
ffd::Quota::get
bytes_type get(void) const
Get value in bytes equal to total bytes_ * fraction_.
Definition: Quota.hpp:122
ffd::Quota
This class extends ffd::Bytes to specify percents of an amount of bytes.
Definition: Quota.hpp:30
ffd::Quota::Quota
Quota(const Quota &other)
Copy constructor.
Definition: Quota.hpp:73
ffd::Quota::operator=
Quota & operator=(const Quota &other)
Copy assignment.
Definition: Quota.hpp:92
ffd::Quota::NEAREST
@ NEAREST
Round to nearest whole byte.
Definition: Quota.hpp:37
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::Quota::parse_fraction
double parse_fraction(const std::string &str)
Parse str from a percentage to a decimal fraction.
Definition: quota.cpp:33
ffd::Quota::~Quota
~Quota()=default
Destroy the Quota object.
ffd::Quota::DOWN
@ DOWN
Floor.
Definition: Quota.hpp:38
ffd::Quota::UP
@ UP
Ceiling.
Definition: Quota.hpp:39
ffd::Quota::Quota
Quota(void)
Construct a new empty Quota object.
Definition: Quota.hpp:67
ffd::Quota::operator=
Quota & operator=(Quota &&other)
Assignment move constructor.
Definition: Quota.hpp:104
ffd::Quota::Quota
Quota(const Bytes &max, double fraction=1.0, RoundingMethod method=NEAREST)
Construct a new Quota object.
Definition: Quota.hpp:55
ffd::Quota::get_fraction
double get_fraction(void) const
Get just the fraction as a double.
Definition: Quota.hpp:138
ffd::Quota::set_fraction
void set_fraction(const std::string &str)
Set fraction from formatted string /\d+(.\d*)?\s*%?/.
Definition: Quota.hpp:154
ffd::Quota::RoundingMethod
RoundingMethod
Rounding method enum.
Definition: Quota.hpp:36