Pixie
Loading...
Searching...
No Matches
read_only_view.h
1#pragma once
2
3#include <pixie/storage.h>
4
5#include <span>
6
7namespace pixie {
8
15class ReadOnlyStorageView : public StorageBase<ReadOnlyStorageView> {
16 public:
17 ReadOnlyStorageView() = default;
18
20 explicit ReadOnlyStorageView(std::span<const std::byte> data) : data_(data) {}
21
23 std::size_t size_bytes_impl() const { return data_.size(); }
24
26 position_type begin_position_impl() const { return 0; }
27
29 position_type end_position_impl() const { return data_.size(); }
30
32 std::span<const std::byte> as_bytes_impl() const { return data_; }
33
35 SplitSpan<const std::byte> segments_impl(std::size_t offset_bytes,
36 std::size_t count_bytes) const {
37 return SplitSpan(data_.subspan(offset_bytes, count_bytes));
38 }
39
41 ReadOnlyStorageView view_impl(std::size_t offset_bytes,
42 std::size_t count_bytes) const {
43 if (offset_bytes > data_.size() ||
44 count_bytes > data_.size() - offset_bytes) {
45 throw std::out_of_range("Storage view is outside the backing storage");
46 }
47 return ReadOnlyStorageView(data_.subspan(offset_bytes, count_bytes));
48 }
49
60 static ReadOnlyStorageView deserialize_impl(BinaryReader& reader) {
61 const std::size_t size = reader.read_size();
62 return ReadOnlyStorageView(reader.read_bytes(size));
63 }
64
65 private:
66 std::span<const std::byte> data_;
67};
68
69} // namespace pixie
Bounds-checked reader for canonical little-endian binary data.
Definition serialization.h:526
std::span< const std::byte > read_bytes(std::size_t count)
Read exactly count uninterpreted bytes.
Definition serialization.h:604
std::size_t read_size()
Read an unsigned 64-bit size and convert it to size_t.
Definition serialization.h:587
ReadOnlyStorageView(std::span< const std::byte > data)
Construct a view over data.
Definition read_only_view.h:20
SplitSpan< const std::byte > segments_impl(std::size_t offset_bytes, std::size_t count_bytes) const
Return a checked viewed byte range as one physical segment.
Definition read_only_view.h:35
ReadOnlyStorageView view_impl(std::size_t offset_bytes, std::size_t count_bytes) const
Return a checked read-only byte subrange.
Definition read_only_view.h:41
static ReadOnlyStorageView deserialize_impl(BinaryReader &reader)
Deserialize a size-prefixed view and advance reader.
Definition read_only_view.h:60
std::span< const std::byte > as_bytes_impl() const
Return the viewed bytes.
Definition read_only_view.h:32
position_type end_position_impl() const
Return the position one past the final logical byte.
Definition read_only_view.h:29
position_type begin_position_impl() const
Return the first logical byte position.
Definition read_only_view.h:26
std::size_t size_bytes_impl() const
Return the number of viewed bytes.
Definition read_only_view.h:23
A logical contiguous sequence stored in at most two physical spans.
Definition split_span.h:25
CRTP facade for byte-addressable storage.
Definition storage.h:65
std::uint64_t position_type
Definition storage.h:68
Common interface for byte-addressable storage.