DUDS
Distributed Update of Data from Something
duds::general::DataSize< Bits > Class Template Reference

A class to assist with specifiying the sizes of data with scaling units, much like std::chrono::duration does with time. More...

#include <DataSize.hpp>

Public Member Functions

 DataSize ()=default
 Trival constructor. More...
 
constexpr DataSize (std::size_t blocks)
 Initalizes the size to the indicated value. More...
 
template<std::size_t ConvBits>
constexpr DataSize (const DataSize< ConvBits > &ds)
 Constructs a new DataSize object representing the same size as the given object, but with a multiple different from Bits. More...
 
constexpr std::uint64_t bits () const
 Returns the size specified in bits. More...
 
constexpr std::size_t blocks () const
 Returns the size specified as a multuple of Bits. More...
 
constexpr std::uint64_t bytes () const
 Returns the size specified in bytes, or fails if the size cannot be exactly represented as an integer value of bytes. More...
 
constexpr std::uint64_t bytesRounded () const
 Returns the size specified in bytes, rounded up. More...
 
template<std::size_t ConvBits>
constexpr operator DataSize< ConvBits > () const
 Implicit conversion of DataSize objects to other DataSize classes using a different size multiple. More...
 
template<std::size_t OtherBits>
constexpr bool operator!= (const DataSize< OtherBits > &op) const
 Checks inequality by comparing the number of bits so that any DataSize template types may be compared without a conversion. More...
 
constexpr DataSize operator* (int scalar) const
 Muliplies the size by a scalar value. More...
 
DataSizeoperator*= (int scalar)
 Multiplies a DataSize object by a scalar and stores the result. More...
 
constexpr DataSize operator+ (const DataSize &op) const
 Adds two sizes. More...
 
DataSizeoperator+= (const DataSize &op)
 Adds two DataSize objects and stores the result. More...
 
constexpr DataSize operator- (const DataSize &op) const
 Subtracts two sizes. More...
 
DataSizeoperator-= (const DataSize &op)
 Subtracts two DataSize objects and stores the result. More...
 
constexpr DataSize operator/ (int scalar) const
 Divides the size by a scalar value. More...
 
DataSizeoperator/= (int scalar)
 Divides a DataSize object by a scalar and stores the result. More...
 
template<std::size_t OtherBits>
constexpr bool operator< (const DataSize< OtherBits > &op) const
 Less-than comparison done by comparing the number of bits so that any DataSize template types may be compared without a conversion. More...
 
template<std::size_t OtherBits>
constexpr bool operator<= (const DataSize< OtherBits > &op) const
 Less-than-or-equal comparison done by comparing the number of bits so that any DataSize template types may be compared without a conversion. More...
 
template<std::size_t OtherBits>
constexpr bool operator== (const DataSize< OtherBits > &op) const
 Checks equality by comparing the number of bits so that any DataSize template types may be compared without a conversion. More...
 
template<std::size_t OtherBits>
constexpr bool operator> (const DataSize< OtherBits > &op) const
 Greater-than comparison done by comparing the number of bits so that any DataSize template types may be compared without a conversion. More...
 
template<std::size_t OtherBits>
constexpr bool operator>= (const DataSize< OtherBits > &op) const
 Greater-than-or-equal comparison done by comparing the number of bits so that any DataSize template types may be compared without a conversion. More...
 
template<class OtherDataSize >
constexpr OtherDataSize size () const
 Returns a new DataSize object representing the same size, but with a multiple different from Bits. More...
 
template<class OtherDataSize >
constexpr OtherDataSize sizeRounded () const
 Returns a new DataSize object representing a size with a multiple different from Bits that is as small as possible while representing at least as many bits as this object. More...
 

Static Public Member Functions

static constexpr std::size_t blockSize ()
 Returns the number of bits in each unit for this type; same as Bits. More...
 

Private Attributes

std::size_t numblocks
 The indicated size stored as a multiple of Bits. More...
 

Detailed Description

