roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
memory_input_iterator.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstring>
4
6
7namespace roo_io {
8
9/// Infinite iterator reading from memory starting at provided address.
10///
11/// Reader controls bounds externally.
12///
13/// Memory footprint: one pointer (4 bytes on 32-bit targets).
14///
15/// Implements input iterator contract.
16template <typename PtrTypeT>
18 public:
20
21 /// Creates iterator starting at `ptr`.
23
24 /// Returns byte at current pointer and advances by one.
25 ///
26 /// Does not modify status (`status()` is always `kOk`).
27 byte read() { return *ptr_++; }
28
29 /// Copies `count` bytes from current pointer, then advances by `count`.
30 ///
31 /// Does not modify status (`status()` is always `kOk`).
32 size_t read(byte* result, size_t count) {
34 ptr_ += count;
35 return count;
36 }
37
38 /// Advances pointer by `count` bytes.
39 ///
40 /// Does not modify status (`status()` is always `kOk`).
41 void skip(size_t count) { ptr_ += count; }
42
43 /// Returns `kOk` unconditionally.
44 Status status() const { return kOk; }
45
46 /// Returns current pointer.
47 PtrType ptr() const { return ptr_; }
48
49 protected:
51};
52
54
55/// Bounded iterator reading from [`begin`, `end`).
56///
57/// Reaching `end` transitions status to `kEndOfStream`.
58///
59/// Memory footprint: two pointers (8 bytes on 32-bit targets).
60///
61/// Implements input iterator contract.
62template <typename PtrTypeT>
64 public:
66
67 /// Creates bounded iterator over [`begin`, `end`).
70
71 /// Reads one byte when available.
72 ///
73 /// If already exhausted, returns zero byte and keeps EOS state.
74 /// If this call detects exhaustion, marks EOS by setting internal end marker
75 /// to null.
76 byte read() {
77 if (ptr_ == end_ || end_ == nullptr) {
78 end_ = nullptr;
79 return byte{0};
80 }
81 return *ptr_++;
82 }
83
84 /// Reads up to `count` bytes when available.
85 ///
86 /// Returns zero when exhausted and keeps EOS state.
87 /// If this call detects exhaustion before reading, marks EOS.
88 /// Reading exactly to the original end keeps status `kOk` until a later call
89 /// detects exhaustion.
90 size_t read(byte* result, size_t count) {
91 if (ptr_ == end_ || end_ == nullptr) {
92 end_ = nullptr;
93 return 0;
94 }
95 const size_t available = static_cast<size_t>(end_ - ptr_);
96 if (count > available) {
97 count = available;
98 }
99 memcpy(result, ptr_, count);
100 ptr_ += count;
101 return count;
102 }
103
104 /// Skips up to `count` bytes.
105 ///
106 /// Skipping past available range sets EOS.
107 /// Skipping exactly to end keeps status `kOk` until a later operation
108 /// detects exhaustion.
109 void skip(size_t count) {
110 if (end_ != nullptr) {
111 const size_t available = static_cast<size_t>(end_ - ptr_);
112 if (count <= available) {
113 ptr_ += count;
114 } else {
115 ptr_ = end_;
116 end_ = nullptr;
117 }
118 }
119 }
120
121 /// Returns `kEndOfStream` after EOS marker is set, otherwise `kOk`.
122 Status status() const { return end_ == nullptr ? kEndOfStream : kOk; }
123
124 /// Returns current pointer.
125 PtrType ptr() const { return ptr_; }
126 // PtrType end() const { return end_; }
127
128 private:
129 PtrType ptr_;
130 PtrType end_;
131};
132
134
135/// Multipass bounded iterator reading from [`begin`, `end`).
136///
137/// Supports `size()`, `position()`, `rewind()`, and `seek()`.
138///
139/// Memory footprint: three pointers and a boolean.
140///
141/// Implements multipass input iterator contract.
142template <typename PtrTypeT>
144 public:
146
147 /// Creates multipass iterator over [`begin`, `end`).
149 : ptr_(begin), position_(0), size_(end - begin), eos_(false) {}
150
151 /// Reads one byte at current position and advances.
152 ///
153 /// If position is already at/after size, returns zero byte and sets EOS.
154 byte read() {
155 if (position_ >= size_) {
156 eos_ = true;
157 return byte{0};
158 }
159 return ptr_[position_++];
160 }
161
162 /// Reads up to `count` bytes from current position and advances.
163 ///
164 /// If already at/after size, returns zero and sets EOS.
165 /// Reading exactly to end does not set EOS.
166 size_t read(byte* result, size_t count) {
167 if (position_ >= size_) {
168 eos_ = true;
169 return 0;
170 }
171 if (count > size_ - position_) {
172 count = size_ - position_;
173 }
174 memcpy(result, ptr_ + position_, count);
175 position_ += count;
176 return count;
177 }
178
179 /// Advances by up to `count` bytes.
180 ///
181 /// Advancing past end clamps to end and sets EOS.
182 /// Advancing exactly to end keeps status `kOk`.
183 void skip(size_t count) {
184 if (position_ + count <= size_) {
185 position_ += count;
186 } else {
187 position_ = size_;
188 eos_ = true;
189 return;
190 }
191 }
192
193 /// Returns `kEndOfStream` after EOS is observed, otherwise `kOk`.
194 Status status() const { return eos_ ? kEndOfStream : kOk; }
195
196 /// Returns pointer at current position.
197 PtrType ptr() const { return ptr_ + position_; }
198
199 /// Returns total iterable size.
200 uint64_t size() const { return size_; }
201
202 /// Returns current position.
203 uint64_t position() const { return position_; }
204
205 /// Resets position to start and clears EOS.
206 void rewind() {
207 position_ = 0;
208 eos_ = false;
209 }
210
211 /// Sets absolute position and clears EOS.
212 ///
213 /// Position is not range-checked here; out-of-range is detected by
214 /// subsequent read/skip operations.
216 position_ = position;
217 eos_ = false;
218 }
219
220 private:
221 PtrType ptr_;
222 size_t position_;
223 size_t size_;
224 bool eos_;
225};
226
228
229namespace internal {
230
231template <typename Itr>
232struct MemoryIteratorTraits {
233 static constexpr bool is_memory = false;
234};
235
236template <typename PtrType>
237struct MemoryIteratorTraits<UnsafeGenericMemoryIterator<PtrType>> {
238 static constexpr bool is_memory = true;
239};
240
241template <typename PtrType>
242struct MemoryIteratorTraits<SafeGenericMemoryIterator<PtrType>> {
243 static constexpr bool is_memory = true;
244};
245
246template <typename PtrType>
247struct MemoryIteratorTraits<MultipassGenericMemoryIterator<PtrType>> {
248 static constexpr bool is_memory = true;
249};
250
251} // namespace internal
252
253} // namespace roo_io
Multipass bounded iterator reading from [begin, end).
void skip(size_t count)
Advances by up to count bytes.
byte read()
Reads one byte at current position and advances.
void seek(uint64_t position)
Sets absolute position and clears EOS.
Status status() const
Returns kEndOfStream after EOS is observed, otherwise kOk.
MultipassGenericMemoryIterator(PtrType begin, PtrType end)
Creates multipass iterator over [begin, end).
uint64_t size() const
Returns total iterable size.
size_t read(byte *result, size_t count)
Reads up to count bytes from current position and advances.
void rewind()
Resets position to start and clears EOS.
PtrType ptr() const
Returns pointer at current position.
uint64_t position() const
Returns current position.
Bounded iterator reading from [begin, end).
PtrType ptr() const
Returns current pointer.
size_t read(byte *result, size_t count)
Reads up to count bytes when available.
byte read()
Reads one byte when available.
Status status() const
Returns kEndOfStream after EOS marker is set, otherwise kOk.
void skip(size_t count)
Skips up to count bytes.
SafeGenericMemoryIterator(PtrType begin, PtrType end)
Creates bounded iterator over [begin, end).
Infinite iterator reading from memory starting at provided address.
byte read()
Returns byte at current pointer and advances by one.
void skip(size_t count)
Advances pointer by count bytes.
Status status() const
Returns kOk unconditionally.
UnsafeGenericMemoryIterator(PtrType ptr)
Creates iterator starting at ptr.
size_t read(byte *result, size_t count)
Copies count bytes from current pointer, then advances by count.
PtrType ptr() const
Returns current pointer.
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
size_t count
Definition compare.h:45
SafeGenericMemoryIterator< const byte * > MemoryIterator
Status
Definition status.h:7
@ kOk
Definition status.h:8
@ kEndOfStream
Definition status.h:9
UnsafeGenericMemoryIterator< const byte * > UnsafeMemoryIterator
MultipassGenericMemoryIterator< const byte * > MultipassMemoryIterator