DUDS
Distributed Update of Data from Something
DataSize.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2018 Jeff Jackowski
9  */
10 #ifndef DATASIZES_HPP
11 #define DATASIZES_HPP
12 
13 #include <duds/general/Errors.hpp>
14 #include <cstdint>
15 
16 namespace duds { namespace general {
17 
24 struct DataSizeConversionError : virtual std::exception, virtual boost::exception { };
25 
29 typedef boost::error_info<struct Info_DataSizeSourceUnit, std::size_t>
31 
35 typedef boost::error_info<struct Info_DataSizeTargetUnit, std::size_t>
37 
43 typedef boost::error_info<struct Info_DataSizeBlocks, std::size_t>
45 
46 #if defined(DUDS_ERRORS_VERBOSE) || defined(DOXYGEN)
47 
57 #define THROW_DATASIZE_CONVERSION_ERROR(bits, trgbits, blocks) \
58  throw boost::enable_current_exception(DataSizeConversionError()) << \
59  DataSizeSourceUnit(bits) << DataSizeTargetUnit(trgbits) << \
60  DataSizeBlocks(blocks) << \
61  boost::throw_function(BOOST_CURRENT_FUNCTION) << \
62  boost::throw_file(__FILE__) << ::boost::throw_line((int)__LINE__) << \
63  duds::general::StackTrace(boost::stacktrace::stacktrace())
64 #else
65 #define THROW_DATASIZE_CONVERSION_ERROR(bits, trgbits, blocks) \
66  throw boost::enable_current_exception(DataSizeConversionError()) << \
67  DataSizeSourceUnit(bits) << DataSizeTargetUnit(trgbits) << \
68  DataSizeBlocks(blocks) << \
69  boost::throw_function(BOOST_CURRENT_FUNCTION) << \
70  boost::throw_file(__FILE__) << ::boost::throw_line((int)__LINE__)
71 #endif
72 
105 template <std::size_t Bits>
106 class DataSize {
110  std::size_t numblocks;
111 public:
115  static constexpr std::size_t blockSize() {
116  return Bits;
117  }
121  DataSize() = default;
127  constexpr DataSize(std::size_t blocks) : numblocks(blocks) { }
150  template <std::size_t ConvBits>
151  constexpr DataSize(const DataSize<ConvBits> &ds) :
152  numblocks(
153  // the throw prevents compilation when the conditional is false and
154  // this function is used in a constexpr expression
155  (ds.bits() % Bits) ?
156  THROW_DATASIZE_CONVERSION_ERROR(ConvBits, Bits, ds.blocks()) :
157  (ds.bits() / Bits)
158  ) { }
162  constexpr std::size_t blocks() const {
163  return numblocks;
164  }
170  constexpr std::uint64_t bits() const {
171  return (std::uint64_t)numblocks * (std::uint64_t)Bits;
172  }
188  template <class OtherDataSize>
189  constexpr OtherDataSize size() const {
190  // the throw prevents compilation when the conditional is false and
191  // this function is used in a constexpr expression
192  return (bits() % OtherDataSize::blockSize()) ?
193  THROW_DATASIZE_CONVERSION_ERROR(Bits, OtherDataSize::blockSize(), numblocks) :
194  OtherDataSize(bits() / OtherDataSize::blockSize());
195  }
212  template <std::size_t ConvBits>
213  constexpr operator DataSize<ConvBits>() const {
214  return size<ConvBits>();
215  }
225  template <class OtherDataSize>
226  constexpr OtherDataSize sizeRounded() const {
227  return OtherDataSize(
228  (bits() / OtherDataSize::blockSize()) +
229  ((bits() % OtherDataSize::blockSize()) ? 1 : 0)
230  );
231  }
241  constexpr std::uint64_t bytes() const {
242  return size< DataSize<8> >().blocks();
243  }
249  constexpr std::uint64_t bytesRounded() const {
250  return sizeRounded< DataSize<8> >().blocks();
251  }
256  template <std::size_t OtherBits>
257  constexpr bool operator == (const DataSize<OtherBits> &op) const {
258  return bits() == op.bits();
259  }
264  template <std::size_t OtherBits>
265  constexpr bool operator != (const DataSize<OtherBits> &op) const {
266  return bits() != op.bits();
267  }
272  template <std::size_t OtherBits>
273  constexpr bool operator > (const DataSize<OtherBits> &op) const {
274  return bits() > op.bits();
275  }
280  template <std::size_t OtherBits>
281  constexpr bool operator < (const DataSize<OtherBits> &op) const {
282  return bits() < op.bits();
283  }
288  template <std::size_t OtherBits>
289  constexpr bool operator >= (const DataSize<OtherBits> &op) const {
290  return bits() >= op.bits();
291  }
296  template <std::size_t OtherBits>
297  constexpr bool operator <= (const DataSize<OtherBits> &op) const {
298  return bits() <= op.bits();
299  }
310  constexpr DataSize operator + (const DataSize &op) const {
311  return DataSize(numblocks + op.numblocks);
312  }
323  constexpr DataSize operator - (const DataSize &op) const {
324  return DataSize(numblocks - op.numblocks);
325  }
329  DataSize &operator += (const DataSize &op) {
330  numblocks += op.numblocks;
331  return *this;
332  }
336  DataSize &operator -= (const DataSize &op) {
337  numblocks -= op.numblocks;
338  return *this;
339  }
344  constexpr DataSize operator * (int scalar) const {
345  return DataSize(numblocks * scalar);
346  }
352  constexpr DataSize operator / (int scalar) const {
353  return DataSize(numblocks / scalar);
354  }
358  DataSize &operator *= (int scalar) {
359  numblocks *= scalar;
360  return *this;
361  }
365  DataSize &operator /= (int scalar) {
366  numblocks /= scalar;
367  return *this;
368  }
369 };
370 
375 
380 
385 
390 
395 
400 
405 
410 
411 } }
412 
413 #endif // #ifndef DATASIZES_HPP
DataSize< 1000 > Kilobits
DataSize type for a size in kilobits.
Definition: DataSize.hpp:399
Used to report a failure to make an exact conversion of a size represented by a DataSize object...
Definition: DataSize.hpp:24
static constexpr std::size_t blockSize()
Returns the number of bits in each unit for this type; same as Bits.
Definition: DataSize.hpp:115
boost::error_info< struct Info_DataSizeBlocks, std::size_t > DataSizeBlocks
The number of blocks from a DataSize object that cannot be converted.
Definition: DataSize.hpp:44
constexpr bool operator==(const DigitalPinCap &cap, const DigitalPinCap::Flags &flg)
constexpr std::uint64_t bytesRounded() const
Returns the size specified in bytes, rounded up.
Definition: DataSize.hpp:249
constexpr std::uint64_t bits() const
Returns the size specified in bits.
Definition: DataSize.hpp:170
boost::error_info< struct Info_DataSizeTargetUnit, std::size_t > DataSizeTargetUnit
The number of bits that make up the size unit of the target DataSize object.
Definition: DataSize.hpp:36
DataSize< 1000000000 > Gigabits
DataSize type for a size in gigabits.
Definition: DataSize.hpp:409
DataSize< 1024 *1024 *8 > Megabytes
DataSize type for a size in megabytes.
Definition: DataSize.hpp:394
std::size_t numblocks
The indicated size stored as a multiple of Bits.
Definition: DataSize.hpp:110
constexpr bool operator!=(const DigitalPinCap &cap, const DigitalPinCap::Flags &flg)
DataSize< 1 > Bits
DataSize type for a size in bits.
Definition: DataSize.hpp:374
constexpr std::size_t blocks() const
Returns the size specified as a multuple of Bits.
Definition: DataSize.hpp:162
constexpr OtherDataSize size() const
Returns a new DataSize object representing the same size, but with a multiple different from Bits...
Definition: DataSize.hpp:189
constexpr OtherDataSize sizeRounded() const
Returns a new DataSize object representing a size with a multiple different from Bits that is as smal...
Definition: DataSize.hpp:226
Quantity operator*(double s, const Quantity &q)
Multiplies a Quantity by a scalar.
Definition: Quantity.hpp:225
constexpr std::uint64_t bytes() const
Returns the size specified in bytes, or fails if the size cannot be exactly represented as an integer...
Definition: DataSize.hpp:241
A class to assist with specifiying the sizes of data with scaling units, much like std::chrono::durat...
Definition: DataSize.hpp:106
DataSize< 1000000 > Megabits
DataSize type for a size in megabits.
Definition: DataSize.hpp:404
DataSize< 1024 *8 > Kilobytes
DataSize type for a size in kilobytes.
Definition: DataSize.hpp:389
constexpr DataSize(std::size_t blocks)
Initalizes the size to the indicated value.
Definition: DataSize.hpp:127
boost::error_info< struct Info_DataSizeSourceUnit, std::size_t > DataSizeSourceUnit
The number of bits that make up the size unit of the source DataSize object.
Definition: DataSize.hpp:30
constexpr DataSize(const DataSize< ConvBits > &ds)
Constructs a new DataSize object representing the same size as the given object, but with a multiple ...
Definition: DataSize.hpp:151
DataSize< 4 > Nibbles
DataSize type for a size in nibbles.
Definition: DataSize.hpp:379
General errors.
#define THROW_DATASIZE_CONVERSION_ERROR(bits, trgbits, blocks)
Throws a DataSizeConversionError exception and adds in error metadata.
Definition: DataSize.hpp:57
Quantity operator/(double s, const Quantity &q)
Divides a Quantity by a scalar.
Definition: Quantity.hpp:232
DataSize< 8 > Bytes
DataSize type for a size in bytes.
Definition: DataSize.hpp:384