0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-12 11:19:08 -05:00

Remove CDataStream::Init in favor of C++11 member initialization

This commit is contained in:
MarcoFalke 2020-11-23 19:19:56 +01:00
parent fada14b948
commit faf4aa2f47
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 19 additions and 21 deletions

View file

@ -84,7 +84,7 @@ static void PrevectorDeserialize(benchmark::Bench& bench)
for (auto x = 0; x < 1000; ++x) { for (auto x = 0; x < 1000; ++x) {
s0 >> t1; s0 >> t1;
} }
s0.Init(SER_NETWORK, 0); s0.Rewind();
}); });
} }

View file

@ -14,6 +14,7 @@
#include <assert.h> #include <assert.h>
#include <ios> #include <ios>
#include <limits> #include <limits>
#include <optional>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -205,12 +206,12 @@ class CDataStream
protected: protected:
using vector_type = SerializeData; using vector_type = SerializeData;
vector_type vch; vector_type vch;
unsigned int nReadPos; unsigned int nReadPos{0};
int nType; int nType;
int nVersion; 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;
typedef vector_type::difference_type difference_type; typedef vector_type::difference_type difference_type;
@ -222,30 +223,22 @@ public:
typedef vector_type::reverse_iterator reverse_iterator; typedef vector_type::reverse_iterator reverse_iterator;
explicit CDataStream(int nTypeIn, int nVersionIn) explicit CDataStream(int nTypeIn, int nVersionIn)
{ : nType{nTypeIn},
Init(nTypeIn, nVersionIn); nVersion{nVersionIn} {}
}
explicit CDataStream(Span<const uint8_t> sp, int nTypeIn, int nVersionIn) explicit CDataStream(Span<const uint8_t> sp, int nTypeIn, int nVersionIn)
: vch(sp.data(), sp.data() + sp.size()) : vch(sp.data(), sp.data() + sp.size()),
{ nType{nTypeIn},
Init(nTypeIn, nVersionIn); nVersion{nVersionIn} {}
}
template <typename... Args> template <typename... Args>
CDataStream(int nTypeIn, int nVersionIn, Args&&... args) CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
: nType{nTypeIn},
nVersion{nVersionIn}
{ {
Init(nTypeIn, nVersionIn);
::SerializeMany(*this, std::forward<Args>(args)...); ::SerializeMany(*this, std::forward<Args>(args)...);
} }
void Init(int nTypeIn, int nVersionIn)
{
nReadPos = 0;
nType = nTypeIn;
nVersion = nVersionIn;
}
std::string str() const std::string str() const
{ {
return (std::string(begin(), end())); return (std::string(begin(), end()));
@ -342,12 +335,17 @@ public:
nReadPos = 0; 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 // Rewind by n characters if the buffer hasn't been compacted yet
if (n > nReadPos) if (*n > nReadPos)
return false; return false;
nReadPos -= n; nReadPos -= *n;
return true; return true;
} }