vbl_io_array_1d.hxx
Go to the documentation of this file.
1 // This is core/vbl/io/vbl_io_array_1d.hxx
2 #ifndef vbl_io_array_1d_hxx_
3 #define vbl_io_array_1d_hxx_
4 //:
5 // \file
6 // \brief binary IO functions for vbl_array_1d<T>
7 // \author K.Y.McGaul
8 
9 #include <iostream>
10 #include "vbl_io_array_1d.h"
11 #include <vsl/vsl_binary_io.h>
12 #include <vbl/vbl_array_1d.h>
13 
14 //====================================================================
15 //: Binary save self to stream.
16 template<class T>
17 void vsl_b_write(vsl_b_ostream & os, const vbl_array_1d<T> & p)
18 {
19  constexpr short io_version_no = 1;
20  vsl_b_write(os, io_version_no);
21 
22  int array_size = (int)(p.size());
23  vsl_b_write(os, array_size);
24  int array_capacity = (int)(p.capacity());
25  vsl_b_write(os, array_capacity);
26  for (int i=0; i < array_size; ++i)
27  vsl_b_write(os, p[i]);
28 }
29 
30 //====================================================================
31 //: Binary load self from stream.
32 template<class T>
33 void vsl_b_read(vsl_b_istream &is, vbl_array_1d<T> & p)
34 {
35  if (!is) return;
36 
37  short ver;
38  int array_size;
39  int array_capacity;
40  T val;
41  vsl_b_read(is, ver);
42  switch (ver)
43  {
44  case 1:
45  vsl_b_read(is, array_size);
46  vsl_b_read(is, array_capacity);
47  p.reserve(array_capacity);
48  for (int i=0; i<array_size; ++i)
49  {
50  vsl_b_read(is, val);
51  p.push_back(val);
52  }
53  break;
54 
55  default:
56  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_array_1d<T>&)\n"
57  << " Unknown version number "<< ver << '\n';
58  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
59  return;
60  }
61 }
62 
63 
64 //===========================================================================
65 //: Output a human readable summary to the stream
66 template<class T>
67 void vsl_print_summary(std::ostream & os,const vbl_array_1d<T> & p)
68 {
69  os<<"Length: "<<p.size()<<std::endl;
70  for (unsigned int i =0; i < p.size() && i < 5; i++ )
71  {
72  os << ' ' << i << ": ";
73  vsl_print_summary(os, p[i]);
74  os << std::endl;
75  }
76  if (p.size() > 5)
77  os << " ...\n";
78 }
79 
80 #define VBL_IO_ARRAY_1D_INSTANTIATE(T) \
81 template void vsl_print_summary(std::ostream &, const vbl_array_1d<T > &); \
82 template void vsl_b_read(vsl_b_istream &, vbl_array_1d<T > &); \
83 template void vsl_b_write(vsl_b_ostream &, const vbl_array_1d<T > &)
84 
85 #endif // vbl_io_array_1d_hxx_
size_type size() const
Definition: vbl_array_1d.h:148
size_type capacity() const
Definition: vbl_array_1d.h:149
A simple container.
Definition: vbl_array_1d.h:28
void vsl_b_read(vsl_b_istream &is, vbl_array_1d< T > &p)
Binary load self from stream.
void vsl_b_write(vsl_b_ostream &os, const vbl_array_1d< T > &p)
Binary save self to stream.
A simple container.
void push_back(T const &x)
Definition: vbl_array_1d.h:117
void vsl_print_summary(std::ostream &os, const vbl_array_1d< T > &p)
Output a human readable summary to the stream.
void reserve(std::ptrdiff_t new_n)
Definition: vbl_array_1d.h:94