vsl_list_io.hxx
Go to the documentation of this file.
1 // This is core/vsl/vsl_list_io.hxx
2 #ifndef vsl_list_io_hxx_
3 #define vsl_list_io_hxx_
4 //:
5 // \file
6 // \brief binary IO functions for std::list<T>
7 // \author K.Y.McGaul
8 //
9 // Implementation
10 
11 #include <iostream>
12 #include "vsl_list_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 list to binary stream
21 template <class T>
22 void vsl_b_write(vsl_b_ostream& s, const std::list<T>& v)
23 {
24  constexpr short version_no = 1;
26  vsl_b_write(s, v.size());
27  for (typename std::list<T>::const_iterator iter = v.begin(); iter != v.end(); iter++)
28  vsl_b_write(s,*iter);
29 }
30 
31 //====================================================================================
32 //: Read list from binary stream
33 template <class T>
34 void vsl_b_read(vsl_b_istream& is, std::list<T>& v)
35 {
36  if (!is) return;
37 
38  v.clear();
39  unsigned list_size;
40  short ver;
41  vsl_b_read(is, ver);
42  switch (ver)
43  {
44  case 1:
45  vsl_b_read(is, list_size);
46  for (unsigned i=0; i<list_size; i++)
47  {
48  T tmp;
49  vsl_b_read(is,tmp);
50  v.push_back(tmp);
51  }
52  break;
53  default:
54  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, std::list<T>&)\n"
55  << " Unknown version number "<< ver << '\n';
56  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
57  return;
58  }
59 }
60 
61 //====================================================================================
62 //: Output a human readable summary to the stream
63 template <class T>
64 void vsl_print_summary(std::ostream& os, const std::list<T> &v)
65 {
66  unsigned i=0;
67  os << "List length: " << v.size() << '\n';
68  for (typename std::list<T>::const_iterator iter = v.begin();
69  iter != v.end() && i<5; ++iter,++i)
70  {
71  os << vsl_indent() << ' ' << i << ": ";
72  vsl_indent_inc(os);
73  vsl_print_summary(os, *iter);
74  os << '\n';
75  vsl_indent_dec(os);
76  }
77  if (v.size() > 5)
78  os << " ...\n";
79 }
80 
81 #define VSL_LIST_IO_INSTANTIATE(T) \
82 template void vsl_print_summary(std::ostream&, const std::list<T >&); \
83 template void vsl_b_write(vsl_b_ostream& s, const std::list<T >& v); \
84 template void vsl_b_read(vsl_b_istream& s, std::list<T >& v)
85 
86 #endif // vsl_list_io_hxx_
binary IO functions for std::list<T>
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_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::list< T > &v)
Output a human readable summary to the stream.
Definition: vsl_list_io.hxx:64
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
An adaptor for any std::istream to make it suitable for binary input.
Set of functions, and objects to perform binary IO.
void vsl_b_read(vsl_b_istream &is, std::list< T > &v)
Read list from binary stream.
Definition: vsl_list_io.hxx:34
void vsl_b_write(vsl_b_ostream &s, const std::list< T > &v)
Write list to binary stream.
Definition: vsl_list_io.hxx:22