StormByte 2.0.0
C++26 foundation of the StormByte suite
 
Loading...
Searching...
No Matches
StormByte::Size Class Reference

Byte count that is the same width on every host. More...

#include <StormByte/size.hxx>

Public Member Functions

Life
constexpr Size () noexcept
 Zero.
 
template<Type::Integral T>
constexpr Size (T value) noexcept
 From an integer count.
 
constexpr Size (const Size &other) noexcept=default
 Copy constructor.
 
constexpr Size (Size &&other) noexcept=default
 Move constructor.
 
constexpr ~Size () noexcept=default
 Destructor.
 
constexpr Size & operator= (const Size &other) noexcept=default
 Copy assignment.
 
constexpr Size & operator= (Size &&other) noexcept=default
 Move assignment.
 
template<Type::Integral T>
constexpr Size & operator= (T value) noexcept
 Assign an integer count.
 
Observers
constexpr std::uint64_t Value () const noexcept
 Stored count.
 
Conversions
constexpr operator std::uint64_t () const noexcept
 Stored count.
 
template<typename T >
requires Type::SameAs<T, std::size_t> && (!Type::SameAs<std::size_t, std::uint64_t>)
constexpr operator T () const noexcept
 Stored count as host size_t.
 
constexpr operator std::ptrdiff_t () const noexcept
 Stored count as ptrdiff_t (non-negative offsets).
 
 operator CString () const noexcept
 IEC text owned by StormByte.
 
 operator std::string () const
 IEC text in the caller’s heap.
 

Friends

Comparison
constexpr bool operator== (Size lhs, Size rhs) noexcept
 Numeric equality.
 
constexpr std::strong_ordering operator<=> (Size lhs, Size rhs) noexcept
 Numeric order.
 

Arithmetic

constexpr Size & operator+= (Size other) noexcept
 Adds other to this size.
 
constexpr Size & operator-= (Size other) noexcept
 Subtracts other from this size.
 
constexpr Size operator+ (Size lhs, Size rhs) noexcept
 Sum.
 
constexpr Size operator- (Size lhs, Size rhs) noexcept
 Difference.
 

Detailed Description

Byte count that is the same width on every host.

Stored as std::uint64_t. Safe to return across a DLL boundary and between 32-bit and 64-bit modules. Not a replacement for std::size_t inside a single translation unit.

Construction from a non-negative integer is implicit and constexpr: Size s = 0, s = 16, { Status::Ok, 0 }, constexpr Size MinWindow{16ull * 1024ull}. A negative integer is undefined and asserts when assertions are on.

Mixed comparisons and addition/subtraction with integers work on both sides (5 == s, s == 5, 16 + s, s + 16) via hidden friends. There is no implicit conversion to an integer. Value(), explicit operator uint64_t, explicit operator size_t (only when size_t is not uint64_t) and explicit operator ptrdiff_t are the numeric exits. A value that does not fit the target type is undefined and asserts when assertions are on.

Addition, subtraction and unit scale (4 * MiB, s * 4, s / 4, s % 4) are constexpr. Overflow, a negative integer or a zero divisor is undefined and asserts when assertions are on. There is no Size * Size and no floating-point scale of a Size.

Units are IEC (KiB, MiB, …) and SI (KB, MB, …). A fractional factor rounds to the nearest byte.

operator CString and operator std::string are the IEC human-readable form (B, KiB, …). CString is built in the StormByte DLL. std::string is built in the caller’s heap from that CString.

Constructor & Destructor Documentation

◆ Size() [1/4]

constexpr StormByte::Size::Size ( )
inlineconstexprnoexcept

Zero.

◆ Size() [2/4]

template<Type::Integral T>
constexpr StormByte::Size::Size ( T  value)
inlineconstexprnoexcept

From an integer count.

Template Parameters
TIntegral type.
Parameters
valueByte count.
Note
A negative value is undefined. Checked with assert when assertions are on.

◆ Size() [3/4]

constexpr StormByte::Size::Size ( const Size &  other)
constexprdefaultnoexcept

Copy constructor.

Parameters
otherSize to copy.

◆ Size() [4/4]

constexpr StormByte::Size::Size ( Size &&  other)
constexprdefaultnoexcept

