core/vsl/vsl_stack_io.txx
Go to the documentation of this file.
00001 // This is core/vsl/vsl_stack_io.txx
00002 #ifndef vsl_stack_io_txx_
00003 #define vsl_stack_io_txx_
00004 //:
00005 // \file
00006 // \brief  binary IO functions for vcl_stack<T>
00007 // \author K.Y.McGaul
00008 
00009 #include "vsl_stack_io.h"
00010 #include <vsl/vsl_binary_io.h>
00011 #include <vsl/vsl_indent.h>
00012 
00013 //====================================================================================
00014 //: Write stack to binary stream
00015 template <class T>
00016 void vsl_b_write(vsl_b_ostream& s, const vcl_stack<T>& v)
00017 {
00018   const short version_no = 1;
00019   vsl_b_write(s, version_no);
00020   // Make a copy of v since we have to change a stack to get
00021   // the values out:
00022   vcl_stack<T> tmp_stack = v;
00023 
00024   unsigned int stack_size = (unsigned int)(v.size());
00025   vsl_b_write(s, stack_size);
00026   for (unsigned int i=0; i<stack_size; i++)
00027   {
00028     vsl_b_write(s,tmp_stack.top());
00029     tmp_stack.pop();
00030   }
00031 }
00032 
00033 //====================================================================================
00034 //: Read stack from binary stream
00035 template <class T>
00036 void vsl_b_read(vsl_b_istream& is, vcl_stack<T>& v)
00037 {
00038   if (!is) return;
00039 
00040   while (!v.empty()) v.pop(); // clear stack, which has no clear() member
00041 
00042   unsigned int stack_size;
00043   vcl_stack<T> tmp_stack;
00044   short ver;
00045   vsl_b_read(is, ver);
00046   switch (ver)
00047   {
00048   case 1:
00049     vsl_b_read(is, stack_size);
00050 
00051     // We need to reverse the order of the values before we load them
00052     // back into the stack, so use another temporary stack for this:
00053     for (unsigned int i=0; i<stack_size; i++)
00054     {
00055       T tmp;
00056       vsl_b_read(is,tmp);
00057       tmp_stack.push(tmp);
00058     }
00059     for (unsigned int i=0; i<stack_size; i++)
00060     {
00061       v.push(tmp_stack.top());
00062       tmp_stack.pop();
00063     }
00064     break;
00065   default:
00066     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vcl_stack<T>&)\n"
00067              << "           Unknown version number "<< ver << '\n';
00068     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00069     return;
00070   }
00071 }
00072 
00073 //====================================================================================
00074 //: Output a human readable summary to the stream
00075 template <class T>
00076 void vsl_print_summary(vcl_ostream& os, const vcl_stack<T> &v)
00077 {
00078   vcl_stack<T> tmp_stack = v;
00079   os << "Stack length: " << v.size() << '\n';
00080 
00081   unsigned int stack_size = (unsigned int)(v.size());
00082   for (unsigned int i=0; i<stack_size && i<5; i++)
00083   {
00084     os << vsl_indent() << ' ' << i << ": ";
00085     vsl_indent_inc(os);
00086     vsl_print_summary(os, tmp_stack.top());
00087     tmp_stack.pop();
00088     os << '\n';
00089     vsl_indent_dec(os);
00090   }
00091   if (stack_size > 5)
00092     os << " ...\n";
00093 }
00094 
00095 
00096 #define VSL_STACK_IO_INSTANTIATE(T) \
00097 template void vsl_print_summary(vcl_ostream& s, const vcl_stack<T >& v); \
00098 template void vsl_b_write(vsl_b_ostream& s, const vcl_stack<T >& v); \
00099 template void vsl_b_read(vsl_b_istream& s, vcl_stack<T >& v)
00100 
00101 #endif // vsl_stack_io_txx_