mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 23:09:44 -04:00
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:
parent
3ce7b27124
commit
fa9becfe1c
1 changed files with 53 additions and 30 deletions
|
@ -182,16 +182,13 @@ public:
|
||||||
* >> and << read and write unformatted data using the above serialization templates.
|
* >> and << read and write unformatted data using the above serialization templates.
|
||||||
* Fills with data in linear time; some stringstream implementations take N^2 time.
|
* Fills with data in linear time; some stringstream implementations take N^2 time.
|
||||||
*/
|
*/
|
||||||
class CDataStream
|
class DataStream
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
using vector_type = SerializeData;
|
using vector_type = SerializeData;
|
||||||
vector_type vch;
|
vector_type vch;
|
||||||
vector_type::size_type m_read_pos{0};
|
vector_type::size_type m_read_pos{0};
|
||||||
|
|
||||||
int nType;
|
|
||||||
int nVersion;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef vector_type::allocator_type allocator_type;
|
typedef vector_type::allocator_type allocator_type;
|
||||||
typedef vector_type::size_type size_type;
|
typedef vector_type::size_type size_type;
|
||||||
|
@ -203,23 +200,9 @@ public:
|
||||||
typedef vector_type::const_iterator const_iterator;
|
typedef vector_type::const_iterator const_iterator;
|
||||||
typedef vector_type::reverse_iterator reverse_iterator;
|
typedef vector_type::reverse_iterator reverse_iterator;
|
||||||
|
|
||||||
explicit CDataStream(int nTypeIn, int nVersionIn)
|
explicit DataStream() {}
|
||||||
: nType{nTypeIn},
|
explicit DataStream(Span<const uint8_t> sp) : DataStream{AsBytes(sp)} {}
|
||||||
nVersion{nVersionIn} {}
|
explicit DataStream(Span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {}
|
||||||
|
|
||||||
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)...);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string str() const
|
std::string str() const
|
||||||
{
|
{
|
||||||
|
@ -271,11 +254,6 @@ public:
|
||||||
bool eof() const { return size() == 0; }
|
bool eof() const { return size() == 0; }
|
||||||
int in_avail() const { return size(); }
|
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)
|
void read(Span<value_type> dst)
|
||||||
{
|
{
|
||||||
if (dst.size() == 0) return;
|
if (dst.size() == 0) return;
|
||||||
|
@ -283,7 +261,7 @@ public:
|
||||||
// Read from the beginning of the buffer
|
// Read from the beginning of the buffer
|
||||||
auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
|
auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
|
||||||
if (!next_read_pos.has_value() || next_read_pos.value() > vch.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());
|
memcpy(dst.data(), &vch[m_read_pos], dst.size());
|
||||||
if (next_read_pos.value() == vch.size()) {
|
if (next_read_pos.value() == vch.size()) {
|
||||||
|
@ -299,7 +277,7 @@ public:
|
||||||
// Ignore from the beginning of the buffer
|
// Ignore from the beginning of the buffer
|
||||||
auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
|
auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
|
||||||
if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
|
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()) {
|
if (next_read_pos.value() == vch.size()) {
|
||||||
m_read_pos = 0;
|
m_read_pos = 0;
|
||||||
|
@ -324,7 +302,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
CDataStream& operator<<(const T& obj)
|
DataStream& operator<<(const T& obj)
|
||||||
{
|
{
|
||||||
// Serialize to this stream
|
// Serialize to this stream
|
||||||
::Serialize(*this, obj);
|
::Serialize(*this, obj);
|
||||||
|
@ -332,7 +310,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
CDataStream& operator>>(T&& obj)
|
DataStream& operator>>(T&& obj)
|
||||||
{
|
{
|
||||||
// Unserialize from this stream
|
// Unserialize from this stream
|
||||||
::Unserialize(*this, obj);
|
::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>
|
template <typename IStream>
|
||||||
class BitStreamReader
|
class BitStreamReader
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue