105 constexpr Size() noexcept: m_value(0) {}
113 template<Type::Integral T>
114 constexpr Size(T value)
noexcept: m_value(0) {
116 assert(value >=
static_cast<T
>(0));
117 m_value =
static_cast<std::uint64_t
>(value);
124 constexpr Size(
const Size& other)
noexcept =
default;
135 constexpr ~Size() noexcept = default;
142 constexpr
Size& operator=(const
Size& other) noexcept = default;
149 constexpr
Size& operator=(
Size&& other) noexcept = default;
158 template<
Type::Integral T>
159 constexpr
Size& operator=(T value) noexcept {
175 constexpr std::uint64_t
Value() const noexcept {
190 constexpr explicit operator std::uint64_t() const noexcept {
204 constexpr explicit operator T()
const noexcept {
205 assert(m_value <=
static_cast<std::uint64_t
>(std::numeric_limits<std::size_t>::max()));
206 return static_cast<std::size_t
>(m_value);
214 constexpr explicit operator std::ptrdiff_t() const noexcept {
215 assert(m_value <=
static_cast<std::uint64_t
>(std::numeric_limits<std::ptrdiff_t>::max()));
216 return static_cast<std::ptrdiff_t
>(m_value);
229 inline operator
std::
string()
const {
230 return static_cast<std::string
>(
static_cast<CString>(*this));
247 return lhs.m_value == rhs.m_value;
257 return lhs.m_value <=> rhs.m_value;
275 assert(lhs.m_value <= std::numeric_limits<std::uint64_t>::max() - rhs.m_value);
276 return Size(
static_cast<std::uint64_t
>(lhs.m_value + rhs.m_value));
287 assert(lhs.m_value >= rhs.m_value);
288 return Size(
static_cast<std::uint64_t
>(lhs.m_value - rhs.m_value));
298 *
this = *
this + other;
309 *
this = *
this - other;
316 std::uint64_t m_value;
334 inline constexpr Unit GiB{1024ull * 1024 * 1024};
335 inline constexpr Unit TiB{1024ull * 1024 * 1024 * 1024};
336 inline constexpr Unit PiB{1024ull * 1024 * 1024 * 1024 * 1024};
337 inline constexpr Unit EiB{1024ull * 1024 * 1024 * 1024 * 1024 * 1024};
345 inline constexpr Unit MB{1000ull * 1000};
346 inline constexpr Unit GB{1000ull * 1000 * 1000};
347 inline constexpr Unit TB{1000ull * 1000 * 1000 * 1000};
348 inline constexpr Unit PB{1000ull * 1000 * 1000 * 1000 * 1000};
349 inline constexpr Unit EB{1000ull * 1000 * 1000 * 1000 * 1000 * 1000};
360 template<Type::Integral T>
363 assert(count >= T{0});
364 const std::uint64_t factor =
static_cast<std::uint64_t
>(count);
365 assert(unit.factor == 0 || factor <= std::numeric_limits<std::uint64_t>::max() / unit.factor);
366 return Size(factor * unit.factor);
376 template<Type::Integral T>
389 template<Type::FloatingPo
int T>
390 constexpr Size
operator*(T count, Unit unit)
noexcept {
391 assert(count >= T{0});
392 const long double product =
static_cast<long double>(count) *
static_cast<long double>(unit.factor);
393 assert(product <=
static_cast<long double>(std::numeric_limits<std::uint64_t>::max()));
394 return Size(
static_cast<std::uint64_t
>(product + 0.5L));
404 template<Type::FloatingPo
int T>
405 constexpr Size
operator*(Unit unit, T count)
noexcept {
417 template<Type::Integral T>
420 assert(count > T{0});
422 assert(count != T{0});
423 const std::uint64_t factor =
static_cast<std::uint64_t
>(count);
424 assert(size.Value() == 0 || factor <= std::numeric_limits<std::uint64_t>::max() / size.Value());
425 return Size(factor * size.Value());
435 template<Type::Integral T>
448 template<Type::Integral T>
451 assert(count > T{0});
453 assert(count != T{0});
454 return size.
Value() /
static_cast<std::uint64_t
>(count);
465 template<Type::Integral T>
468 assert(count > T{0});
470 assert(count != T{0});
471 return size.Value() %
static_cast<std::uint64_t
>(count);
481 return stream << static_cast<std::string>(size);
Owned NUL-terminated buffer, safe to use across a DLL boundary.
Definition cstring.hxx:89
Byte count that is the same width on every host.
Definition size.hxx:95
friend constexpr Size operator-(Size lhs, Size rhs) noexcept
Difference.
Definition size.hxx:286
friend constexpr std::strong_ordering operator<=>(Size lhs, Size rhs) noexcept
Numeric order.
Definition size.hxx:256
constexpr std::uint64_t Value() const noexcept
Stored count.
Definition size.hxx:175
constexpr Size(T value) noexcept
From an integer count.
Definition size.hxx:114
constexpr Size(Size &&other) noexcept=default
Move constructor.
constexpr ~Size() noexcept=default
Destructor.
constexpr Size & operator+=(Size other) noexcept
Adds other to this size.
Definition size.hxx:297
constexpr Size(const Size &other) noexcept=default
Copy constructor.
constexpr Size() noexcept
Zero.
Definition size.hxx:105
constexpr Size & operator-=(Size other) noexcept
Subtracts other from this size.
Definition size.hxx:308
friend constexpr bool operator==(Size lhs, Size rhs) noexcept
Numeric equality.
Definition size.hxx:246
friend constexpr Size operator+(Size lhs, Size rhs) noexcept
Sum.
Definition size.hxx:274
Same type after stripping cv and references from both sides.
Definition relations.hxx:94
Signed arithmetic type (std::is_signed).
Definition categories.hxx:219
Root namespace of the StormByte suite.
constexpr Size operator*(T count, Unit unit) noexcept
Scale unit by an integer count.
Definition size.hxx:361
constexpr Unit EiB
2^60 bytes
Definition size.hxx:337
constexpr Unit B
2^0 bytes
Definition size.hxx:331
constexpr Unit KB
10^3 bytes
Definition size.hxx:344
constexpr Unit GB
10^9 bytes
Definition size.hxx:346
constexpr Unit TiB
2^40 bytes
Definition size.hxx:335
constexpr Unit MB
10^6 bytes
Definition size.hxx:345
constexpr Unit EB
10^18 bytes
Definition size.hxx:349
constexpr Unit PB
10^15 bytes
Definition size.hxx:348
std::ostream & operator<<(std::ostream &stream, const CString &text)
Writes text to stream.
Definition cstring.hxx:304
constexpr Unit KiB
2^10 bytes
Definition size.hxx:332
constexpr Unit PiB
2^50 bytes
Definition size.hxx:336
constexpr Unit TB
10^12 bytes
Definition size.hxx:347
constexpr std::uint64_t operator%(const Size &size, T count) noexcept
Leftover bytes after splitting size into pieces of count bytes.
Definition size.hxx:466
constexpr std::uint64_t operator/(const Size &size, T count) noexcept
How many pieces of count bytes fit in size.
Definition size.hxx:449
constexpr Unit MiB
2^20 bytes
Definition size.hxx:333
constexpr Unit GiB
2^30 bytes
Definition size.hxx:334
Named concepts and small type utilities used across the suite.
Scale factor for Size (KiB, KB, …).
Definition size.hxx:323
std::uint64_t factor
Bytes in one unit.
Definition size.hxx:324
#define STORMBYTE_PUBLIC
Definition visibility.h:52