vsl_deque_io.hxx
Go to the documentation of this file.
1 // This is core/vsl/vsl_deque_io.hxx
2 #ifndef vsl_deque_io_hxx_
3 #define vsl_deque_io_hxx_
4 //:
5 // \file
6 // \brief binary IO functions for std::deque<T>
7 // \author K.Y.McGaul
8 //
9 // Implementation
10 
11 #include <iostream>
12 #include "vsl_deque_io.h"
13 #ifdef _MSC_VER
14 # include <vcl_msvc_warnings.h>
15 #endif
16 #include <vsl/vsl_binary_io.h>
17 #include <vsl/vsl_indent.h>
18 
19 //====================================================================================
20 //: Write deque to binary stream
21 template <class T>
22 void vsl_b_write(vsl_b_ostream& s, const std::deque<T>& v)
23 {
24  constexpr short version_no = 1;
26  vsl_b_write(s, v.size());
27  for (unsigned i=0; i<v.size(); i++)
28  vsl_b_write(s,v[i]);
29 }
30 
31 //====================================================================================
32 //: Read deque from binary stream
33 template <class T>
34 void vsl_b_read(vsl_b_istream& is, std::deque<T>& v)
35 {
36  if (!is) return;
37 
38  unsigned deque_size;
39  short ver;
40  vsl_b_read(is, ver);
41  switch (ver)
42  {
43  case 1:
44  vsl_b_read(is, deque_size);
45  v.resize(deque_size);
46  for (unsigned i=0; i<deque_size; i++)
47  vsl_b_read(is,v[i]);
48  break;
49  default:
50  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, std::deque<T>&)\n"
51  << " Unknown version number "<< ver << '\n';
52  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
53  return;
54  }
55 }
56 
57 //====================================================================================
58 //: Output a human readable summary to the stream
59 template <class T>
60 void vsl_print_summary(std::ostream& os, const std::deque<T> &v)
61 {
62  os << "Deque length: " << v.size() << '\n';
63  for (unsigned int i=0; i<v.size() && i<5; i++)
64  {
65  os << vsl_indent() << ' ' << i << ": ";
66  vsl_indent_inc(os);
67  vsl_print_summary(os,v[i]);
68  os << '\n';
69  vsl_indent_dec(os);
70  }
71  if (v.size() > 5)
72  os << " ...\n";
73 }
74 
75 
76 #define VSL_DEQUE_IO_INSTANTIATE(T) \
77 template void vsl_print_summary(std::ostream&, const std::deque<T >&); \
78 template void vsl_b_write(vsl_b_ostream& s, const std::deque<T >& v); \
79 template void vsl_b_read(vsl_b_istream& s, std::deque<T >& v)
80 
81 #endif // vsl_deque_io_hxx_
A binary output adaptor for any std::ostream.
Definition: vsl_binary_io.h:37
unsigned short version_no() const
Return the version number of the IO format of the file being read.
std::istream & is() const
A reference to the adaptor's stream.
void vsl_b_write(vsl_b_ostream &s, const std::deque< T > &v)
Write deque to binary stream.
void vsl_b_read(vsl_b_istream &is, std::deque< T > &v)
Read deque from binary stream.
void vsl_indent_inc(std::ostream &os)
Increments current indent for given stream.
Definition: vsl_indent.cxx:38
void vsl_print_summary(std::ostream &os, const std::deque< T > &v)
Output a human readable summary to the stream.
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
binary IO functions for std::deque<T>
An adaptor for any std::istream to make it suitable for binary input.
Set of functions, and objects to perform binary IO.