MyraMath
iosize.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_IOSIZE_H
7 #define MYRAMATH_IO_IOSIZE_H
8 
14 #include <myramath/io/Streams.h>
15 #include <myramath/MYRAMATH_EXPORT.h>
16 #include <cstddef>
17 
18 namespace myra {
19 
22  {
23  public:
24 
27  : N(0) { }
28 
30  virtual void write_binary(const char* p, size_t n)
31  { N += n; }
32 
34  size_t size()
35  { return N; }
36 
37  private:
38 
39  // Accumulates size of all write()'s.
40  size_t N;
41 
42  };
43 
45 template<class T> size_t iosize(const T& t)
46  {
47  CountingStream stream;
48  stream << t;
49  return stream.size();
50  }
51 
52 } // namespace
53 
54 #endif
Definition: syntax.dox:1
CountingStream()
Constructor, initializes size() = 0.
Definition: iosize.h:26
size_t iosize(const T &t)
Returns the serialized size (in bytes) of a serializable type T.
Definition: iosize.h:45
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
size_t size()
Returns total size.
Definition: iosize.h:34
virtual void write_binary(const char *p, size_t n)
Accumulates n, but doesn&#39;t actually serialize anything.
Definition: iosize.h:30
An OutputStream that just counts up the size of everything you write() to it.
Definition: iosize.h:21
Bases classes for binary input/output streams.