aboutsummaryrefslogtreecommitdiff
path: root/src/streams.h
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@protonmail.com>2021-02-01 15:05:56 +0100
committerWladimir J. van der Laan <laanwj@protonmail.com>2021-02-01 15:17:28 +0100
commit2c0fc856a6b0c82c5dddbbaee417171577514507 (patch)
treec60c97bd24c3d36cb310f49ff3501d7a255b5919 /src/streams.h
parent56fcf93349c3137af95e08c1b37a8726e7e01cb6 (diff)
parentfa292724598c273867bc6dbf311f1440fe2541ba (diff)
downloadbitcoin-2c0fc856a6b0c82c5dddbbaee417171577514507.tar.xz
Merge #20464: refactor: Treat CDataStream bytes as uint8_t
fa292724598c273867bc6dbf311f1440fe2541ba Remove redundant MakeUCharSpan wrappers (MarcoFalke) faf4aa2f47c0de4f3a0c5f5fe5b3ec32f611eefd Remove CDataStream::Init in favor of C++11 member initialization (MarcoFalke) fada14b948cac147198e3b685b5dd8cb72dc2911 Treat CDataStream bytes as uint8_t (MarcoFalke) fa8bdb048e65cae2d26bea3f991717a856e2fb39 refactor: Drop CDataStream constructors in favor of one taking a Span of bytes (MarcoFalke) faa96f841fe45bc49ebb6e07ac82a129fa9c40bf Remove 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 ACK fa292724598c273867bc6dbf311f1440fe2541ba theStack: ACK fa292724598c273867bc6dbf311f1440fe2541ba 🍾 Tree-SHA512: 931ee28bd99843d7e894b48e90e1187ffb0278677c267044b3c0c255069d9bbd9298ab2e539b1002a30b543d240450eaec718ef4ee95a7fd4be0a295e926343f
Diffstat (limited to 'src/streams.h')
-rw-r--r--src/streams.h90
1 files changed, 26 insertions, 64 deletions
diff --git a/src/streams.h b/src/streams.h
index fce411d3df..e78da31cbc 100644
--- a/src/streams.h
+++ b/src/streams.h
@@ -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);
- }
-
- 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);
- }
+ : nType{nTypeIn},
+ nVersion{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.
*