@@ -182,16 +182,13 @@ class SpanReader
182
182
* >> and << read and write unformatted data using the above serialization templates.
183
183
* Fills with data in linear time; some stringstream implementations take N^2 time.
184
184
*/
185
- class CDataStream
185
+ class DataStream
186
186
{
187
187
protected:
188
188
using vector_type = SerializeData;
189
189
vector_type vch;
190
190
vector_type::size_type m_read_pos{0 };
191
191
192
- int nType;
193
- int nVersion;
194
-
195
192
public:
196
193
typedef vector_type::allocator_type allocator_type;
197
194
typedef vector_type::size_type size_type;
@@ -203,23 +200,9 @@ class CDataStream
203
200
typedef vector_type::const_iterator const_iterator;
204
201
typedef vector_type::reverse_iterator reverse_iterator;
205
202
206
- explicit CDataStream (int nTypeIn, int nVersionIn)
207
- : nType{nTypeIn},
208
- nVersion{nVersionIn} {}
209
-
210
- explicit CDataStream (Span<const uint8_t > sp, int type, int version) : CDataStream{AsBytes (sp), type, version} {}
211
- explicit CDataStream (Span<const value_type> sp, int nTypeIn, int nVersionIn)
212
- : vch(sp.data(), sp.data() + sp.size()),
213
- nType{nTypeIn},
214
- nVersion{nVersionIn} {}
215
-
216
- template <typename ... Args>
217
- CDataStream (int nTypeIn, int nVersionIn, Args&&... args)
218
- : nType{nTypeIn},
219
- nVersion{nVersionIn}
220
- {
221
- ::SerializeMany (*this , std::forward<Args>(args)...);
222
- }
203
+ explicit DataStream () {}
204
+ explicit DataStream (Span<const uint8_t > sp) : DataStream{AsBytes (sp)} {}
205
+ explicit DataStream (Span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {}
223
206
224
207
std::string str () const
225
208
{
@@ -271,19 +254,14 @@ class CDataStream
271
254
bool eof () const { return size () == 0 ; }
272
255
int in_avail () const { return size (); }
273
256
274
- void SetType (int n) { nType = n; }
275
- int GetType () const { return nType; }
276
- void SetVersion (int n) { nVersion = n; }
277
- int GetVersion () const { return nVersion; }
278
-
279
257
void read (Span<value_type> dst)
280
258
{
281
259
if (dst.size () == 0 ) return ;
282
260
283
261
// Read from the beginning of the buffer
284
262
auto next_read_pos{CheckedAdd (m_read_pos, dst.size ())};
285
263
if (!next_read_pos.has_value () || next_read_pos.value () > vch.size ()) {
286
- throw std::ios_base::failure (" CDataStream ::read(): end of data" );
264
+ throw std::ios_base::failure (" DataStream ::read(): end of data" );
287
265
}
288
266
memcpy (dst.data (), &vch[m_read_pos], dst.size ());
289
267
if (next_read_pos.value () == vch.size ()) {
@@ -299,7 +277,7 @@ class CDataStream
299
277
// Ignore from the beginning of the buffer
300
278
auto next_read_pos{CheckedAdd (m_read_pos, num_ignore)};
301
279
if (!next_read_pos.has_value () || next_read_pos.value () > vch.size ()) {
302
- throw std::ios_base::failure (" CDataStream ::ignore(): end of data" );
280
+ throw std::ios_base::failure (" DataStream ::ignore(): end of data" );
303
281
}
304
282
if (next_read_pos.value () == vch.size ()) {
305
283
m_read_pos = 0 ;
@@ -324,15 +302,15 @@ class CDataStream
324
302
}
325
303
326
304
template <typename T>
327
- CDataStream & operator <<(const T& obj)
305
+ DataStream & operator <<(const T& obj)
328
306
{
329
307
// Serialize to this stream
330
308
::Serialize (*this , obj);
331
309
return (*this );
332
310
}
333
311
334
312
template <typename T>
335
- CDataStream & operator >>(T&& obj)
313
+ DataStream & operator >>(T&& obj)
336
314
{
337
315
// Unserialize from this stream
338
316
::Unserialize (*this , obj);
@@ -363,6 +341,51 @@ class CDataStream
363
341
}
364
342
};
365
343
344
+ class CDataStream : public DataStream
345
+ {
346
+ private:
347
+ int nType;
348
+ int nVersion;
349
+
350
+ public:
351
+ explicit CDataStream (int nTypeIn, int nVersionIn)
352
+ : nType{nTypeIn},
353
+ nVersion{nVersionIn} {}
354
+
355
+ explicit CDataStream (Span<const uint8_t > sp, int type, int version) : CDataStream{AsBytes (sp), type, version} {}
356
+ explicit CDataStream (Span<const value_type> sp, int nTypeIn, int nVersionIn)
357
+ : DataStream{sp},
358
+ nType{nTypeIn},
359
+ nVersion{nVersionIn} {}
360
+
361
+ template <typename ... Args>
362
+ CDataStream (int nTypeIn, int nVersionIn, Args&&... args)
363
+ : nType{nTypeIn},
364
+ nVersion{nVersionIn}
365
+ {
366
+ ::SerializeMany (*this , std::forward<Args>(args)...);
367
+ }
368
+
369
+ void SetType (int n) { nType = n; }
370
+ int GetType () const { return nType; }
371
+ void SetVersion (int n) { nVersion = n; }
372
+ int GetVersion () const { return nVersion; }
373
+
374
+ template <typename T>
375
+ CDataStream& operator <<(const T& obj)
376
+ {
377
+ ::Serialize (*this , obj);
378
+ return *this ;
379
+ }
380
+
381
+ template <typename T>
382
+ CDataStream& operator >>(T&& obj)
383
+ {
384
+ ::Unserialize (*this , obj);
385
+ return *this ;
386
+ }
387
+ };
388
+
366
389
template <typename IStream>
367
390
class BitStreamReader
368
391
{
0 commit comments