MCPcopy Create free account
hub / github.com/ElementsProject/elements / SpanReader

Class SpanReader

src/streams.h:134–178  ·  view source on GitHub ↗

Minimal stream for reading from an existing byte array by Span. */

Source from the content-addressed store, hash-verified

132/** Minimal stream for reading from an existing byte array by Span.
133 */
134class SpanReader
135{
136private:
137 const int m_type;
138 const int m_version;
139 Span<const unsigned char> m_data;
140
141public:
142
143 /**
144 * @param[in] type Serialization Type
145 * @param[in] version Serialization Version (including any flags)
146 * @param[in] data Referenced byte vector to overwrite/append
147 */
148 SpanReader(int type, int version, Span<const unsigned char> data)
149 : m_type(type), m_version(version), m_data(data) {}
150
151 template<typename T>
152 SpanReader& operator>>(T&& obj)
153 {
154 // Unserialize from this stream
155 ::Unserialize(*this, obj);
156 return (*this);
157 }
158
159 int GetVersion() const { return m_version; }
160 int GetType() const { return m_type; }
161
162 size_t size() const { return m_data.size(); }
163 bool empty() const { return m_data.empty(); }
164
165 void read(Span<std::byte> dst)
166 {
167 if (dst.size() == 0) {
168 return;
169 }
170
171 // Read from the beginning of the buffer
172 if (dst.size() > m_data.size()) {
173 throw std::ios_base::failure("SpanReader::read(): end of data");
174 }
175 memcpy(dst.data(), m_data.data(), dst.size());
176 m_data = m_data.subspan(dst.size());
177 }
178};
179
180/** Double ended buffer combining vector and stream-like interfaces.
181 *

Callers

nothing calls this directly

Calls 1

UnserializeFunction · 0.85

Tested by

no test coverage detected