diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-08-18 13:32:36 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-08-18 13:32:45 +0200 |
| commit | f3fc901290c65ec8a4c025c30a316cb2ac5dd53d (patch) | |
| tree | 75a3ef0086b088280612f4b36692ebaf9cc01a98 /libs/kstd | |
| parent | 493f50c697b17049b820e65664af9f4a7c9c0c2c (diff) | |
| download | kernel-f3fc901290c65ec8a4c025c30a316cb2ac5dd53d.tar.xz kernel-f3fc901290c65ec8a4c025c30a316cb2ac5dd53d.zip | |
kstd: clean up units
Diffstat (limited to 'libs/kstd')
| -rw-r--r-- | libs/kstd/kstd/bits/basic_unit.hpp | 117 | ||||
| -rw-r--r-- | libs/kstd/kstd/units.hpp | 110 |
2 files changed, 135 insertions, 92 deletions
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 <concepts> +#include <utility> + +namespace kstd +{ + + //! A basic template for strongly typed units. + template<typename ValueType, typename Tags> + struct basic_unit + { + using value_type = ValueType; + + constexpr basic_unit() noexcept + : value{} + {} + + explicit constexpr basic_unit(value_type value) noexcept + : value{value} + {} + + template<std::integral T> + 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<value_type>() / std::declval<value_type>()) + { + 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<value_type>) + { + return basic_unit{-unit.value}; + } + + constexpr auto friend operator<=>(basic_unit const &, basic_unit const &) noexcept + -> decltype(std::declval<value_type const &>() <=> std::declval<value_type const &>()) = 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 <compare> +#include <kstd/bits/basic_unit.hpp> + #include <concepts> #include <cstddef> namespace kstd { - //! A basic template for strongly typed units. - template<typename ValueType, typename Tag> - struct basic_unit - { - using value_type = ValueType; - - constexpr basic_unit() noexcept - : value{} - {} - - explicit constexpr basic_unit(value_type value) noexcept - : value{value} - {} - - template<std::integral T> - 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<std::integral Factor, typename ValueType, typename Tag> - constexpr auto operator*(Factor factor, basic_unit<ValueType, Tag> const & unit) noexcept - -> basic_unit<ValueType, Tag> - { - return basic_unit<ValueType, Tag>{unit.value * factor}; - } - namespace units { + using bytes = basic_unit<std::size_t, struct bytes_tag>; - 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<bytes::value_type>(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<bytes::value_type>(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<bytes::value_type>(value) * 1024 * 1024 * 1024}; } template<typename ValueType> @@ -142,13 +65,16 @@ namespace kstd } // namespace units_literals template<typename ValueType> - constexpr auto object_size(ValueType const &) -> units::bytes + consteval auto size_of(ValueType const &) noexcept -> units::bytes { return units::bytes{sizeof(ValueType)}; } - template<typename T> - constexpr auto type_size = units::bytes{sizeof(T)}; + template<typename ValueType> + consteval auto size_of() noexcept -> units::bytes + { + return units::bytes{sizeof(ValueType)}; + } } // namespace kstd |
