core/vsl/vsl_set_io.txx
Go to the documentation of this file.
00001 // This is core/vsl/vsl_set_io.txx
00002 #ifndef vsl_set_io_txx_
00003 #define vsl_set_io_txx_
00004 //:
00005 // \file
00006 // \brief  binary IO functions for vcl_set<T>
00007 // \author K.Y.McGaul
00008 
00009 #include "vsl_set_io.h"
00010 #include <vsl/vsl_binary_io.h>
00011 
00012 //====================================================================================
00013 //: Write set to binary stream
00014 template <class T>
00015 void vsl_b_write(vsl_b_ostream& s, const vcl_set<T>& v)
00016 {
00017   const short version_no = 1;
00018   vsl_b_write(s, version_no);
00019   vsl_b_write(s, v.size());
00020   for (typename vcl_set<T>::const_iterator iter = v.begin(); iter != v.end(); iter++)
00021     vsl_b_write(s,*iter);
00022 }
00023 
00024 //====================================================================================
00025 //: Read set from binary stream
00026 template <class T>
00027 void vsl_b_read(vsl_b_istream& is, vcl_set<T>& v)
00028 {
00029   if (!is) return;
00030 
00031   v.clear();
00032   unsigned set_size;
00033   short ver;
00034   vsl_b_read(is, ver);
00035   switch (ver)
00036   {
00037   case 1:
00038     vsl_b_read(is, set_size);
00039     for (unsigned i=0; i<set_size; i++)
00040     {
00041       T tmp;
00042       vsl_b_read(is,tmp);
00043       v.insert(tmp);
00044     }
00045     break;
00046   default:
00047     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vcl_set<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_set<T> &v)
00058 {
00059   os << "Set length: " << v.size() << '\n';
00060   unsigned i =0;
00061   for (typename vcl_set<T>::const_iterator iter = v.begin();
00062        iter != v.end() && i<5; ++iter,++i)
00063   {
00064     os << ' ' << i << ": ";
00065     vsl_print_summary(os, *iter);
00066     os << '\n';
00067   }
00068   if (v.size() > 5)
00069     os << " ...\n";
00070 }
00071 
00072 
00073 #define VSL_SET_IO_INSTANTIATE(T) \
00074 template void vsl_print_summary(vcl_ostream& s, const vcl_set<T >& v); \
00075 template void vsl_b_write(vsl_b_ostream& s, const vcl_set<T >& v); \
00076 template void vsl_b_read(vsl_b_istream& s, vcl_set<T >& v)
00077 
00078 #endif // vsl_set_io_txx_