roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
memory_output_iterator.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstring>
5
7
8namespace roo_io {
9
10/// Infinite output iterator writing to memory from given address.
11///
12/// Writer controls bounds externally.
14 public:
15 /// Creates iterator starting at `ptr`.
17
18 /// Writes one byte and advances output pointer.
19 ///
20 /// Does not modify status (`status()` is always `kOk`).
21 void write(byte v) { *ptr_++ = v; }
22
23 /// Writes `count` bytes and advances output pointer.
24 ///
25 /// Does not modify status (`status()` is always `kOk`).
26 ///
27 /// @return Number of bytes written (`count`).
28 size_t write(const byte* buf, size_t count) {
29 memcpy(ptr_, buf, count);
30 ptr_ += count;
31 return count;
32 }
33
34 /// Returns current status (`kOk`).
35 ///
36 /// @return `kOk`.
37 Status status() const { return kOk; }
38
39 /// Returns current output pointer.
40 ///
41 /// @return Current pointer.
42 byte* ptr() const { return ptr_; }
43
44 /// Flushes output (no-op).
45 ///
46 /// Does not modify status.
47 void flush() {}
48
49 private:
50 byte* ptr_;
51};
52
53/// Bounded output iterator writing from given address up to `end`.
54///
55/// Writing past end is rejected and status becomes `kNoSpaceLeftOnDevice`.
57 public:
58 /// Creates bounded iterator over [`ptr`, `end`).
59 MemoryOutputIterator(byte* ptr, const byte* end) : ptr_(ptr), end_(end) {}
60
61 /// Creates bounded iterator over fixed array.
62 template <size_t N>
63 MemoryOutputIterator(byte buf[N]) : ptr_(buf), end_(&buf[N]) {}
64
65 /// Writes one byte.
66 ///
67 /// If no space remains, write is ignored and status becomes
68 /// `kNoSpaceLeftOnDevice`.
69 void write(byte v) {
70 if (end_ == nullptr) {
71 return;
72 }
73 if (ptr_ == end_) {
74 end_ = nullptr;
75 return;
76 }
77 *ptr_++ = v;
78 }
79
80 /// Writes up to `count` bytes.
81 ///
82 /// If available space is smaller than `count`, writes available prefix,
83 /// updates status to `kNoSpaceLeftOnDevice`, and returns bytes written.
84 ///
85 /// @return Number of bytes written.
86 size_t write(const byte* buf, size_t count) {
87 if (end_ == nullptr) return 0;
88 if (count > (size_t)(end_ - ptr_)) {
89 count = end_ - ptr_;
90 end_ = nullptr;
91 }
92 memcpy(ptr_, buf, count);
93 ptr_ += count;
94 return count;
95 }
96
97 /// Returns current status.
98 ///
99 /// @return `kOk` when space remains; `kNoSpaceLeftOnDevice` after overflow.
100 Status status() const { return end_ == nullptr ? kNoSpaceLeftOnDevice : kOk; }
101
102 /// Returns current output pointer.
103 ///
104 /// @return Current pointer.
105 const byte* ptr() const { return ptr_; }
106
107 /// Flushes output (no-op).
108 ///
109 /// Does not modify status.
110 void flush() {}
111
112 private:
113 byte* ptr_;
114 const byte* end_;
115};
116
117template <typename Collection>
119 public:
120 /// Creates iterator appending to collection `c`.
122
123 /// Appends one byte to target collection.
124 ///
125 /// Does not modify status (`status()` is always `kOk`).
126 void write(byte b) { *itr_++ = (typename Collection::value_type)b; }
127
128 /// Appends `count` bytes to target collection.
129 ///
130 /// Does not modify status (`status()` is always `kOk`).
131 ///
132 /// @return Number of bytes appended (`count`).
133 size_t write(const byte* buf, size_t count) {
134 for (size_t i = 0; i < count; ++i) write(buf[i]);
135 return count;
136 }
137
138 /// Flushes output (no-op).
139 ///
140 /// Does not modify status.
141 void flush() {}
142
143 /// Returns current status (`kOk`).
144 ///
145 /// @return `kOk`.
146 Status status() const { return kOk; }
147
148 private:
149 std::back_insert_iterator<Collection> itr_;
150};
151
152} // namespace roo_io
void flush()
Flushes output (no-op).
size_t write(const byte *buf, size_t count)
Appends count bytes to target collection.
BackInsertingIterator(Collection &c)
Creates iterator appending to collection c.
void write(byte b)
Appends one byte to target collection.
Status status() const
Returns current status (kOk).
Bounded output iterator writing from given address up to end.
MemoryOutputIterator(byte *ptr, const byte *end)
Creates bounded iterator over [ptr, end).
MemoryOutputIterator(byte buf[N])
Creates bounded iterator over fixed array.
void flush()
Flushes output (no-op).
void write(byte v)
Writes one byte.
const byte * ptr() const
Returns current output pointer.
size_t write(const byte *buf, size_t count)
Writes up to count bytes.
Status status() const
Returns current status.
Infinite output iterator writing to memory from given address.
byte * ptr() const
Returns current output pointer.
void write(byte v)
Writes one byte and advances output pointer.
UnsafeMemoryOutputIterator(byte *ptr)
Creates iterator starting at ptr.
Status status() const
Returns current status (kOk).
size_t write(const byte *buf, size_t count)
Writes count bytes and advances output pointer.
void flush()
Flushes output (no-op).
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
Status
Definition status.h:7
@ kNoSpaceLeftOnDevice
Definition status.h:55
@ kOk
Definition status.h:8