Skip to content

Commit fa9becf

Browse files
author
MarcoFalke
committed
streams: Add DataStream without ser-type and ser-version
The moved parts can be reviewed with "--color-moved=dimmed-zebra". The one-char changes can be reviewed with "--word-diff-regex=.".
1 parent 3ce7b27 commit fa9becf

File tree

1 file changed

+53
-30
lines changed

1 file changed

+53
-30
lines changed

src/streams.h

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,13 @@ class SpanReader
182182
* >> and << read and write unformatted data using the above serialization templates.
183183
* Fills with data in linear time; some stringstream implementations take N^2 time.
184184
*/
185-
class CDataStream
185+
class DataStream
186186
{
187187
protected:
188188
using vector_type = SerializeData;
189189
vector_type vch;
190190
vector_type::size_type m_read_pos{0};
191191

192-
int nType;
193-
int nVersion;
194-
195192
public:
196193
typedef vector_type::allocator_type allocator_type;
197194
typedef vector_type::size_type size_type;
@@ -203,23 +200,9 @@ class CDataStream
203200
typedef vector_type::const_iterator const_iterator;
204201
typedef vector_type::reverse_iterator reverse_iterator;
205202

206-
explicit CDataStream(int nTypeIn, int nVersionIn)
207-
: nType{nTypeIn},
208-
nVersion{nVersionIn} {}
209-
210-
explicit CDataStream(Span<const uint8_t> sp, int type, int version) : CDataStream{AsBytes(sp), type, version} {}
211-
explicit CDataStream(Span<const value_type> sp, int nTypeIn, int nVersionIn)
212-
: vch(sp.data(), sp.data() + sp.size()),
213-
nType{nTypeIn},
214-
nVersion{nVersionIn} {}
215-
216-
template <typename... Args>
217-
CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
218-
: nType{nTypeIn},
219-
nVersion{nVersionIn}
220-
{
221-
::SerializeMany(*this, std::forward<Args>(args)...);
222-
}
203+
explicit DataStream() {}
204+
explicit DataStream(Span<const uint8_t> sp) : DataStream{AsBytes(sp)} {}
205+
explicit DataStream(Span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {}
223206

224207
std::string str() const
225208
{
@@ -271,19 +254,14 @@ class CDataStream
271254
bool eof() const { return size() == 0; }
272255
int in_avail() const { return size(); }
273256

274-
void SetType(int n) { nType = n; }
275-
int GetType() const { return nType; }
276-
void SetVersion(int n) { nVersion = n; }
277-
int GetVersion() const { return nVersion; }
278-
279257
void read(Span<value_type> dst)
280258
{
281259
if (dst.size() == 0) return;
282260

283261
// Read from the beginning of the buffer
284262
auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
285263
if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
286-
throw std::ios_base::failure("CDataStream::read(): end of data");
264+
throw std::ios_base::failure("DataStream::read(): end of data");
287265
}
288266
memcpy(dst.data(), &vch[m_read_pos], dst.size());
289267
if (next_read_pos.value() == vch.size()) {
@@ -299,7 +277,7 @@ class CDataStream
299277
// Ignore from the beginning of the buffer
300278
auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
301279
if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
302-
throw std::ios_base::failure("CDataStream::ignore(): end of data");
280+
throw std::ios_base::failure("DataStream::ignore(): end of data");
303281
}
304282
if (next_read_pos.value() == vch.size()) {
305283
m_read_pos = 0;
@@ -324,15 +302,15 @@ class CDataStream
324302
}
325303

326304
template<typename T>
327-
CDataStream& operator<<(const T& obj)
305+
DataStream& operator<<(const T& obj)
328306
{
329307
// Serialize to this stream
330308
::Serialize(*this, obj);
331309
return (*this);
332310
}
333311

334312
template<typename T>
335-
CDataStream& operator>>(T&& obj)
313+
DataStream& operator>>(T&& obj)
336314
{
337315
// Unserialize from this stream
338316
::Unserialize(*this, obj);
@@ -363,6 +341,51 @@ class CDataStream
363341
}
364342
};
365343

344+
class CDataStream : public DataStream
345+
{
346+
private:
347+
int nType;
348+
int nVersion;
349+
350+
public:
351+
explicit CDataStream(int nTypeIn, int nVersionIn)
352+
: nType{nTypeIn},
353+
nVersion{nVersionIn} {}
354+
355+
explicit CDataStream(Span<const uint8_t> sp, int type, int version) : CDataStream{AsBytes(sp), type, version} {}
356+
explicit CDataStream(Span<const value_type> sp, int nTypeIn, int nVersionIn)
357+
: DataStream{sp},
358+
nType{nTypeIn},
359+
nVersion{nVersionIn} {}
360+
361+
template <typename... Args>
362+
CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
363+
: nType{nTypeIn},
364+
nVersion{nVersionIn}
365+
{
366+
::SerializeMany(*this, std::forward<Args>(args)...);
367+
}
368+
369+
void SetType(int n) { nType = n; }
370+
int GetType() const { return nType; }
371+
void SetVersion(int n) { nVersion = n; }
372+
int GetVersion() const { return nVersion; }
373+
374+
template <typename T>
375+
CDataStream& operator<<(const T& obj)
376+
{
377+
::Serialize(*this, obj);
378+
return *this;
379+
}
380+
381+
template <typename T>
382+
CDataStream& operator>>(T&& obj)
383+
{
384+
::Unserialize(*this, obj);
385+
return *this;
386+
}
387+
};
388+
366389
template <typename IStream>
367390
class BitStreamReader
368391
{

0 commit comments

Comments
 (0)