vsl_string_io.hxx
Go to the documentation of this file.
1 // This is core/vsl/vsl_string_io.hxx
2 #ifndef vsl_string_io_hxx_
3 #define vsl_string_io_hxx_
4 //:
5 // \file
6 // \brief binary IO functions for std::basic_string<T>
7 // \author K.Y.McGaul
8 
9 #include "vsl_string_io.h"
10 #include <vsl/vsl_binary_io.h>
11 
12 //====================================================================================
13 //: Write basic_string to binary stream
14 template <class T>
15 void vsl_b_write(vsl_b_ostream& s, const std::basic_string<T>& v)
16 {
17  constexpr short version_no = 1;
19  unsigned n = v.length();
20  vsl_b_write(s,n);
21  for (int i=0;i<n;++i)
22  vsl_b_write(s,v[i]);
23 }
24 
25 //====================================================================================
26 //: Read basic_string from binary stream
27 template <class T>
28 void vsl_b_read(vsl_b_istream& is, std::basic_string<T>& v)
29 {
30  if (!is) return;
31 
32  unsigned n;
33  short ver;
34  vsl_b_read(is, ver);
35  switch (ver)
36  {
37  case 1:
38  vsl_b_read(is,n);
39  v.resize(n);
40  for (int i=0;i<n;++i)
41  vsl_b_read(is,v[i]);
42  break;
43  default:
44  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, std::basic_string<T>&)\n"
45  << " Unknown version number "<< ver << '\n';
46  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
47  return;
48  }
49 }
50 
51 
52 #define VSL_STRING_IO_INSTANTIATE(T) \
53 template void vsl_b_write(vsl_b_ostream& s, const std::basic_string<T >& v); \
54 template void vsl_b_read(vsl_b_istream& s, std::basic_string<T >& v)
55 
56 #endif // vsl_string_io_hxx_
A binary output adaptor for any std::ostream.
Definition: vsl_binary_io.h:37
void vsl_b_write(vsl_b_ostream &s, const std::basic_string< T > &v)
Write basic_string to binary stream.
unsigned short version_no() const
Return the version number of the IO format of the file being read.
void vsl_b_read(vsl_b_istream &is, std::basic_string< T > &v)
Read basic_string from binary stream.
std::istream & is() const
A reference to the adaptor's stream.
An adaptor for any std::istream to make it suitable for binary input.
binary IO functions for std::basic_string<T>
Set of functions, and objects to perform binary IO.