vnl_io_vector_fixed.hxx
Go to the documentation of this file.
1 // This is core/vnl/io/vnl_io_vector_fixed.hxx
2 #ifndef vnl_io_vector_fixed_hxx_
3 #define vnl_io_vector_fixed_hxx_
4 //:
5 // \file
6 
7 #include "vnl_io_vector_fixed.h"
8 #include <vsl/vsl_binary_io.h>
9 #include <vsl/vsl_b_read_block_old.h>
10 #include <vsl/vsl_block_binary.h>
11 
12 //=================================================================================
13 //: Binary save self to stream.
14 template<class T, unsigned int n>
15 void vsl_b_write(vsl_b_ostream & os, const vnl_vector_fixed<T,n> & p)
16 {
17  constexpr short io_version_no = 2;
18  vsl_b_write(os, io_version_no);
19  vsl_b_write(os, p.size());
20  if (p.size())
21  vsl_block_binary_write(os, p.data_block(), p.size());
22 }
23 
24 //=================================================================================
25 //: Binary load self from stream.
26 template<class T, unsigned int n>
27 void vsl_b_read(vsl_b_istream &is, vnl_vector_fixed<T,n> & p)
28 {
29  if (!is) return;
30 
31  short ver;
32  unsigned stream_n;
33  vsl_b_read(is, ver);
34  switch (ver)
35  {
36  case 1:
37  vsl_b_read(is, stream_n);
38  if ( n == stream_n ) {
39  vsl_b_read_block_old(is, p.begin(), n);
40  } else {
41  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_vector_fixed<T,n>&)\n"
42  << " Expected n="<<n<<", got "<<stream_n<<'\n';
43  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
44  return;
45  }
46  break;
47 
48  case 2:
49  vsl_b_read(is, stream_n);
50  if ( n == stream_n ) {
51  vsl_block_binary_read(is, p.data_block(), n);
52  } else {
53  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_vector_fixed<T,n>&)\n"
54  << " Expected n="<<n<<", got "<<stream_n<<'\n';
55  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
56  return;
57  }
58  break;
59 
60  default:
61  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vnl_vector_fixed<T,n>&)\n"
62  << " Unknown version number "<< ver << '\n';
63  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
64  return;
65  }
66 }
67 
68 //====================================================================================
69 //: Output a human readable summary to the stream
70 template<class T, unsigned int n>
71 void vsl_print_summary(std::ostream & os,const vnl_vector_fixed<T,n> & p)
72 {
73  os<<"Len: "<<p.size()<<" [fixed] (";
74  for ( unsigned int i =0; i < p.size() && i < 5; ++i )
75  os << p(i) <<' ';
76  if (p.size() > 5) os << " ...";
77  os << ')';
78 }
79 
80 #define VNL_IO_VECTOR_FIXED_INSTANTIATE(T,n) \
81 template void vsl_print_summary(std::ostream &, const vnl_vector_fixed<T,n > &); \
82 template void vsl_b_read(vsl_b_istream &, vnl_vector_fixed<T,n > &); \
83 template void vsl_b_write(vsl_b_ostream &, const vnl_vector_fixed<T,n > &)
84 
85 #endif // vnl_io_vector_fixed_hxx_
void vsl_print_summary(std::ostream &os, vnl_bignum const &b)
Print human readable summary of object to a stream.
unsigned int size() const
Length of the vector.
void vsl_b_read(vsl_b_istream &is, vnl_bignum &v)
Binary load vnl_bignum from stream.
void vsl_b_write(vsl_b_ostream &os, vnl_bignum const &v)
Binary save vnl_bignum to stream.
T const * data_block() const
Access the contiguous block storing the elements in the vector.
Fixed length stack-stored, space-efficient vector.
Definition: vnl_fwd.h:22
iterator begin()
Iterator pointing to start of data.