Pixie
Loading...
Searching...
No Matches
storage.h
Go to the documentation of this file.
1#pragma once
2
10
11#include <pixie/bit_stream.h>
12
13#include <concepts>
14#include <cstddef>
15#include <cstdint>
16#include <span>
17#include <stdexcept>
18
19namespace pixie {
20
26template <class Impl>
28 public:
30 std::size_t size_bytes() const { return impl().size_bytes_impl(); }
31
33 std::size_t size_bits() const { return size_bytes() * 8; }
34
36 bool empty() const { return size_bytes() == 0; }
37
39 std::span<const std::byte> as_bytes() const { return impl().as_bytes_impl(); }
40
46 std::span<const std::uint16_t> as_words16() const {
47 return as_words<std::uint16_t>();
48 }
49
55 std::span<const std::uint64_t> as_words64() const {
56 return as_words<std::uint64_t>();
57 }
58
60 auto view() const { return impl().view_impl(0, size_bytes()); }
61
68 auto view(std::size_t offset_bytes, std::size_t count_bytes) const {
69 return impl().view_impl(offset_bytes, count_bytes);
70 }
71
73 void serialize(OutputBitStream& stream) const {
74 stream << size_bytes();
75 for (const std::byte byte : as_bytes()) {
76 stream << static_cast<std::uint8_t>(byte);
77 }
78 }
79
81 void resize(std::size_t size_bits)
82 requires requires(Impl& value) { value.resize_impl(size_bits); }
83 {
84 impl().resize_impl(size_bits);
85 }
86
89 requires requires(Impl& value) { value.writable_bytes_impl(); }
90 {
91 return impl().writable_bytes_impl();
92 }
93
96 requires requires(Impl& value) { value.writable_words16_impl(); }
97 {
98 return impl().writable_words16_impl();
99 }
100
103 requires requires(Impl& value) { value.writable_words64_impl(); }
104 {
105 return impl().writable_words64_impl();
106 }
107
109 std::size_t allocated_bytes() const
110 requires requires(const Impl& value) { value.allocated_bytes_impl(); }
111 {
112 return impl().allocated_bytes_impl();
113 }
114
117 requires requires(Impl& value) { value.shrink_to_fit_impl(); }
118 {
119 impl().shrink_to_fit_impl();
120 }
121
122 private:
124 const Impl& impl() const { return static_cast<const Impl&>(*this); }
125
127 Impl& impl() { return static_cast<Impl&>(*this); }
128
129 template <class Word>
130 std::span<const Word> as_words() const {
131 const auto bytes = as_bytes();
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");
135 }
136 return {reinterpret_cast<const Word*>(bytes.data()),
137 bytes.size() / sizeof(Word)};
138 }
139};
140
142template <class Storage>
144 std::derived_from<Storage, StorageBase<Storage>>;
145
146} // namespace pixie
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