core/vsl/vsl_vector_io_bool.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Specialised version of binary IO for vector<bool>
00004 // \author Ian Scott
00005 //
00006 
00007 #include "vsl_vector_io.h"
00008 #include <vsl/vsl_binary_io.h>
00009 #include <vcl_iostream.h>
00010 
00011 //====================================================================================
00012 //: Write vector to binary stream
00013 VCL_DEFINE_SPECIALIZATION
00014 void vsl_b_write(vsl_b_ostream& s, const vcl_vector<bool>& v)
00015 {
00016   const short version_no = 1;
00017   vsl_b_write(s, version_no);
00018   unsigned int n = (unsigned int)(v.size());
00019   vsl_b_write(s,n);
00020   for (unsigned int i=0; i<n; ++i)
00021     vsl_b_write(s, v[i]);
00022 }
00023 
00024 //====================================================================================
00025 //: Read vector from binary stream
00026 VCL_DEFINE_SPECIALIZATION
00027 void vsl_b_read(vsl_b_istream& is, vcl_vector<bool>& v)
00028 {
00029   if (!is) return;
00030 
00031   unsigned int n;
00032   short ver;
00033   vsl_b_read(is, ver);
00034   switch (ver)
00035   {
00036   case 1:
00037     vsl_b_read(is,n);
00038     v.resize(n);
00039     for (unsigned int i=0; i<n; ++i)
00040     {
00041       bool b;
00042       vsl_b_read(is, b);
00043       v[i] = b;
00044     }
00045     break;
00046   default:
00047     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vcl_vector<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 VCL_DEFINE_SPECIALIZATION
00057 void vsl_print_summary(vcl_ostream& os, const vcl_vector<bool> &v)
00058 {
00059   os << "Vector length: " << v.size() << '\n';
00060   for (unsigned int i=0; i<v.size() && i<5; i++)
00061   {
00062     os << ' ' << i << ": ";
00063     vsl_print_summary(os, v[i]);
00064     os << '\n';
00065   }
00066   if (v.size() > 5)
00067     os << " ..." << '\n';
00068 }
00069 
00070