From f3fc901290c65ec8a4c025c30a316cb2ac5dd53d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 18 Aug 2026 13:32:36 +0200 Subject: kstd: clean up units --- libs/acpi/acpi/common/table_header.tests.cpp | 2 +- libs/kstd/kstd/bits/basic_unit.hpp | 117 +++++++++++++++++++++++++++ libs/kstd/kstd/units.hpp | 110 +++++-------------------- 3 files changed, 136 insertions(+), 93 deletions(-) create mode 100644 libs/kstd/kstd/bits/basic_unit.hpp (limited to 'libs') diff --git a/libs/acpi/acpi/common/table_header.tests.cpp b/libs/acpi/acpi/common/table_header.tests.cpp index bbd42bd6..971c715c 100644 --- a/libs/acpi/acpi/common/table_header.tests.cpp +++ b/libs/acpi/acpi/common/table_header.tests.cpp @@ -28,7 +28,7 @@ SCENARIO("Common table header parsing", "[common_table_header]") THEN("the length is correct") { - REQUIRE(header->length() == kstd::type_size); + REQUIRE(header->length() == kstd::size_of()); } THEN("the oem id is correct") diff --git a/libs/kstd/kstd/bits/basic_unit.hpp b/libs/kstd/kstd/bits/basic_unit.hpp new file mode 100644 index 00000000..c7f794ea --- /dev/null +++ b/libs/kstd/kstd/bits/basic_unit.hpp @@ -0,0 +1,117 @@ +#ifndef KSTD_BITS_BASIC_UNITS_HPP +#define KSTD_BITS_BASIC_UNITS_HPP + +#include +#include + +namespace kstd +{ + + //! A basic template for strongly typed units. + template + struct basic_unit + { + using value_type = ValueType; + + constexpr basic_unit() noexcept + : value{} + {} + + explicit constexpr basic_unit(value_type value) noexcept + : value{value} + {} + + template + explicit constexpr operator T() const noexcept + { + return value; + } + + constexpr auto friend operator+=(basic_unit & lhs, basic_unit const & rhs) noexcept -> basic_unit & + { + lhs.value += rhs.value; + return lhs; + } + + constexpr auto friend operator+(basic_unit lhs, basic_unit const & rhs) noexcept -> basic_unit + { + return lhs += rhs; + } + + constexpr auto friend operator-=(basic_unit & lhs, basic_unit const & rhs) noexcept -> basic_unit & + { + lhs.value -= rhs.value; + return lhs; + } + + constexpr auto friend operator-(basic_unit lhs, basic_unit const & rhs) noexcept -> basic_unit + { + return lhs -= rhs; + } + + constexpr auto friend operator*=(basic_unit & lhs, std::integral auto factor) noexcept -> basic_unit & + { + lhs.value *= factor; + return lhs; + } + + constexpr auto friend operator*(basic_unit lhs, std::integral auto factor) noexcept -> basic_unit + { + return lhs *= factor; + } + + constexpr auto friend operator*(std::integral auto factor, basic_unit rhs) noexcept -> basic_unit + { + return rhs *= factor; + } + + constexpr auto friend operator/=(basic_unit & lhs, std::integral auto divisor) noexcept -> basic_unit & + { + lhs.value /= divisor; + return lhs; + } + + constexpr auto friend operator/(basic_unit lhs, std::integral auto divisor) noexcept -> basic_unit + { + return lhs /= divisor; + } + + constexpr auto friend operator%=(basic_unit & lhs, std::integral auto divisor) noexcept -> basic_unit & + { + lhs.value %= divisor; + return lhs; + } + + constexpr auto friend operator%(basic_unit lhs, std::integral auto divisor) noexcept -> basic_unit + { + return lhs %= divisor; + } + + constexpr auto friend operator/(basic_unit const & lhs, basic_unit const & rhs) noexcept + -> decltype(std::declval() / std::declval()) + { + return lhs.value / rhs.value; + } + + constexpr auto friend operator%(basic_unit const & lhs, basic_unit const & rhs) noexcept -> basic_unit + { + return basic_unit{lhs.value % rhs.value}; + } + + constexpr auto friend operator-(basic_unit const & unit) noexcept -> basic_unit + requires(std::is_signed_v) + { + return basic_unit{-unit.value}; + } + + constexpr auto friend operator<=>(basic_unit const &, basic_unit const &) noexcept + -> decltype(std::declval() <=> std::declval()) = default; + + constexpr auto friend operator==(basic_unit const &, basic_unit const &) noexcept -> bool = default; + + value_type value; + }; + +} // namespace kstd + +#endif diff --git a/libs/kstd/kstd/units.hpp b/libs/kstd/kstd/units.hpp index 6e9ea1e2..cb33af21 100644 --- a/libs/kstd/kstd/units.hpp +++ b/libs/kstd/kstd/units.hpp @@ -1,112 +1,35 @@ #ifndef KSTD_UNITS_HPP #define KSTD_UNITS_HPP -#include +#include + #include #include namespace kstd { - //! A basic template for strongly typed units. - template - struct basic_unit - { - using value_type = ValueType; - - constexpr basic_unit() noexcept - : value{} - {} - - explicit constexpr basic_unit(value_type value) noexcept - : value{value} - {} - - template - explicit constexpr operator T() const noexcept - { - return value; - } - - constexpr auto operator+(basic_unit const & other) const noexcept -> basic_unit - { - return basic_unit{value + other.value}; - } - - constexpr auto operator+=(basic_unit const & other) noexcept -> basic_unit & - { - return *this = *this + other; - } - - constexpr auto operator-(basic_unit const & other) const noexcept -> basic_unit - { - return basic_unit{value - other.value}; - } - - constexpr auto operator-=(basic_unit const & other) noexcept -> basic_unit & - { - return *this = *this - other; - } - - constexpr auto operator*(std::integral auto factor) const noexcept -> basic_unit - { - return basic_unit{value * factor}; - } - - constexpr auto operator*=(std::integral auto factor) noexcept -> basic_unit - { - return *this = *this * factor; - } - - constexpr auto operator/(std::integral auto divisor) const noexcept -> basic_unit - { - return basic_unit{value / divisor}; - } - - constexpr auto operator/=(std::integral auto divisor) noexcept -> basic_unit - { - return *this = *this / divisor; - } - - constexpr auto operator/(basic_unit const & other) const noexcept - { - return value / other.value; - } - - constexpr auto operator%(basic_unit const & other) const noexcept -> basic_unit - { - return basic_unit{value % other.value}; - } - - constexpr auto operator<=>(basic_unit const & other) const noexcept -> std::strong_ordering = default; - - value_type value; - }; - - template - constexpr auto operator*(Factor factor, basic_unit const & unit) noexcept - -> basic_unit - { - return basic_unit{unit.value * factor}; - } - namespace units { + using bytes = basic_unit; - constexpr auto KiB(std::size_t value) noexcept -> bytes + // NOLINTNEXTLINE(readability-identifier-naming) + constexpr auto KiB(std::unsigned_integral auto value) noexcept -> bytes { - return bytes{value * 1024}; + return bytes{static_cast(value) * 1024}; } - constexpr auto MiB(std::size_t value) noexcept -> bytes + // NOLINTNEXTLINE(readability-identifier-naming) + constexpr auto MiB(std::unsigned_integral auto value) noexcept -> bytes { - return bytes{value * 1024 * 1024}; + return bytes{static_cast(value) * 1024 * 1024}; } - constexpr auto GiB(std::size_t value) noexcept -> bytes + // NOLINTNEXTLINE(readability-identifier-naming) + constexpr auto GiB(std::unsigned_integral auto value) noexcept -> bytes { - return bytes{value * 1024 * 1024 * 1024}; + return bytes{static_cast(value) * 1024 * 1024 * 1024}; } template @@ -142,13 +65,16 @@ namespace kstd } // namespace units_literals template - constexpr auto object_size(ValueType const &) -> units::bytes + consteval auto size_of(ValueType const &) noexcept -> units::bytes { return units::bytes{sizeof(ValueType)}; } - template - constexpr auto type_size = units::bytes{sizeof(T)}; + template + consteval auto size_of() noexcept -> units::bytes + { + return units::bytes{sizeof(ValueType)}; + } } // namespace kstd -- cgit v1.2.3