aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-08-18 13:51:55 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-08-18 13:51:55 +0200
commitcd665ec172dadd049e7c1830b070f15b58fb9011 (patch)
tree42c2eb8ecb984d179263f68cab1c6c2667e8dd2c /libs/kstd
parent5754a8591252da5d5eeb4281064f0b82bfdc03a8 (diff)
downloadkernel-cd665ec172dadd049e7c1830b070f15b58fb9011.tar.xz
kernel-cd665ec172dadd049e7c1830b070f15b58fb9011.zip
kstd: flatten units namespace
Diffstat (limited to 'libs/kstd')
-rw-r--r--libs/kstd/kstd/units.hpp113
1 files changed, 59 insertions, 54 deletions
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<std::size_t, struct bytes_tag>;
-
- // NOLINTNEXTLINE(readability-identifier-naming)
- constexpr auto KiB(std::unsigned_integral auto value) noexcept -> bytes
- {
- return bytes{static_cast<bytes::value_type>(value) * 1024};
- }
+ using bytes = basic_unit<std::size_t, struct bytes_tag>;
- // NOLINTNEXTLINE(readability-identifier-naming)
- constexpr auto MiB(std::unsigned_integral auto value) noexcept -> bytes
- {
- return bytes{static_cast<bytes::value_type>(value) * 1024 * 1024};
- }
-
- // NOLINTNEXTLINE(readability-identifier-naming)
- constexpr auto GiB(std::unsigned_integral auto value) noexcept -> bytes
- {
- return bytes{static_cast<bytes::value_type>(value) * 1024 * 1024 * 1024};
- }
-
- template<typename ValueType>
- 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<bytes::value_type>(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<bytes::value_type>(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<bytes::value_type>(value) * 1024 * 1024 * 1024};
+ }
- constexpr auto operator""_MiB(unsigned long long value) noexcept -> units::bytes
- {
- return units::MiB(value);
- }
+ template<typename ValueType>
+ 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<typename ValueType>
- 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<typename ValueType>
- 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<typename Type>
+ consteval auto size_of() noexcept -> bytes
{
- return units::bytes{sizeof(ValueType)};
+ return bytes{sizeof(Type)};
}
} // namespace kstd