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=.".
This commit is contained in:
MarcoFalke 2023-01-03 12:43:48 +01:00
parent 3ce7b27124
commit fa9becfe1c
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

@ -182,16 +182,13 @@ public:
* >> and << read and write unformatted data using the above serialization templates.
* Fills with data in linear time; some stringstream implementations take N^2 time.
*/
class CDataStream
class DataStream
{
protected:
using vector_type = SerializeData;
vector_type vch;
vector_type::size_type m_read_pos{0};
int nType;
int nVersion;
public:
typedef vector_type::allocator_type allocator_type;
typedef vector_type::size_type size_type;
@ -203,23 +200,9 @@ public:
typedef vector_type::const_iterator const_iterator;
typedef vector_type::reverse_iterator reverse_iterator;
explicit CDataStream(int nTypeIn, int nVersionIn)
: nType{nTypeIn},
nVersion{nVersionIn} {}
explicit CDataStream(Span<const uint8_t> sp, int type, int version) : CDataStream{AsBytes(sp), type, version} {}
explicit CDataStream(Span<const value_type> sp, int nTypeIn, int nVersionIn)
: vch(sp.data(), sp.data() + sp.size()),
nType{nTypeIn},
nVersion{nVersionIn} {}
template <typename... Args>
CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
: nType{nTypeIn},
nVersion{nVersionIn}
{
::SerializeMany(*this, std::forward<Args>(args)...);
}
explicit DataStream() {}
explicit DataStream(Span<const uint8_t> sp) : DataStream{AsBytes(sp)} {}
explicit DataStream(Span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {}
std::string str() const
{
@ -271,11 +254,6 @@ public:
bool eof() const { return size() == 0; }
int in_avail() const { return size(); }
void SetType(int n) { nType = n; }
int GetType() const { return nType; }
void SetVersion(int n) { nVersion = n; }
int GetVersion() const { return nVersion; }
void read(Span<value_type> dst)
{
if (dst.size() == 0) return;
@ -283,7 +261,7 @@ public:
// Read from the beginning of the buffer
auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
throw std::ios_base::failure("CDataStream::read(): end of data");
throw std::ios_base::failure("DataStream::read(): end of data");
}
memcpy(dst.data(), &vch[m_read_pos], dst.size());
if (next_read_pos.value() == vch.size()) {
@ -299,7 +277,7 @@ public:
// Ignore from the beginning of the buffer
auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
throw std::ios_base::failure("CDataStream::ignore(): end of data");
throw std::ios_base::failure("DataStream::ignore(): end of data");
}
if (next_read_pos.value() == vch.size()) {
m_read_pos = 0;
@ -324,7 +302,7 @@ public:
}
template<typename T>
CDataStream& operator<<(const T& obj)
DataStream& operator<<(const T& obj)
{
// Serialize to this stream
::Serialize(*this, obj);
@ -332,7 +310,7 @@ public:
}
template<typename T>
CDataStream& operator>>(T&& obj)
DataStream& operator>>(T&& obj)
{
// Unserialize from this stream
::Unserialize(*this, obj);
@ -363,6 +341,51 @@ public:
}
};
class CDataStream : public DataStream
{
private:
int nType;
int nVersion;
public:
explicit CDataStream(int nTypeIn, int nVersionIn)
: nType{nTypeIn},
nVersion{nVersionIn} {}
explicit CDataStream(Span<const uint8_t> sp, int type, int version) : CDataStream{AsBytes(sp), type, version} {}
explicit CDataStream(Span<const value_type> sp, int nTypeIn, int nVersionIn)
: DataStream{sp},
nType{nTypeIn},
nVersion{nVersionIn} {}
template <typename... Args>
CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
: nType{nTypeIn},
nVersion{nVersionIn}
{
::SerializeMany(*this, std::forward<Args>(args)...);
}
void SetType(int n) { nType = n; }
int GetType() const { return nType; }
void SetVersion(int n) { nVersion = n; }
int GetVersion() const { return nVersion; }
template <typename T>
CDataStream& operator<<(const T& obj)
{
::Serialize(*this, obj);
return *this;
}
template <typename T>
CDataStream& operator>>(T&& obj)
{
::Unserialize(*this, obj);
return *this;
}
};
template <typename IStream>
class BitStreamReader
{