diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-08-24 16:57:24 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-08-26 11:16:17 +0200 |
| commit | 2a09bbc8f813d28d69768ba9375b637e2ba8bd8d (patch) | |
| tree | 867d6ec61d5d7ff30d03a90d124948c4d9bcccc6 /libs/kstd | |
| parent | 98fff0dc733050d8bbdfba5235de918ba15fee0e (diff) | |
| download | kernel-2a09bbc8f813d28d69768ba9375b637e2ba8bd8d.tar.xz kernel-2a09bbc8f813d28d69768ba9375b637e2ba8bd8d.zip | |
kernel/fs: ext2: cosmetic cleanup
Diffstat (limited to 'libs/kstd')
| -rw-r--r-- | libs/kstd/kstd/span.hpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/libs/kstd/kstd/span.hpp b/libs/kstd/kstd/span.hpp new file mode 100644 index 00000000..6be2eea2 --- /dev/null +++ b/libs/kstd/kstd/span.hpp @@ -0,0 +1,57 @@ +#ifndef KSTD_SPAN_HPP +#define KSTD_SPAN_HPP + +#include <cstddef> +#include <ranges> +#include <span> // IWYU pragma: export + +namespace kstd +{ + + //! Get a read-only span of bytes, that maps to a given object + //! + //! @param object The object to obtain the byte span for. + //! @return A span over the raw bytes of the object representation of the given object. + template<typename ObjectType> + requires(!std::ranges::range<ObjectType>) + constexpr auto raw_bytes(ObjectType const & object) -> std::span<std::byte const, sizeof(ObjectType)> + { + return std::as_bytes(std::span<ObjectType const, 1>{std::addressof(object), 1}); + } + + //! Get a read-write span of bytes, that maps to a given object + //! + //! @param object The object to obtain the byte span for. + //! @return A span over the raw bytes of the object representation of the given object. + template<typename ObjectType> + requires(!std::ranges::range<ObjectType>) + constexpr auto raw_bytes(ObjectType & object) -> std::span<std::byte, sizeof(ObjectType)> + { + return std::as_writable_bytes(std::span<ObjectType, 1>{std::addressof(object), 1}); + } + + //! Get a read-only span of bytes, that maps to a given range object + //! + //! @param range The range of objects to obtain the byte span for. + //! @return A span over the raw bytes of the object representation of the given object. + template<typename ObjectType> + requires(std::ranges::range<ObjectType>) + constexpr auto raw_bytes(ObjectType const & range) -> std::span<std::byte const> + { + return std::as_bytes(std::span{range}); + } + + //! Get a read-write span of bytes, that maps to a given range object + //! + //! @param range The range of objects to obtain the byte span for. + //! @return A span over the raw bytes of the object representation of the given object. + template<typename ObjectType> + requires(std::ranges::range<ObjectType>) + constexpr auto raw_bytes(ObjectType & range) -> std::span<std::byte> + { + return std::as_writable_bytes(std::span{range}); + } + +} // namespace kstd + +#endif
\ No newline at end of file |
