diff options
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 |
