Skip to content

Commit faf4aa2

Browse files
author
MarcoFalke
committed
Remove CDataStream::Init in favor of C++11 member initialization
1 parent fada14b commit faf4aa2

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

src/bench/prevector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static void PrevectorDeserialize(benchmark::Bench& bench)
8484
for (auto x = 0; x < 1000; ++x) {
8585
s0 >> t1;
8686
}
87-
s0.Init(SER_NETWORK, 0);
87+
s0.Rewind();
8888
});
8989
}
9090

src/streams.h

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <assert.h>
1515
#include <ios>
1616
#include <limits>
17+
#include <optional>
1718
#include <stdint.h>
1819
#include <stdio.h>
1920
#include <string.h>
@@ -205,12 +206,12 @@ class CDataStream
205206
protected:
206207
using vector_type = SerializeData;
207208
vector_type vch;
208-
unsigned int nReadPos;
209+
unsigned int nReadPos{0};
209210

210211
int nType;
211212
int nVersion;
212-
public:
213213

214+
public:
214215
typedef vector_type::allocator_type allocator_type;
215216
typedef vector_type::size_type size_type;
216217
typedef vector_type::difference_type difference_type;
@@ -222,30 +223,22 @@ class CDataStream
222223
typedef vector_type::reverse_iterator reverse_iterator;
223224

224225
explicit CDataStream(int nTypeIn, int nVersionIn)
225-
{
226-
Init(nTypeIn, nVersionIn);
227-
}
226+
: nType{nTypeIn},
227+
nVersion{nVersionIn} {}
228228

229229
explicit CDataStream(Span<const uint8_t> sp, int nTypeIn, int nVersionIn)
230-
: vch(sp.data(), sp.data() + sp.size())
231-
{
232-
Init(nTypeIn, nVersionIn);
233-
}
230+
: vch(sp.data(), sp.data() + sp.size()),
231+
nType{nTypeIn},
232+
nVersion{nVersionIn} {}
234233

235234
template <typename... Args>
236235
CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
236+
: nType{nTypeIn},
237+
nVersion{nVersionIn}
237238
{
238-
Init(nTypeIn, nVersionIn);
239239
::SerializeMany(*this, std::forward<Args>(args)...);
240240
}
241241

242-
void Init(int nTypeIn, int nVersionIn)
243-
{
244-
nReadPos = 0;
245-
nType = nTypeIn;
246-
nVersion = nVersionIn;
247-
}
248-
249242
std::string str() const
250243
{
251244
return (std::string(begin(), end()));
@@ -342,12 +335,17 @@ class CDataStream
342335
nReadPos = 0;
343336
}
344337

345-
bool Rewind(size_type n)
338+
bool Rewind(std::optional<size_type> n = std::nullopt)
346339
{
340+
// Total rewind if no size is passed
341+
if (!n) {
342+
nReadPos = 0;
343+
return true;
344+
}
347345
// Rewind by n characters if the buffer hasn't been compacted yet
348-
if (n > nReadPos)
346+
if (*n > nReadPos)
349347
return false;
350-
nReadPos -= n;
348+
nReadPos -= *n;
351349
return true;
352350
}
353351

0 commit comments

Comments
 (0)