mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 22:18:54 +01:00
Merge #20464: refactor: Treat CDataStream bytes as uint8_t
fa29272459Remove redundant MakeUCharSpan wrappers (MarcoFalke)faf4aa2f47Remove CDataStream::Init in favor of C++11 member initialization (MarcoFalke)fada14b948Treat CDataStream bytes as uint8_t (MarcoFalke)fa8bdb048erefactor: Drop CDataStream constructors in favor of one taking a Span of bytes (MarcoFalke)faa96f841fRemove unused CDataStream methods (MarcoFalke) Pull request description: Using `uint8_t` for raw bytes has a style benefit: * The signedness is clear from reading the code, as it does not depend on the architecture Other clean-ups in this pull include: * Remove unused methods * Constructor is simplified with `Span` * Remove `Init()` member in favor of C++11 member initialization ACKs for top commit: laanwj: code review ACKfa29272459theStack: ACKfa29272459🍾 Tree-SHA512: 931ee28bd99843d7e894b48e90e1187ffb0278677c267044b3c0c255069d9bbd9298ab2e539b1002a30b543d240450eaec718ef4ee95a7fd4be0a295e926343f
This commit is contained in:
@@ -6,17 +6,19 @@
|
||||
#ifndef BITCOIN_STREAMS_H
|
||||
#define BITCOIN_STREAMS_H
|
||||
|
||||
#include <support/allocators/zeroafterfree.h>
|
||||
#include <serialize.h>
|
||||
#include <span.h>
|
||||
#include <support/allocators/zeroafterfree.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <assert.h>
|
||||
#include <ios>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -202,14 +204,14 @@ public:
|
||||
class CDataStream
|
||||
{
|
||||
protected:
|
||||
typedef CSerializeData vector_type;
|
||||
using vector_type = SerializeData;
|
||||
vector_type vch;
|
||||
unsigned int nReadPos;
|
||||
unsigned int nReadPos{0};
|
||||
|
||||
int nType;
|
||||
int nVersion;
|
||||
public:
|
||||
|
||||
public:
|
||||
typedef vector_type::allocator_type allocator_type;
|
||||
typedef vector_type::size_type size_type;
|
||||
typedef vector_type::difference_type difference_type;
|
||||
@@ -221,62 +223,22 @@ public:
|
||||
typedef vector_type::reverse_iterator reverse_iterator;
|
||||
|
||||
explicit CDataStream(int nTypeIn, int nVersionIn)
|
||||
{
|
||||
Init(nTypeIn, nVersionIn);
|
||||
}
|
||||
: nType{nTypeIn},
|
||||
nVersion{nVersionIn} {}
|
||||
|
||||
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
|
||||
{
|
||||
Init(nTypeIn, nVersionIn);
|
||||
}
|
||||
|
||||
CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
|
||||
{
|
||||
Init(nTypeIn, nVersionIn);
|
||||
}
|
||||
|
||||
CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
|
||||
{
|
||||
Init(nTypeIn, nVersionIn);
|
||||
}
|
||||
|
||||
CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
|
||||
{
|
||||
Init(nTypeIn, nVersionIn);
|
||||
}
|
||||
|
||||
CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
|
||||
{
|
||||
Init(nTypeIn, nVersionIn);
|
||||
}
|
||||
explicit CDataStream(Span<const uint8_t> 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}
|
||||
{
|
||||
Init(nTypeIn, nVersionIn);
|
||||
::SerializeMany(*this, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
void Init(int nTypeIn, int nVersionIn)
|
||||
{
|
||||
nReadPos = 0;
|
||||
nType = nTypeIn;
|
||||
nVersion = nVersionIn;
|
||||
}
|
||||
|
||||
CDataStream& operator+=(const CDataStream& b)
|
||||
{
|
||||
vch.insert(vch.end(), b.begin(), b.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
|
||||
{
|
||||
CDataStream ret = a;
|
||||
ret += b;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
std::string str() const
|
||||
{
|
||||
return (std::string(begin(), end()));
|
||||
@@ -297,12 +259,12 @@ public:
|
||||
const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
|
||||
reference operator[](size_type pos) { return vch[pos + nReadPos]; }
|
||||
void clear() { vch.clear(); nReadPos = 0; }
|
||||
iterator insert(iterator it, const char x=char()) { return vch.insert(it, x); }
|
||||
void insert(iterator it, size_type n, const char x) { vch.insert(it, n, x); }
|
||||
iterator insert(iterator it, const uint8_t x) { return vch.insert(it, x); }
|
||||
void insert(iterator it, size_type n, const uint8_t x) { vch.insert(it, n, x); }
|
||||
value_type* data() { return vch.data() + nReadPos; }
|
||||
const value_type* data() const { return vch.data() + nReadPos; }
|
||||
|
||||
void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
|
||||
void insert(iterator it, std::vector<uint8_t>::const_iterator first, std::vector<uint8_t>::const_iterator last)
|
||||
{
|
||||
if (last == first) return;
|
||||
assert(last - first > 0);
|
||||
@@ -373,12 +335,17 @@ public:
|
||||
nReadPos = 0;
|
||||
}
|
||||
|
||||
bool Rewind(size_type n)
|
||||
bool Rewind(std::optional<size_type> n = std::nullopt)
|
||||
{
|
||||
// Total rewind if no size is passed
|
||||
if (!n) {
|
||||
nReadPos = 0;
|
||||
return true;
|
||||
}
|
||||
// Rewind by n characters if the buffer hasn't been compacted yet
|
||||
if (n > nReadPos)
|
||||
if (*n > nReadPos)
|
||||
return false;
|
||||
nReadPos -= n;
|
||||
nReadPos -= *n;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -462,11 +429,6 @@ public:
|
||||
return (*this);
|
||||
}
|
||||
|
||||
void GetAndClear(CSerializeData &d) {
|
||||
d.insert(d.end(), begin(), end());
|
||||
clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* XOR the contents of this stream with a certain key.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user