template<std::size_t Bits>
class duds::general::DataSize< Bits >

A class to assist with specifiying the sizes of data with scaling units, much like std::chrono::duration does with time.

The base unit used is the bit in order to work for any application, not just data storage on common computers. The template takes the number of bits for its scalar, and stores the multiple of those bits for its indicated size. Many of the functions are declared constexpr, and the class is trivially constructable and copyable.

DataSize objects are implicitly convertable to other DataSize template types, and will convert the stored size to match units. If such a conversion cannot represent the size exactly in an integer, the conversion will fail. If the conversion is in a constexpr expression, the failure is at compile time. Otherwise, it is at runtime with a DataSizeConversionError exception. There are conversion functions that end with "Rounded" that will round up when the size cannot be exactly represented so that the result will be at least as large as the input.

Template Parameters
BitsThe number of bits that make up one unit of data for this type. For example, 8 for a byte, and 1024 for a kilobyte. Several typedefs for common sizes exist, including Bytes and Kilobytes.
See also
Bits
Kilobits
Megabits
Gigabits
Bytes
Kilobytes
Megabytes
Author
Jeff Jackowski

Definition at line 106 of file DataSize.hpp.

Constructor & Destructor Documentation

◆ DataSize() [1/3]

template<std::size_t Bits>
duds::general::DataSize< Bits >::DataSize ( )
default

Trival constructor.

◆ DataSize() [2/3]

template<std::size_t Bits>
constexpr duds::general::DataSize< Bits >::DataSize ( std::size_t  blocks)
inline

Initalizes the size to the indicated value.

The represented size will be blocks multiplied by Bits.

Parameters
blocksThe size specified as a multuple of Bits.

Definition at line 127 of file DataSize.hpp.

◆ DataSize() [3/3]

template<std::size_t Bits>
template<std::size_t ConvBits>
constexpr duds::general::DataSize< Bits >::DataSize ( const DataSize< ConvBits > &  ds)
inline

Constructs a new DataSize object representing the same size as the given object, but with a multiple different from Bits.

Note
When used in a constexpr expression, this constructor will fail to compile if ds.bits() divided by Bits has a reminder. This is intentional.
Warning
This constructor will never fail to compile if not used in an explicitly constexpr expression.
Bug:
The constructor should fail to compile when not used in an explicitly constexpr expression if Bits divided by ConvBits has a reminder, and the parameter ds is constexpr.
Template Parameters
ConvBitsThe number of bits in each unit of the converted result. This will need to be explicitly specified.
Parameters
dsThe source DataSize object.
Returns
The size converted to new units. It represents the same number of bits.
Exceptions
DataSizeConversionErrorThe size cannot be excactly represented by a DataSize<ConvBits> object. This will only be thrown from expressions that are not constexpr.

Definition at line 151 of file DataSize.hpp.

Member Function Documentation

◆ bits()

template<std::size_t Bits>
constexpr std::uint64_t duds::general::DataSize< Bits >::bits ( ) const
inline

Returns the size specified in bits.

Note
The maximum size that can be represented as bits is one bit short of 2EB (exabytes).

Definition at line 170 of file DataSize.hpp.

Referenced by duds::general::DataSize< Bits >::operator!=(), duds::general::DataSize< Bits >::operator==(), duds::general::DataSize< Bits >::operator>(), and duds::general::DataSize< Bits >::operator>=().

◆ blocks()

template<std::size_t Bits>
constexpr std::size_t duds::general::DataSize< Bits >::blocks ( ) const
inline

Returns the size specified as a multuple of Bits.

Definition at line 162 of file DataSize.hpp.

Referenced by duds::hardware::interface::linux::SpiMasterSyncSerial::transfer().

◆ blockSize()

template<std::size_t Bits>
static constexpr std::size_t duds::general::DataSize< Bits >::blockSize ( )
inlinestatic

Returns the number of bits in each unit for this type; same as Bits.

Definition at line 115 of file DataSize.hpp.

◆ bytes()

template<std::size_t Bits>
constexpr std::uint64_t duds::general::DataSize< Bits >::bytes ( ) const
inline

Returns the size specified in bytes, or fails if the size cannot be exactly represented as an integer value of bytes.

Note
When used in a constexpr expression, this function will fail to compile if bits() divided by 8 has a reminder. This is intentional.
The maximum size that can be represented as bytes is one byte short of 16EB (exabytes).

Definition at line 241 of file DataSize.hpp.

◆ bytesRounded()

template<std::size_t Bits>
constexpr std::uint64_t duds::general::DataSize< Bits >::bytesRounded ( ) const
inline

Returns the size specified in bytes, rounded up.

Note
The maximum size that can be represented as bytes is one byte short of 16EB (exabytes).

Definition at line 249 of file DataSize.hpp.

◆ operator DataSize< ConvBits >()

template<std::size_t Bits>
template<std::size_t ConvBits>
constexpr duds::general::DataSize< Bits >::operator DataSize< ConvBits > ( ) const
inline

Implicit conversion of DataSize objects to other DataSize classes using a different size multiple.

The conversion will fail if the size cannot be exactly represented.

Note
When used in a constexpr expression, this function will fail to compile if bits() divided by ConvBits has a reminder. This is intentional.
Template Parameters
ConvBitsThe number of bits in each unit of the converted result. This will need to be explicitly specified.
Returns
A new DataSize object with the same size in bits converted to new units.
Exceptions
DataSizeConversionErrorThe size cannot be excactly represented by a DataSize<ConvBits> object. This will only be thrown from expressions that are not constexpr.

Definition at line 213 of file DataSize.hpp.

◆ operator!=()

template<std::size_t Bits>
template<std::size_t OtherBits>
constexpr bool duds::general::DataSize< Bits >::operator!= ( const DataSize< OtherBits > &  op) const
inline

Checks inequality by comparing the number of bits so that any DataSize template types may be compared without a conversion.

Definition at line 265 of file DataSize.hpp.

◆ operator*()

template<std::size_t Bits>
constexpr DataSize duds::general::DataSize< Bits >::operator* ( int  scalar) const
inline

Muliplies the size by a scalar value.

Multiplying two DataSize objects is not directly allowed since the result should be a value in square bits.

Definition at line 344 of file DataSize.hpp.

◆ operator*=()

template<std::size_t Bits>
DataSize& duds::general::DataSize< Bits >::operator*= ( int  scalar)
inline

Multiplies a DataSize object by a scalar and stores the result.

Definition at line 358 of file DataSize.hpp.

◆ operator+()

template<std::size_t Bits>
constexpr DataSize duds::general::DataSize< Bits >::operator+ ( const DataSize< Bits > &  op) const
inline

Adds two sizes.

Differing DataSize templates may be used; this will cause an implicit conversion which will fail if the conversion cannot be exact. The result is that the DataSize template with the smaller uint (Bits) should generally be on the left side of the expression.

Bytes(8) + Kilobytes(8); // works
Kilobits(8) + Bits(8); // fails

Definition at line 310 of file DataSize.hpp.

◆ operator+=()

template<std::size_t Bits>
DataSize& duds::general::DataSize< Bits >::operator+= ( const DataSize< Bits > &  op)
inline

Adds two DataSize objects and stores the result.

Definition at line 329 of file DataSize.hpp.

◆ operator-()

template<std::size_t Bits>
constexpr DataSize duds::general::DataSize< Bits >::operator- ( const DataSize< Bits > &  op) const
inline

Subtracts two sizes.

Differing DataSize templates may be used; this will cause an implicit conversion which will fail if the conversion cannot be exact. The result is that the DataSize template with the smaller uint (Bits) should generally be on the left side of the expression.

Bytes(8) - Kilobytes(8); // works
Kilobits(8) - Bits(8); // fails

Definition at line 323 of file DataSize.hpp.

◆ operator-=()

template<std::size_t Bits>
DataSize& duds::general::DataSize< Bits >::operator-= ( const DataSize< Bits > &  op)
inline

