14
14
#include < assert.h>
15
15
#include < ios>
16
16
#include < limits>
17
+ #include < optional>
17
18
#include < stdint.h>
18
19
#include < stdio.h>
19
20
#include < string.h>
@@ -205,12 +206,12 @@ class CDataStream
205
206
protected:
206
207
using vector_type = SerializeData;
207
208
vector_type vch;
208
- unsigned int nReadPos;
209
+ unsigned int nReadPos{ 0 } ;
209
210
210
211
int nType;
211
212
int nVersion;
212
- public:
213
213
214
+ public:
214
215
typedef vector_type::allocator_type allocator_type;
215
216
typedef vector_type::size_type size_type;
216
217
typedef vector_type::difference_type difference_type;
@@ -222,30 +223,22 @@ class CDataStream
222
223
typedef vector_type::reverse_iterator reverse_iterator;
223
224
224
225
explicit CDataStream (int nTypeIn, int nVersionIn)
225
- {
226
- Init (nTypeIn, nVersionIn);
227
- }
226
+ : nType{nTypeIn},
227
+ nVersion{nVersionIn} {}
228
228
229
229
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} {}
234
233
235
234
template <typename ... Args>
236
235
CDataStream (int nTypeIn, int nVersionIn, Args&&... args)
236
+ : nType{nTypeIn},
237
+ nVersion{nVersionIn}
237
238
{
238
- Init (nTypeIn, nVersionIn);
239
239
::SerializeMany (*this , std::forward<Args>(args)...);
240
240
}
241
241
242
- void Init (int nTypeIn, int nVersionIn)
243
- {
244
- nReadPos = 0 ;
245
- nType = nTypeIn;
246
- nVersion = nVersionIn;
247
- }
248
-
249
242
std::string str () const
250
243
{
251
244
return (std::string (begin (), end ()));
@@ -342,12 +335,17 @@ class CDataStream
342
335
nReadPos = 0 ;
343
336
}
344
337
345
- bool Rewind (size_type n )
338
+ bool Rewind (std::optional< size_type> n = std::nullopt )
346
339
{
340
+ // Total rewind if no size is passed
341
+ if (!n) {
342
+ nReadPos = 0 ;
343
+ return true ;
344
+ }
347
345
// Rewind by n characters if the buffer hasn't been compacted yet
348
- if (n > nReadPos)
346
+ if (* n > nReadPos)
349
347
return false ;
350
- nReadPos -= n;
348
+ nReadPos -= * n;
351
349
return true ;
352
350
}
353
351
0 commit comments