C/C++ API Reference
Loading...
Searching...
No Matches
memory_stream.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <array>
17#include <cstddef>
18
19#include "pw_bytes/span.h"
20#include "pw_result/result.h"
21#include "pw_span/span.h"
22#include "pw_stream/seek.h"
23#include "pw_stream/stream.h"
24
25namespace pw::stream {
26
28
37 public:
38 using difference_type = ptrdiff_t;
39 using reference = const std::byte&;
40 using const_reference = const std::byte&;
41 using pointer = const std::byte*;
42 using const_pointer = const std::byte*;
43 using iterator = const std::byte*;
44 using const_iterator = const std::byte*;
45
46 constexpr MemoryWriter(ByteSpan dest) : dest_(dest) {}
47
48 // Construct writer with prepopulated data in the buffer. The number of
49 // pre-written bytes is provided as `bytes_written`.
50 //
51 // Precondition: The number of pre-written bytes must not be greater than the
52 // size of the provided buffer.
53 constexpr MemoryWriter(ByteSpan dest, size_t bytes_written)
54 : dest_(dest), position_(bytes_written) {
55 PW_ASSERT(position_ <= dest.size_bytes());
56 }
57
58 ConstByteSpan WrittenData() const { return dest_.first(position_); }
59
60 void clear() { position_ = 0; }
61
62 std::byte* data() { return dest_.data(); }
63 const std::byte* data() const { return dest_.data(); }
64
65 const std::byte& operator[](size_t index) const { return dest_[index]; }
66
67 [[nodiscard]] bool empty() const { return size() == 0u; }
68
69 size_t size() const { return position_; }
70 size_t bytes_written() const { return size(); }
71
72 size_t capacity() const { return dest_.size(); }
73
74 const std::byte* begin() const { return dest_.data(); }
75 const std::byte* end() const { return dest_.data() + position_; }
76
77 private:
78 size_t ConservativeLimit(LimitType type) const override {
79 return type == LimitType::kWrite ? dest_.size_bytes() - position_ : 0;
80 }
81
82 // Implementation for writing data to this stream.
83 //
84 // If the in-memory buffer is exhausted in the middle of a write, this will
85 // perform a partial write and Status::ResourceExhausted() will be returned.
87
88 Status DoSeek(ptrdiff_t offset, Whence origin) final {
89 return CalculateSeek(offset, origin, dest_.size(), position_);
90 }
91
92 size_t DoTell() final { return position_; }
93
94 ByteSpan dest_;
95 size_t position_ = 0;
96};
97
98template <size_t kSizeBytes>
99class MemoryWriterBuffer final : public MemoryWriter {
100 public:
101 constexpr MemoryWriterBuffer() : MemoryWriter(buffer_) {}
102
108
114
115 private:
116 std::array<std::byte, kSizeBytes> buffer_;
117};
118
121class MemoryReader final : public SeekableReader {
122 public:
123 constexpr MemoryReader(ConstByteSpan source)
124 : source_(source), position_(0) {}
125
126 size_t bytes_read() const { return position_; }
127
128 const std::byte* data() const { return source_.data(); }
129
130 private:
131 size_t ConservativeLimit(LimitType type) const override {
132 return type == LimitType::kRead ? source_.size_bytes() - position_ : 0;
133 }
134
135 Status DoSeek(ptrdiff_t offset, Whence origin) override {
136 return CalculateSeek(offset, origin, source_.size(), position_);
137 }
138
139 size_t DoTell() override { return position_; }
140
141 // Implementation for reading data from this stream.
142 //
143 // If the in-memory buffer does not have enough remaining bytes for what was
144 // requested, this will perform a partial read and OK will still be returned.
146
147 ConstByteSpan source_;
148 size_t position_;
149};
150
152
153} // namespace pw::stream
Definition: status.h:109
Definition: status_with_size.h:51
Definition: memory_stream.h:121
StatusWithSize DoRead(ByteSpan dest) override
Virtual Read() function implemented by derived classes.
size_t ConservativeLimit(LimitType type) const override
Definition: memory_stream.h:131
Status DoSeek(ptrdiff_t offset, Whence origin) override
Virtual Seek() function implemented by derived classes.
Definition: memory_stream.h:135
size_t DoTell() override
Definition: memory_stream.h:139
Definition: memory_stream.h:99
MemoryWriterBuffer(MemoryWriterBuffer &&)=delete
MemoryWriterBuffer & operator=(MemoryWriter &&)=delete
Definition: memory_stream.h:36
Status DoSeek(ptrdiff_t offset, Whence origin) final
Virtual Seek() function implemented by derived classes.
Definition: memory_stream.h:88
size_t DoTell() final
Definition: memory_stream.h:92
Status DoWrite(ConstByteSpan data) final
Virtual Write() function implemented by derived classes.
size_t ConservativeLimit(LimitType type) const override
Definition: memory_stream.h:78
Definition: stream.h:400
Definition: stream.h:491
Whence
Positions from which to seek.
Definition: stream.h:48