Iros
 
Loading...
Searching...
No Matches
byte_buffer.h
Go to the documentation of this file.
1#pragma once
2
9#include "di/meta/core.h"
10#include "di/meta/operations.h"
11#include "di/meta/vocab.h"
12#include "di/platform/prelude.h"
13#include "di/types/in_place.h"
14#include "di/util/exchange.h"
19
23
24 template<typename T>
26 requires(T& self) {
27 { self.span() } -> concepts::ConvertibleTo<Span<byte>>;
28 })
29 constexpr static auto operator()(T& self) -> Span<byte> {
32 "as_writable_byte_span() customizations must return a Span<byte>");
33 return tag_invoke(AsWritableByteSpan {}, self);
34 } else {
35 return self.span();
36 }
37 }
38};
39
40struct AsByteSpan {
42
43 template<typename T>
45 requires(T const& self) {
47 })
48 constexpr static auto operator()(T const& self) -> Span<byte const> {
51 "as_byte_span() customizations must return a Span<byte const>");
52 return tag_invoke(AsByteSpan {}, self);
53 } else {
54 return self.span();
55 }
56 }
57};
58}
59
60namespace di {
63}
64
65namespace di::vocab::byte_buffer {
66template<concepts::Allocator Alloc = platform::DefaultAllocator>
68
69template<concepts::Allocator Alloc = platform::DefaultAllocator>
70class ByteBufferImpl;
71
72template<concepts::Allocator Alloc = platform::DefaultAllocator>
74
75template<concepts::Allocator Alloc>
77public:
80
81 template<concepts::Impl<meta::List<AsByteSpan>> T>
84 auto data = as_byte_span(value);
85 auto store = DI_TRY(Store::create(di::forward<T>(value)));
86 return ByteBufferImpl(data, di::move(store));
87 }
88
89 ByteBufferImpl() = default;
90
91 template<concepts::Impl<meta::List<AsByteSpan>> T>
93 constexpr explicit ByteBufferImpl(T&& value) : m_data(as_byte_span(value)), m_store(di::forward<T>(value)) {}
94
95 constexpr explicit ByteBufferImpl(Span<byte const> data, Store&& store) : m_data(data), m_store(di::move(store)) {}
96 constexpr explicit ByteBufferImpl(Span<byte const> data, Store const& store) : m_data(data), m_store(store) {}
97
98 constexpr auto data() const -> byte const* { return m_data.data(); }
99 constexpr auto size() const -> usize { return m_data.size(); }
100 constexpr auto span() const -> Span<byte const> { return m_data; }
101 constexpr auto empty() const -> bool { return m_data.empty(); }
102
103 constexpr auto store() const& -> Store const& { return m_store; }
104
105 constexpr auto first(usize count) const -> Optional<ByteBufferImpl> {
106 return span().first(count) % [&](Span<byte const> data) {
107 return ByteBufferImpl(data, m_store);
108 };
109 }
110
111 constexpr auto last(usize count) const -> Optional<ByteBufferImpl> {
112 return span().last(count) % [&](Span<byte const> data) {
113 return ByteBufferImpl(data, m_store);
114 };
115 }
116
117 constexpr auto slice(usize offset) const -> Optional<ByteBufferImpl> {
118 return span().subspan(offset) % [&](Span<byte const> data) {
119 return ByteBufferImpl(data, m_store);
120 };
121 }
122
123 constexpr auto slice(usize offset, usize count) const -> Optional<ByteBufferImpl> {
124 return span().subspan(offset, count) % [&](Span<byte const> data) {
125 return ByteBufferImpl(data, m_store);
126 };
127 }
128
129private:
130 Span<byte const> m_data;
131 Store m_store;
132};
133
134template<concepts::Allocator Alloc>
136public:
139
140 template<concepts::Impl<meta::List<AsWritableByteSpan>> T>
142 constexpr static auto create(T&& value)
144 auto data = as_writable_byte_span(value);
145 auto store = DI_TRY(Store::create(di::forward<T>(value)));
146 return ExclusiveByteBufferImpl(data, di::move(store));
147 }
148
150
151 template<concepts::Impl<meta::List<AsWritableByteSpan>> T>
153 constexpr explicit ExclusiveByteBufferImpl(T&& value)
154 : m_data(as_writable_byte_span(value)), m_store(di::forward<T>(value)) {}
155
157 : m_data(data), m_store(di::move(store)) {}
158
160 constexpr ExclusiveByteBufferImpl(ExclusiveByteBufferImpl&& other) : m_store(di::move(other.m_store)) {
161 m_data = di::exchange(other.m_data, Span<byte> {});
162 }
163
164 constexpr auto operator=(ExclusiveByteBufferImpl const&) -> ExclusiveByteBufferImpl& = delete;
166 m_store = di::move(other.m_store);
167 m_data = di::exchange(other.m_data, Span<byte> {});
168 }
169
170 constexpr auto data() const -> byte* { return m_data.data(); }
171 constexpr auto size() const -> usize { return m_data.size(); }
172 constexpr auto span() const -> Span<byte> { return m_data; }
173 constexpr auto empty() const -> bool { return m_data.empty(); }
174
175 constexpr auto store() && -> Store&& {
176 m_data = {};
177 return di::move(*this).m_store;
178 }
179
180 constexpr auto shrink_to_first(usize count) { m_data = m_data.first(count).value_or(Span<byte> {}); }
181 constexpr auto shrink_to_last(usize count) { m_data = m_data.last(count).value_or(Span<byte> {}); }
182 constexpr auto shrink_to_slice(usize offset) { m_data = m_data.subspan(offset).value_or(Span<byte> {}); }
183 constexpr auto shrink_to_slice(usize offset, usize count) {
184 m_data = m_data.subspan(offset, count).value_or(Span<byte> {});
185 }
186
187 constexpr auto share() && -> ByteBuffer {
188 auto data = di::exchange(m_data, Span<byte> {});
189 return ByteBuffer(data, di::move(m_store));
190 }
191
192 constexpr operator ByteBuffer() && { return share(); }
193
194private:
195 Span<byte> m_data;
196 Store m_store;
197};
198}
199
200namespace di {
203}
Definition optional_forward_declaration.h:5
Definition span_forward_declaration.h:10
Definition byte_buffer.h:76
constexpr auto span() const -> Span< byte const >
Definition byte_buffer.h:100
constexpr auto slice(usize offset, usize count) const -> Optional< ByteBufferImpl >
Definition byte_buffer.h:123
constexpr ByteBufferImpl(Span< byte const > data, Store &&store)
Definition byte_buffer.h:95
ByteStore< Alloc > Store
Definition byte_buffer.h:78
constexpr auto size() const -> usize
Definition byte_buffer.h:99
constexpr auto slice(usize offset) const -> Optional< ByteBufferImpl >
Definition byte_buffer.h:117
constexpr auto empty() const -> bool
Definition byte_buffer.h:101
static constexpr auto create(T &&value) -> meta::LikeExpected< meta::AllocatorResult< Alloc >, ByteBufferImpl >
Definition byte_buffer.h:83
constexpr auto data() const -> byte const *
Definition byte_buffer.h:98
constexpr auto first(usize count) const -> Optional< ByteBufferImpl >
Definition byte_buffer.h:105
constexpr auto last(usize count) const -> Optional< ByteBufferImpl >
Definition byte_buffer.h:111
constexpr ByteBufferImpl(Span< byte const > data, Store const &store)
Definition byte_buffer.h:96
constexpr ByteBufferImpl(T &&value)
Definition byte_buffer.h:93
ByteBufferImpl ByteBuffer
Definition byte_buffer.h:79
constexpr auto store() const &-> Store const &
Definition byte_buffer.h:103
constexpr auto shrink_to_slice(usize offset)
Definition byte_buffer.h:182
constexpr auto store() &&-> Store &&
Definition byte_buffer.h:175
constexpr auto span() const -> Span< byte >
Definition byte_buffer.h:172
ByteBufferImpl< Alloc > ByteBuffer
Definition byte_buffer.h:138
constexpr auto size() const -> usize
Definition byte_buffer.h:171
constexpr auto empty() const -> bool
Definition byte_buffer.h:173
constexpr auto shrink_to_slice(usize offset, usize count)
Definition byte_buffer.h:183
constexpr auto shrink_to_first(usize count)
Definition byte_buffer.h:180
constexpr auto operator=(ExclusiveByteBufferImpl const &) -> ExclusiveByteBufferImpl &=delete
constexpr ExclusiveByteBufferImpl(T &&value)
Definition byte_buffer.h:153
constexpr ExclusiveByteBufferImpl(ExclusiveByteBufferImpl const &)=delete
constexpr auto shrink_to_last(usize count)
Definition byte_buffer.h:181
constexpr ExclusiveByteBufferImpl(ExclusiveByteBufferImpl &&other)
Definition byte_buffer.h:160
static constexpr auto create(T &&value) -> meta::LikeExpected< meta::AllocatorResult< Alloc >, ExclusiveByteBufferImpl >
Definition byte_buffer.h:142
constexpr auto operator=(ExclusiveByteBufferImpl &&other) -> ExclusiveByteBufferImpl &
Definition byte_buffer.h:165
ByteStore< Alloc > Store
Definition byte_buffer.h:137
constexpr auto share() &&-> ByteBuffer
Definition byte_buffer.h:187
constexpr ExclusiveByteBufferImpl(Span< byte > data, Store &&store)
Definition byte_buffer.h:156
constexpr auto data() const -> byte *
Definition byte_buffer.h:170
Definition operations.h:99
Definition operations.h:114
Definition allocator.h:20
Definition tag_invoke.h:33
#define DI_TRY(...)
Definition monad_try.h:13
Any< Interface, SharedStorage< Alloc > > AnyShared
Definition any_shared.h:9
Type< detail::LikeExpectedHelper< T, U > > LikeExpected
Definition vocab.h:60
size_t usize
Definition integers.h:33
Definition byte_buffer.h:20
AnyShared< meta::List<>, Alloc > ByteStore
Definition byte_buffer.h:67
Definition zstring_parser.h:9
constexpr tag_invoke_detail::TagInvokeFn tag_invoke
Definition tag_invoke.h:22
constexpr auto exchange(T &object, U &&new_value) -> T
Definition exchange.h:8
vocab::byte_buffer::ExclusiveByteBufferImpl<> ExclusiveByteBuffer
Definition byte_buffer.h:202
constexpr auto as_writable_byte_span
Definition byte_buffer.h:61
constexpr auto count
Definition count.h:37
vocab::byte_buffer::ByteBufferImpl<> ByteBuffer
Definition byte_buffer.h:201
constexpr auto as_byte_span
Definition byte_buffer.h:62
Definition method.h:7
Definition this.h:4
Definition byte_buffer.h:40
Method< AsByteSpan, Span< byte const >(This const &)> Type
Definition byte_buffer.h:41
Method< AsWritableByteSpan, Span< byte >(This &)> Type
Definition byte_buffer.h:22