MyraMath
VectorStream.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_IO_VECTORSTREAM_H
7 #define MYRAMATH_IO_VECTORSTREAM_H
8 
14 #include <myramath/MYRAMATH_EXPORT.h>
15 #include <myramath/io/Streams.h>
16 
17 #include <vector>
18 
19 namespace myra {
20 
22 class VectorStream : public InputStream, public OutputStream
23  {
24  public:
25 
28  : i(0) {}
29 
31  virtual void write_binary (const char* p, size_t n)
32  { while(n--) buffer.push_back(*p++); }
33 
35  virtual void read_binary (char* p, size_t n)
36  { while(n--) *p++ = buffer.at(i++); }
37 
39  void swap(std::vector<char>& that)
40  { that.swap(buffer); }
41 
42  private:
43 
44  // Internal buffer, populated via write().
45  std::vector<char> buffer;
46 
47  // Internal pointer, advanced via read().
48  size_t i;
49 
50  };
51 
52 } // namespace
53 
54 #endif
virtual void read_binary(char *p, size_t n)
Reads n bytes from buffer, writing into p.
Definition: VectorStream.h:35
virtual void write_binary(const char *p, size_t n)
Writes n bytes into buffer, using push_back()
Definition: VectorStream.h:31
VectorStream()
Default constructs an empty VectorStream.
Definition: VectorStream.h:27
Wraps a std::vector<char>, presents it as both an InputStream and OutputStream. Useful for hygienic u...
Definition: VectorStream.h:22
Definition: syntax.dox:1
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
void swap(std::vector< char > &that)
Swap internal contents into that, overwriting it.
Definition: VectorStream.h:39
Abstraction layer, deserializable objects read themselves from these.
Definition: Streams.h:47
Bases classes for binary input/output streams.