roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
input_stream_reader.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
9#include "roo_io/data/read.h"
10
11namespace roo_io {
12
13/// Buffered typed reader over `InputStream`.
14///
15/// Uses a 64-byte internal buffer to avoid tiny upstream reads.
16///
17/// Construction with `unique_ptr` transfers ownership; construction with
18/// reference does not.
19///
20/// After constructing this reader, access the stream only through this reader
21/// to keep buffer state coherent.
22///
23/// Reader closes the stream on destruction or explicit `close()`.
25 public:
26 InputStreamReader() : is_(nullptr), owned_(false), in_() {}
27
29 : is_(other.is_), owned_(other.owned_), in_(std::move(other.in_)) {
30 other.is_ = nullptr;
31 other.owned_ = false;
32 other.in_.reset();
33 }
34
36 if (this != &other) {
37 close();
38 is_ = other.is_;
39 owned_ = other.owned_;
40 in_ = std::move(other.in_);
41 other.is_ = nullptr;
42 other.owned_ = false;
43 other.in_.reset();
44 }
45 return *this;
46 }
47
48 InputStreamReader(std::unique_ptr<roo_io::InputStream> is)
49 : is_(is.release()), owned_(is_ != nullptr), in_(*is_) {}
50
52 : is_(&is), owned_(false), in_(*is_) {}
53
55 if (is_ != nullptr) {
56 is_->close();
57 }
58 if (owned_) delete is_;
59 }
60
61 void reset(std::unique_ptr<roo_io::InputStream> is) {
62 if (is_ == is.get()) {
63 owned_ = true;
64 return;
65 }
66 if (is_ != nullptr) {
67 is_->close();
68 }
69 if (owned_) {
70 delete is_;
71 }
72 is_ = is.release();
73 if (is_ == nullptr) {
74 in_.reset();
75 } else {
76 owned_ = true;
77 in_.reset(*is_);
78 }
79 }
80
82 if (is_ == &is) {
83 CHECK(!owned_);
84 return;
85 }
86 if (is_ != nullptr) {
87 is_->close();
88 }
89 if (owned_) {
90 delete is_;
91 }
92 is_ = &is;
93 owned_ = false;
94 in_.reset(*is_);
95 }
96
97 void close() {
98 if (is_ == nullptr) return;
99 is_->close();
100 if (owned_) {
101 delete is_;
102 owned_ = false;
103 }
104 is_ = nullptr;
105 in_.reset();
106 }
107
108 byte read() { return in_.read(); }
109
110 void skip(size_t count) { in_.skip(count); }
111
112 Status status() const { return in_.status(); }
113
114 uint16_t readU8() { return ReadU8(in_); }
115
116 uint16_t readBeU16() { return ReadBeU16(in_); }
117
118 uint16_t readLeU16() { return ReadLeU16(in_); }
119
120 uint32_t readBeU24() { return ReadBeU24(in_); }
121
122 uint32_t readLeU24() { return ReadLeU24(in_); }
123
124 uint32_t readBeU32() { return ReadBeU32(in_); }
125
126 uint32_t readLeU32() { return ReadLeU32(in_); }
127
128 uint64_t readBeU64() { return ReadBeU64(in_); }
129
130 uint64_t readLeU64() { return ReadLeU64(in_); }
131
132 int16_t readS8() { return ReadS8(in_); }
133
134 int16_t readBeS16() { return ReadBeS16(in_); }
135
136 int16_t readLeS16() { return ReadLeS16(in_); }
137
138 int32_t readBeS24() { return ReadBeS24(in_); }
139
140 int32_t readLeS24() { return ReadLeS24(in_); }
141
142 int32_t readBeS32() { return ReadBeS32(in_); }
143
144 int32_t readLeS32() { return ReadLeS32(in_); }
145
146 int64_t readBeS64() { return ReadBeS64(in_); }
147
148 int64_t readLeS64() { return ReadLeS64(in_); }
149
150#if ROO_IO_IEEE754
151 float readBeFloat() { return ReadBeFloat(in_); }
152
153 float readLeFloat() { return ReadLeFloat(in_); }
154
155 double readBeDouble() { return ReadBeDouble(in_); }
156
157 double readLeDouble() { return ReadLeDouble(in_); }
158#endif // ROO_IO_IEEE754
159
160 size_t readByteArray(byte* result, size_t count) {
161 return ReadByteArray(in_, result, count);
162 }
163
164 size_t readCString(char* buf, size_t capacity = SIZE_MAX) {
165 return ReadCString(in_, buf, capacity);
166 }
167
168 std::string readString(size_t max_size = SIZE_MAX) {
169 return ReadString(in_, max_size);
170 }
171
172 template <typename T>
174 return HostNativeReader<T>().read(in_, std::move(default_value));
175 }
176
177 uint64_t readVarU64() { return ReadVarU64(in_); }
178
179 private:
181 bool owned_;
183};
184
185} // namespace roo_io
void skip(size_t count)
Skips up to count bytes.
Status status() const
Returns current iterator status.
void reset(roo_io::InputStream &input)
Rebinds iterator to input and clears buffered state.
Buffered typed reader over InputStream.
std::string readString(size_t max_size=SIZE_MAX)
InputStreamReader(std::unique_ptr< roo_io::InputStream > is)
void reset(roo_io::InputStream &is)
size_t readByteArray(byte *result, size_t count)
InputStreamReader & operator=(InputStreamReader &&other)
InputStreamReader(roo_io::InputStream &is)
void reset(std::unique_ptr< roo_io::InputStream > is)
T readHostNative(T default_value=T())
InputStreamReader(InputStreamReader &&other)
size_t readCString(char *buf, size_t capacity=SIZE_MAX)
Virtualizes access to files, memory, and other readable sources.
virtual void close()
Closes this stream.
Definition byte.h:6
constexpr uint32_t ReadBeU32(InputIterator &in)
Definition read.h:52
size_t ReadCString(InputIterator &in, char *buf, size_t capacity=SIZE_MAX)
Definition read.h:432
constexpr uint64_t ReadLeU64(InputIterator &in)
Definition read.h:72
constexpr uint16_t ReadBeU16(InputIterator &in)
Definition read.h:26
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
constexpr uint32_t ReadLeU24(InputIterator &in)
Definition read.h:45
constexpr int16_t ReadBeS16(InputIterator &in)
Definition read.h:86
size_t ReadByteArray(InputIterator &in, byte *result, size_t count)
Definition read.h:201
constexpr uint32_t ReadBeU24(InputIterator &in)
Definition read.h:38
constexpr int64_t ReadBeS64(InputIterator &in)
Definition read.h:131
constexpr int64_t ReadLeS64(InputIterator &in)
Definition read.h:137
constexpr int32_t ReadBeS24(InputIterator &in)
Definition read.h:107
size_t count
Definition compare.h:45
constexpr int8_t ReadS8(InputIterator &in)
Definition read.h:80
constexpr uint64_t ReadBeU64(InputIterator &in)
Definition read.h:66
constexpr uint32_t ReadLeU32(InputIterator &in)
Definition read.h:59
constexpr int32_t ReadBeS32(InputIterator &in)
Definition read.h:119
Status
Definition status.h:7
constexpr int16_t ReadLeS16(InputIterator &in)
Definition read.h:92
constexpr int32_t ReadLeS32(InputIterator &in)
Definition read.h:125
uint64_t ReadVarU64(InputIterator &in)
Definition read.h:217
constexpr int32_t ReadLeS24(InputIterator &in)
Definition read.h:113
constexpr uint8_t ReadU8(InputIterator &in)
Definition read.h:20
constexpr uint16_t ReadLeU16(InputIterator &in)
Definition read.h:32
std::string ReadString(InputIterator &in, size_t max_size=SIZE_MAX)
Definition read.h:457