vsl_vector_io.hxx
Go to the documentation of this file.
1 // This is core/vsl/vsl_vector_io.hxx
2 #ifndef vsl_vector_io_hxx_
3 #define vsl_vector_io_hxx_
4 //:
5 // \file
6 // \brief binary IO functions for std::vector<T>
7 // \author Tim Cootes
8 
9 #include <iostream>
10 #include "vsl_vector_io.h"
11 #include <vsl/vsl_binary_io.h>
12 #include <vsl/vsl_block_binary.h>
14 #ifdef _MSC_VER
15 # include <vcl_msvc_warnings.h>
16 #endif
17 #include <cassert>
18 #include <vsl/vsl_indent.h>
19 
20 //====================================================================================
21 //: Write vector to binary stream
22 template <class T>
23 void vsl_b_write(vsl_b_ostream& s, const std::vector<T>& v)
24 {
25  std::size_t n = v.size();
26  // There is nothing in the STL standard that says that vector<> has
27  // to store its data in a contiguous memory block. However, most
28  // implementations do store data this way.
29  // Check this assumption holds.
30  assert(n == 0 || &v[n-1] + 1 == &v[0] + n);
31 
32  constexpr short version_no = 3;
34  vsl_b_write(s,n);
35  if (n!=0)
36  vsl_block_binary_write(s, &v.front(), n);
37 }
38 
39 
40 template <class T> bool vsl_is_char(const T&);
41 
42 template <> inline bool vsl_is_char(const unsigned char &)
43 { return true; }
44 template <> inline bool vsl_is_char(const signed char &)
45 { return true; }
46 template <class T> bool vsl_is_char(const T&) { return false; }
47 
48 //====================================================================================
49 //: Read vector from binary stream
50 template <class T>
51 void vsl_b_read(vsl_b_istream& is, std::vector<T>& v)
52 {
53  if (!is) return;
54 
55  short ver;
56  vsl_b_read(is, ver);
57  unsigned n;
58  vsl_b_read(is,n);
59  v.resize(n); // Note that this resize means that the object must be default
60  // constructable. It is very hard to see how to avoid this requirement,
61  // without designing types that are constructable directly from a stream.
62 
63  // In some old versions of the standard STL there is no requirement for
64  // vector<> to store its data in a contiguous memory block. However, most
65  // implementations do store data this way.
66  // Check this assumption holds.
67  assert(n == 0 || &v[n-1] + 1 == &v[0] + n);
68 
69  switch (ver)
70  {
71  case 1:
72  if (n!=0)
73  {
74  vsl_b_read_block_old(is, &v.front(), n);
75  }
76  break;
77  case 2:
78  if (n!=0)
79  {
80  if (vsl_is_char(v.front())) // signed char or unsigned char
81  {
83  vsl_b_read_block_old(is, &v.front(), n);
84  }
85  else
86  vsl_block_binary_read(is, &v.front(), n);
87  }
88  break;
89  case 3:
90  if (n!=0)
91  {
92  vsl_block_binary_read(is, &v.front(), n);
93  }
94  break;
95 
96 
97  default:
98  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, std::vector<T>&)\n"
99  << " Unknown version number "<< ver << '\n';
100  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
101  return;
102  }
103 }
104 
105 //====================================================================================
106 //: Output a human readable summary to the stream
107 template <class T>
108 void vsl_print_summary(std::ostream& os, const std::vector<T> &v)
109 {
110  os << vsl_indent() << "Vector length: " << v.size() << '\n';
111  for (unsigned int i=0; i<v.size() && i<5; i++)
112  {
113  os << vsl_indent() << ' ' << i << ": ";
114  vsl_indent_inc(os);
115  vsl_print_summary(os, v[i]);
116  os << '\n';
117  vsl_indent_dec(os);
118  }
119  if (v.size() > 5)
120  os << vsl_indent() << " ...\n";
121 }
122 
123 
124 #define VSL_VECTOR_IO_INSTANTIATE(T) \
125 template void vsl_print_summary(std::ostream& s, const std::vector<T >& v); \
126 template void vsl_b_write(vsl_b_ostream& s, const std::vector<T >& v); \
127 template void vsl_b_read(vsl_b_istream& s, std::vector<T >& v)
128 
129 #endif // vsl_vector_io_hxx_
A binary output adaptor for any std::ostream.
Definition: vsl_binary_io.h:37
Set of functions to do binary IO on a block of values.
void vsl_b_read(vsl_b_istream &is, std::vector< T > &v)
Read vector from binary stream.
unsigned short version_no() const
Return the version number of the IO format of the file being read.
void vsl_block_binary_read(vsl_b_istream &is, T *begin, std::size_t nelems)
Read a block of values from a vsl_b_ostream, potentially very efficiently for fundamental types.
std::istream & is() const
A reference to the adaptor's stream.
void vsl_block_binary_read_confirm_specialisation(vsl_b_istream &is, bool specialised)
Error checking.
void vsl_indent_inc(std::ostream &os)
Increments current indent for given stream.
Definition: vsl_indent.cxx:38
Backwards compatibility support only.
void vsl_b_write(vsl_b_ostream &s, const std::vector< T > &v)
Write vector to binary stream.
void vsl_b_read_block_old(vsl_b_istream &is, T *begin, std::size_t nelems)
Read a block of values from a vsl_b_istream.
bool vsl_is_char(const T &)
Put indents into output streams, to produce more legible printed output.
Definition: vsl_indent.h:88
void vsl_indent_dec(std::ostream &os)
Decrements current indent for given stream.
Definition: vsl_indent.cxx:44
void vsl_print_summary(std::ostream &os, const std::vector< T > &v)
Output a human readable summary to the stream.
An adaptor for any std::istream to make it suitable for binary input.
void vsl_block_binary_write(vsl_b_ostream &os, const T *begin, std::size_t nelems)
Write a block of values to a vsl_b_ostream, potentially very efficiently for fundamental types.
Set of functions, and objects to perform binary IO.
binary IO functions for std::vector<T>