11#include <pixie/bit_stream.h>
30 std::size_t
size_bytes()
const {
return impl().size_bytes_impl(); }
39 std::span<const std::byte>
as_bytes()
const {
return impl().as_bytes_impl(); }
47 return as_words<std::uint16_t>();
56 return as_words<std::uint64_t>();
68 auto view(std::size_t offset_bytes, std::size_t count_bytes)
const {
69 return impl().view_impl(offset_bytes, count_bytes);
75 for (
const std::byte
byte :
as_bytes()) {
76 stream << static_cast<std::uint8_t>(
byte);
82 requires requires(Impl& value) { value.resize_impl(
size_bits); }
89 requires requires(Impl& value) { value.writable_bytes_impl(); }
91 return impl().writable_bytes_impl();
96 requires requires(Impl& value) { value.writable_words16_impl(); }
98 return impl().writable_words16_impl();
103 requires requires(Impl& value) { value.writable_words64_impl(); }
105 return impl().writable_words64_impl();
110 requires requires(const Impl& value) { value.allocated_bytes_impl(); }
112 return impl().allocated_bytes_impl();
117 requires requires(Impl& value) { value.shrink_to_fit_impl(); }
119 impl().shrink_to_fit_impl();
124 const Impl& impl()
const {
return static_cast<const Impl&
>(*this); }
127 Impl& impl() {
return static_cast<Impl&
>(*this); }
129 template <
class Word>
130 std::span<const Word> as_words()
const {
132 if (bytes.size() %
sizeof(Word) != 0 ||
133 reinterpret_cast<std::uintptr_t
>(bytes.data()) %
alignof(Word) != 0) {
134 throw std::invalid_argument(
"Storage is not aligned to the word type");
136 return {
reinterpret_cast<const Word*
>(bytes.data()),
137 bytes.size() /
sizeof(Word)};
142template <
class Storage>
144 std::derived_from<Storage, StorageBase<Storage>>;
Definition bit_stream.h:9
CRTP facade for byte-addressable storage.
Definition storage.h:27
auto view(std::size_t offset_bytes, std::size_t count_bytes) const
Return a non-owning read-only byte subrange.
Definition storage.h:68
auto writable_words64()
Return writable storage as 64-bit words.
Definition storage.h:102
void shrink_to_fit()
Request release of unused reserved storage.
Definition storage.h:116
std::span< const std::uint64_t > as_words64() const
Return a read-only view as 64-bit words.
Definition storage.h:55
std::span< const std::byte > as_bytes() const
Return a read-only view of all exposed bytes.
Definition storage.h:39
std::size_t size_bits() const
Return the exposed storage size in bits.
Definition storage.h:33
auto writable_words16()
Return writable storage as 16-bit words.
Definition storage.h:95
std::size_t size_bytes() const
Return the exposed storage size in bytes.
Definition storage.h:30
auto view() const
Return a non-owning read-only view of all exposed bytes.
Definition storage.h:60
auto writable_bytes()
Return writable storage bytes.
Definition storage.h:88
std::size_t allocated_bytes() const
Return bytes reserved by an owning storage implementation.
Definition storage.h:109
std::span< const std::uint16_t > as_words16() const
Return a read-only view as 16-bit words.
Definition storage.h:46
void resize(std::size_t size_bits)
Resize mutable storage to hold at least size_bits bits.
Definition storage.h:81
void serialize(OutputBitStream &stream) const
Serialize the exposed byte sequence with a size prefix.
Definition storage.h:73
bool empty() const
Check whether the storage is empty.
Definition storage.h:36
A concrete CRTP implementation of StorageBase.
Definition storage.h:143