core/vsl/vsl_list_io.txx
Go to the documentation of this file.
00001 // This is core/vsl/vsl_list_io.txx
00002 #ifndef vsl_list_io_txx_
00003 #define vsl_list_io_txx_
00004 //:
00005 // \file
00006 // \brief  binary IO functions for vcl_list<T>
00007 // \author K.Y.McGaul
00008 //
00009 // Implementation
00010 
00011 #include "vsl_list_io.h"
00012 #include <vcl_iostream.h>
00013 #include <vsl/vsl_binary_io.h>
00014 #include <vsl/vsl_indent.h>
00015 
00016 //====================================================================================
00017 //: Write list to binary stream
00018 template <class T>
00019 void vsl_b_write(vsl_b_ostream& s, const vcl_list<T>& v)
00020 {
00021   const short version_no = 1;
00022   vsl_b_write(s, version_no);
00023   vsl_b_write(s, v.size());
00024   for (typename vcl_list<T>::const_iterator iter = v.begin(); iter != v.end(); iter++)
00025     vsl_b_write(s,*iter);
00026 }
00027 
00028 //====================================================================================
00029 //: Read list from binary stream
00030 template <class T>
00031 void vsl_b_read(vsl_b_istream& is, vcl_list<T>& v)
00032 {
00033   if (!is) return;
00034 
00035   v.clear();
00036   unsigned list_size;
00037   short ver;
00038   vsl_b_read(is, ver);
00039   switch (ver)
00040   {
00041   case 1:
00042     vsl_b_read(is, list_size);
00043     for (unsigned i=0; i<list_size; i++)
00044     {
00045       T tmp;
00046       vsl_b_read(is,tmp);
00047       v.push_back(tmp);
00048     }
00049     break;
00050   default:
00051     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vcl_list<T>&)\n"
00052              << "           Unknown version number "<< ver << '\n';
00053     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00054     return;
00055   }
00056 }
00057 
00058 //====================================================================================
00059 //: Output a human readable summary to the stream
00060 template <class T>
00061 void vsl_print_summary(vcl_ostream& os, const vcl_list<T> &v)
00062 {
00063   unsigned i=0;
00064   os << "List length: " << v.size() << '\n';
00065   for (typename vcl_list<T>::const_iterator iter = v.begin();
00066        iter != v.end() && i<5; ++iter,++i)
00067   {
00068     os << vsl_indent() << ' ' << i << ": ";
00069     vsl_indent_inc(os);
00070     vsl_print_summary(os, *iter);
00071     os << '\n';
00072     vsl_indent_dec(os);
00073   }
00074   if (v.size() > 5)
00075     os << " ...\n";
00076 }
00077 
00078 #define VSL_LIST_IO_INSTANTIATE(T) \
00079 template void vsl_print_summary(vcl_ostream&, const vcl_list<T >&); \
00080 template void vsl_b_write(vsl_b_ostream& s, const vcl_list<T >& v); \
00081 template void vsl_b_read(vsl_b_istream& s, vcl_list<T >& v)
00082 
00083 #endif // vsl_list_io_txx_