Move constructor.

Parameters
otherSize to take.

◆ ~Size()

constexpr StormByte::Size::~Size ( )
constexprdefaultnoexcept

Destructor.

Member Function Documentation

◆ operator CString()

StormByte::Size::operator CString ( ) const
explicitnoexcept

IEC text owned by StormByte.

Returns
Human-readable size (B, KiB, MiB, GiB, TiB, PiB, EiB).

◆ operator std::ptrdiff_t()

constexpr StormByte::Size::operator std::ptrdiff_t ( ) const
inlineexplicitconstexprnoexcept

Stored count as ptrdiff_t (non-negative offsets).

Returns
Value as std::ptrdiff_t.
Note
A count above ptrdiff_t is undefined. Checked with assert when assertions are on.

◆ operator std::string()

StormByte::Size::operator std::string ( ) const
inline

IEC text in the caller’s heap.

Returns
Human-readable size.

◆ operator std::uint64_t()

constexpr StormByte::Size::operator std::uint64_t ( ) const
inlineexplicitconstexprnoexcept

Stored count.

Returns
Value as uint64_t.

◆ operator T()

template<typename T >
requires Type::SameAs<T, std::size_t> && (!Type::SameAs<std::size_t, std::uint64_t>)
constexpr StormByte::Size::operator T ( ) const
inlineexplicitconstexprnoexcept

Stored count as host size_t.

Template Parameters
TMust be std::size_t and a different type from uint64_t.
Returns
Value as std::size_t.
Note
Ill-formed when size_t and uint64_t are the same type; static_cast<std::size_t> then uses operator uint64_t. A count above size_t is undefined. Checked with assert when assertions are on.

◆ operator+=()

constexpr Size & StormByte::Size::operator+= ( Size  other)
inlineconstexprnoexcept

Adds other to this size.

Parameters
otherAddend.
Returns
*this.
Note
Overflow of uint64_t is undefined. Checked with assert when assertions are on.

◆ operator-=()

constexpr Size & StormByte::Size::operator-= ( Size  other)
inlineconstexprnoexcept

Subtracts other from this size.

Parameters
otherSubtrahend.
Returns
*this.
Note
*this < other is undefined. Checked with assert when assertions are on.

◆ operator=() [1/3]

constexpr Size & StormByte::Size::operator= ( const Size &  other)
constexprdefaultnoexcept

Copy assignment.

Parameters
otherSize to copy.
Returns
*this.

◆ operator=() [2/3]

constexpr Size & StormByte::Size::operator= ( Size &&  other)
constexprdefaultnoexcept

Move assignment.

Parameters
otherSize to take.
Returns
*this.

◆ operator=() [3/3]

template<Type::Integral T>
constexpr Size & StormByte::Size::operator= ( T  value)
inlineconstexprnoexcept

Assign an integer count.

Template Parameters
TIntegral type.
Parameters
valueByte count.
Returns
*this.
Note
A negative value is undefined. Checked with assert when assertions are on.

◆ Value()

constexpr std::uint64_t StormByte::Size::Value ( ) const
inlineconstexprnoexcept

Stored count.

Returns
Value as uint64_t.

Friends And Related Symbol Documentation

◆ operator+

constexpr Size operator+ ( Size  lhs,
Size  rhs 
)
friend

Sum.

Parameters
lhsLeft addend.
rhsRight addend.
Returns
Sum.
Note
Overflow of uint64_t is undefined. Checked with assert when assertions are on.

◆ operator-

constexpr Size operator- ( Size  lhs,
Size  rhs 
)
friend

Difference.

Parameters
lhsMinuend.
rhsSubtrahend.
Returns
Difference.
Note
lhs < rhs is undefined. Checked with assert when assertions are on.

◆ operator<=>

constexpr std::strong_ordering operator<=> ( Size  lhs,
Size  rhs 
)
friend

Numeric order.

Parameters
lhsLeft count.
rhsRight count.
Returns
Ordering.

◆ operator==

constexpr bool operator== ( Size  lhs,
Size  rhs 
)
friend

Numeric equality.

Parameters
lhsLeft count.
rhsRight count.
Returns
Whether the counts are equal.

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