From cd665ec172dadd049e7c1830b070f15b58fb9011 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 18 Aug 2026 13:51:55 +0200 Subject: kstd: flatten units namespace --- libs/kstd/kstd/units.hpp | 113 +++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 54 deletions(-) (limited to 'libs/kstd') diff --git a/libs/kstd/kstd/units.hpp b/libs/kstd/kstd/units.hpp index cb33af21..e3868aa7 100644 --- a/libs/kstd/kstd/units.hpp +++ b/libs/kstd/kstd/units.hpp @@ -9,71 +9,76 @@ namespace kstd { - namespace units - { - - using bytes = basic_unit; - - // NOLINTNEXTLINE(readability-identifier-naming) - constexpr auto KiB(std::unsigned_integral auto value) noexcept -> bytes - { - return bytes{static_cast(value) * 1024}; - } + using bytes = basic_unit; - // NOLINTNEXTLINE(readability-identifier-naming) - constexpr auto MiB(std::unsigned_integral auto value) noexcept -> bytes - { - return bytes{static_cast(value) * 1024 * 1024}; - } - - // NOLINTNEXTLINE(readability-identifier-naming) - constexpr auto GiB(std::unsigned_integral auto value) noexcept -> bytes - { - return bytes{static_cast(value) * 1024 * 1024 * 1024}; - } - - template - constexpr auto operator+(ValueType * pointer, bytes offset) -> ValueType * - { - return pointer + offset.value; - } - - } // namespace units + // NOLINTNEXTLINE(readability-identifier-naming) + constexpr auto KiB(std::unsigned_integral auto value) noexcept -> bytes + { + return bytes{static_cast(value) * 1024}; + } - namespace units_literals + // NOLINTNEXTLINE(readability-identifier-naming) + constexpr auto MiB(std::unsigned_integral auto value) noexcept -> bytes { - constexpr auto operator""_B(unsigned long long value) noexcept -> units::bytes - { - return units::bytes{value}; - } + return bytes{static_cast(value) * 1024 * 1024}; + } - constexpr auto operator""_KiB(unsigned long long value) noexcept -> units::bytes - { - return units::KiB(value); - } + // NOLINTNEXTLINE(readability-identifier-naming) + constexpr auto GiB(std::unsigned_integral auto value) noexcept -> bytes + { + return bytes{static_cast(value) * 1024 * 1024 * 1024}; + } - constexpr auto operator""_MiB(unsigned long long value) noexcept -> units::bytes - { - return units::MiB(value); - } + template + constexpr auto operator+(ValueType * pointer, bytes offset) -> ValueType * + { + return pointer + offset.value; + } - constexpr auto operator""_GiB(unsigned long long value) noexcept -> units::bytes + inline namespace literals + { + inline namespace units_literals { - return units::GiB(value); - } - - } // namespace units_literals - - template - consteval auto size_of(ValueType const &) noexcept -> units::bytes + constexpr auto operator""_B(unsigned long long value) noexcept -> bytes + { + return bytes{value}; + } + + constexpr auto operator""_KiB(unsigned long long value) noexcept -> bytes + { + return KiB(value); + } + + constexpr auto operator""_MiB(unsigned long long value) noexcept -> bytes + { + return MiB(value); + } + + constexpr auto operator""_GiB(unsigned long long value) noexcept -> bytes + { + return GiB(value); + } + + } // namespace units_literals + } // namespace literals + + //! Calculate the size of a given object. + //! + //! @param object The object to calculate the size for. + //! @return the size, in bytes, of the given object. + consteval auto size_of(auto const & object) noexcept -> bytes { - return units::bytes{sizeof(ValueType)}; + return bytes{sizeof(object)}; } - template - consteval auto size_of() noexcept -> units::bytes + //! Calculate the size of a given type. + //! + //! @tparam Type The type to calculate the size for. + //! @return the size, in bytes, of the given type. + template + consteval auto size_of() noexcept -> bytes { - return units::bytes{sizeof(ValueType)}; + return bytes{sizeof(Type)}; } } // namespace kstd -- cgit v1.2.3