Subtracts two DataSize objects and stores the result.

Definition at line 336 of file DataSize.hpp.

◆ operator/()

template<std::size_t Bits>
constexpr DataSize duds::general::DataSize< Bits >::operator/ ( int  scalar) const
inline

Divides the size by a scalar value.

Dividing two DataSize objects is not directly allowed since the result would be unitless (not bits or anything else).

Definition at line 352 of file DataSize.hpp.

◆ operator/=()

template<std::size_t Bits>
DataSize& duds::general::DataSize< Bits >::operator/= ( int  scalar)
inline

Divides a DataSize object by a scalar and stores the result.

Definition at line 365 of file DataSize.hpp.

◆ operator<()

template<std::size_t Bits>
template<std::size_t OtherBits>
constexpr bool duds::general::DataSize< Bits >::operator< ( const DataSize< OtherBits > &  op) const
inline

Less-than comparison done by comparing the number of bits so that any DataSize template types may be compared without a conversion.

Definition at line 281 of file DataSize.hpp.

◆ operator<=()

template<std::size_t Bits>
template<std::size_t OtherBits>
constexpr bool duds::general::DataSize< Bits >::operator<= ( const DataSize< OtherBits > &  op) const
inline

Less-than-or-equal comparison done by comparing the number of bits so that any DataSize template types may be compared without a conversion.

Definition at line 297 of file DataSize.hpp.

◆ operator==()

template<std::size_t Bits>
template<std::size_t OtherBits>
constexpr bool duds::general::DataSize< Bits >::operator== ( const DataSize< OtherBits > &  op) const
inline

Checks equality by comparing the number of bits so that any DataSize template types may be compared without a conversion.

Definition at line 257 of file DataSize.hpp.

◆ operator>()

template<std::size_t Bits>
template<std::size_t OtherBits>
constexpr bool duds::general::DataSize< Bits >::operator> ( const DataSize< OtherBits > &  op) const
inline

Greater-than comparison done by comparing the number of bits so that any DataSize template types may be compared without a conversion.

Definition at line 273 of file DataSize.hpp.

◆ operator>=()

template<std::size_t Bits>
template<std::size_t OtherBits>
constexpr bool duds::general::DataSize< Bits >::operator>= ( const DataSize< OtherBits > &  op) const
inline

Greater-than-or-equal comparison done by comparing the number of bits so that any DataSize template types may be compared without a conversion.

Definition at line 289 of file DataSize.hpp.

◆ size()

template<std::size_t Bits>
template<class OtherDataSize >
constexpr OtherDataSize duds::general::DataSize< Bits >::size ( ) const
inline

Returns a new DataSize object representing the same size, but with a multiple different from Bits.

Note
When used in a constexpr expression, this function will fail to compile if bits() divided by OtherDataSize has a reminder. This is intentional.
Template Parameters
OtherDataSizeThe number of bits in each unit of the converted result. This will need to be explicitly specified.
Returns
The size converted to new units. It represents the same number of bits.
Exceptions
DataSizeConversionErrorThe size cannot be excactly represented by a DataSize<OtherDataSize> object. This will only be thrown from expressions that are not constexpr.

Definition at line 189 of file DataSize.hpp.

◆ sizeRounded()

template<std::size_t Bits>
template<class OtherDataSize >
constexpr OtherDataSize duds::general::DataSize< Bits >::sizeRounded ( ) const
inline

Returns a new DataSize object representing a size with a multiple different from Bits that is as small as possible while representing at least as many bits as this object.

Template Parameters
OtherDataSizeThe number of bits in each unit of the converted result. This will need to be explicitly specified.
Returns
The size converted to new units. It will be larger if the conversion could not be exact.

Definition at line 226 of file DataSize.hpp.

Member Data Documentation

◆ numblocks

template<std::size_t Bits>
std::size_t duds::general::DataSize< Bits >::numblocks
private

The documentation for this class was generated from the following file: