core/vsl/vsl_string_io.txx
Go to the documentation of this file.
00001 // This is core/vsl/vsl_string_io.txx
00002 #ifndef vsl_string_io_txx_
00003 #define vsl_string_io_txx_
00004 //:
00005 // \file
00006 // \brief  binary IO functions for vcl_basic_string<T>
00007 // \author K.Y.McGaul
00008 
00009 #include "vsl_string_io.h"
00010 #include <vsl/vsl_binary_io.h>
00011 
00012 //====================================================================================
00013 //: Write basic_string to binary stream
00014 template <class T>
00015 void vsl_b_write(vsl_b_ostream& s, const vcl_basic_string<T>& v)
00016 {
00017   const short version_no = 1;
00018   vsl_b_write(s, version_no);
00019   unsigned n = v.length();
00020   vsl_b_write(s,n);
00021   for (int i=0;i<n;++i)
00022     vsl_b_write(s,v[i]);
00023 }
00024 
00025 //====================================================================================
00026 //: Read basic_string from binary stream
00027 template <class T>
00028 void vsl_b_read(vsl_b_istream& is, vcl_basic_string<T>& v)
00029 {
00030   if (!is) return;
00031 
00032   unsigned n;
00033   short ver;
00034   vsl_b_read(is, ver);
00035   switch (ver)
00036   {
00037    case 1:
00038     vsl_b_read(is,n);
00039     v.resize(n);
00040     for (int i=0;i<n;++i)
00041       vsl_b_read(is,v[i]);
00042     break;
00043    default:
00044     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vcl_basic_string<T>&)\n"
00045              << "           Unknown version number "<< ver << '\n';
00046     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00047     return;
00048   }
00049 }
00050 
00051 
00052 #define VSL_STRING_IO_INSTANTIATE(T) \
00053 template void vsl_b_write(vsl_b_ostream& s, const vcl_basic_string<T >& v); \
00054 template void vsl_b_read(vsl_b_istream& s, vcl_basic_string<T >& v)
00055 
00056 #endif // vsl_string_io_txx_