core/vsl/vsl_vector_io.txx

Go to the documentation of this file.
00001 // This is core/vsl/vsl_vector_io.txx
00002 #ifndef vsl_vector_io_txx_
00003 #define vsl_vector_io_txx_
00004 //:
00005 // \file
00006 // \brief binary IO functions for vcl_vector<T>
00007 // \author Tim Cootes
00008 
00009 #include "vsl_vector_io.h"
00010 #include <vsl/vsl_binary_io.h>
00011 #include <vsl/vsl_block_binary.h>
00012 #include <vsl/vsl_b_read_block_old.h>
00013 #include <vcl_iostream.h>
00014 #include <vcl_cassert.h>
00015 #include <vsl/vsl_indent.h>
00016 
00017 //====================================================================================
00018 //: Write vector to binary stream
00019 template <class T>
00020 void vsl_b_write(vsl_b_ostream& s, const vcl_vector<T>& v)
00021 {
00022   unsigned n = v.size();
00023   // There is nothing in the STL standard that says that vector<> has
00024   // to store its data in a contiguous memory block. However, most
00025   // implementations do store data this way.
00026   // Check this assumption holds.
00027   assert(n == 0 || &v[n-1] + 1 == &v[0] + n);
00028 
00029   const short version_no = 2;
00030   vsl_b_write(s, version_no);
00031   vsl_b_write(s,n);
00032   if (n!=0)
00033     vsl_block_binary_write(s, &v.front(), n);
00034 }
00035 
00036 //====================================================================================
00037 //: Read vector from binary stream
00038 template <class T>
00039 void vsl_b_read(vsl_b_istream& is, vcl_vector<T>& v)
00040 {
00041   if (!is) return;
00042 
00043   short ver;
00044   vsl_b_read(is, ver);
00045   unsigned n;
00046   vsl_b_read(is,n);
00047   v.resize(n); // Note that this resize means that the object must be default
00048                // constructable. It is very hard to see how to avoid this requirement,
00049                // without designing types that are constructable directly from a stream.
00050 
00051   // There is nothing in the STL standard that says that vector<> has
00052   // to store its data in a contiguous memory block. However, most
00053   // implementations do store data this way.
00054   // Check this assumption holds.
00055   assert(n == 0 || &v[n-1] + 1 == &v[0] + n);
00056 
00057   switch (ver)
00058   {
00059    case 1:
00060     if (n!=0)
00061     {
00062       vsl_b_read_block_old(is, &v.front(), n);
00063     }
00064     break;
00065    case 2:
00066     if (n!=0)
00067     {
00068       vsl_block_binary_read(is, &v.front(), n);
00069     }
00070     break;
00071 
00072    default:
00073     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vcl_vector<T>&)\n"
00074              << "           Unknown version number "<< ver << '\n';
00075     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00076     return;
00077   }
00078 }
00079 
00080 //====================================================================================
00081 //: Output a human readable summary to the stream
00082 template <class T>
00083 void vsl_print_summary(vcl_ostream& os, const vcl_vector<T> &v)
00084 {
00085   os << vsl_indent() << "Vector length: " << v.size() << vcl_endl;
00086   for (unsigned int i=0; i<v.size() && i<5; i++)
00087   {
00088     os << vsl_indent() << ' ' << i << ": ";
00089     vsl_indent_inc(os);
00090     vsl_print_summary(os, v[i]);
00091     os << vcl_endl;
00092     vsl_indent_dec(os);
00093   }
00094   if (v.size() > 5)
00095     os << vsl_indent() << " ...\n";
00096 }
00097 
00098 
00099 #define VSL_VECTOR_IO_INSTANTIATE(T) \
00100 template void vsl_print_summary(vcl_ostream& s, const vcl_vector<T >& v); \
00101 template void vsl_b_write(vsl_b_ostream& s, const vcl_vector<T >& v); \
00102 template void vsl_b_read(vsl_b_istream& s, vcl_vector<T >& v)
00103 
00104 #endif // vsl_vector_io_txx_

Generated on Sun Sep 7 05:05:55 2008 for core/vsl by  doxygen 1.5.1