aboutsummaryrefslogtreecommitdiff
path: root/src/streams.h
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2020-11-23 19:19:56 +0100
committerMarcoFalke <falke.marco@gmail.com>2020-12-31 09:05:51 +0100
commitfaf4aa2f47c0de4f3a0c5f5fe5b3ec32f611eefd (patch)
treedf1068b4006aeb8d7ba2a63ff697e39e22972c10 /src/streams.h
parentfada14b948cac147198e3b685b5dd8cb72dc2911 (diff)
Remove CDataStream::Init in favor of C++11 member initialization
Diffstat (limited to 'src/streams.h')
-rw-r--r--src/streams.h38
1 files changed, 18 insertions, 20 deletions
diff --git a/src/streams.h b/src/streams.h
index 4b34cbfd86..a6e781b95b 100644
--- a/src/streams.h
+++ b/src/streams.h
@@ -14,6 +14,7 @@
#include <assert.h>
#include <ios>
#include <limits>
+#include <optional>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -205,12 +206,12 @@ class CDataStream
protected:
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;
@@ -222,30 +223,22 @@ public:
typedef vector_type::reverse_iterator reverse_iterator;
explicit CDataStream(int nTypeIn, int nVersionIn)
- {
- Init(nTypeIn, nVersionIn);
- }
+ : nType{nTypeIn},
+ nVersion{nVersionIn} {}
explicit CDataStream(Span<const uint8_t> sp, int nTypeIn, int nVersionIn)
- : vch(sp.data(), sp.data() + sp.size())
- {
- Init(nTypeIn, 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;
- }
-
std::string str() const
{
return (std::string(begin(), end()));
@@ -342,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;
}