core/vbl/io/vbl_io_array_1d.txx
Go to the documentation of this file.
00001 // This is core/vbl/io/vbl_io_array_1d.txx
00002 #ifndef vbl_io_array_1d_txx_
00003 #define vbl_io_array_1d_txx_
00004 //:
00005 // \file
00006 // \brief  binary IO functions for vbl_array_1d<T>
00007 // \author K.Y.McGaul
00008 
00009 #include "vbl_io_array_1d.h"
00010 #include <vsl/vsl_binary_io.h>
00011 #include <vbl/vbl_array_1d.h>
00012 
00013 //====================================================================
00014 //: Binary save self to stream.
00015 template<class T>
00016 void vsl_b_write(vsl_b_ostream & os, const vbl_array_1d<T> & p)
00017 {
00018   const short io_version_no = 1;
00019   vsl_b_write(os, io_version_no);
00020 
00021   int array_size = (int)(p.size());
00022   vsl_b_write(os, array_size);
00023   int array_capacity = (int)(p.capacity());
00024   vsl_b_write(os, array_capacity);
00025   for (int i=0; i < array_size; ++i)
00026     vsl_b_write(os, p[i]);
00027 }
00028 
00029 //====================================================================
00030 //: Binary load self from stream.
00031 template<class T>
00032 void vsl_b_read(vsl_b_istream &is, vbl_array_1d<T> & p)
00033 {
00034   if (!is) return;
00035 
00036   short ver;
00037   int array_size;
00038   int array_capacity;
00039   T val;
00040   vsl_b_read(is, ver);
00041   switch (ver)
00042   {
00043    case 1:
00044     vsl_b_read(is, array_size);
00045     vsl_b_read(is, array_capacity);
00046     p.reserve(array_capacity);
00047     for (int i=0; i<array_size; ++i)
00048     {
00049       vsl_b_read(is, val);
00050       p.push_back(val);
00051     }
00052     break;
00053 
00054    default:
00055     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_array_1d<T>&)\n"
00056              << "           Unknown version number "<< ver << '\n';
00057     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00058     return;
00059   }
00060 }
00061 
00062 
00063 //===========================================================================
00064 //: Output a human readable summary to the stream
00065 template<class T>
00066 void vsl_print_summary(vcl_ostream & os,const vbl_array_1d<T> & p)
00067 {
00068   os<<"Length: "<<p.size()<<vcl_endl;
00069   for (unsigned int i =0; i < p.size() && i < 5; i++ )
00070   {
00071     os << ' ' << i << ": ";
00072     vsl_print_summary(os, p[i]);
00073     os << vcl_endl;
00074   }
00075   if (p.size() > 5)
00076     os << " ...\n";
00077 }
00078 
00079 #define VBL_IO_ARRAY_1D_INSTANTIATE(T) \
00080 template void vsl_print_summary(vcl_ostream &, const vbl_array_1d<T > &); \
00081 template void vsl_b_read(vsl_b_istream &, vbl_array_1d<T > &); \
00082 template void vsl_b_write(vsl_b_ostream &, const vbl_array_1d<T > &)
00083 
00084 #endif // vbl_io_array_1d_txx_