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