vsl_stack_io.hxx
Go to the documentation of this file.
1 // This is core/vsl/vsl_stack_io.hxx
2 #ifndef vsl_stack_io_hxx_
3 #define vsl_stack_io_hxx_
4 //:
5 // \file
6 // \brief binary IO functions for std::stack<T>
7 // \author K.Y.McGaul
8 
9 #include <iostream>
10 #include "vsl_stack_io.h"
11 #include <vsl/vsl_binary_io.h>
12 #include <vsl/vsl_indent.h>
13 
14 //====================================================================================
15 //: Write stack to binary stream
16 template <class T>
17 void vsl_b_write(vsl_b_ostream& s, const std::stack<T>& v)
18 {
19  constexpr short version_no = 1;
21  // Make a copy of v since we have to change a stack to get
22  // the values out:
23  std::stack<T> tmp_stack = v;
24 
25  unsigned int stack_size = (unsigned int)(v.size());
26  vsl_b_write(s, stack_size);
27  for (unsigned int i=0; i<stack_size; i++)
28  {
29  vsl_b_write(s,tmp_stack.top());
30  tmp_stack.pop();
31  }
32 }
33 
34 //====================================================================================
35 //: Read stack from binary stream
36 template <class T>
37 void vsl_b_read(vsl_b_istream& is, std::stack<T>& v)
38 {
39  if (!is) return;
40 
41  while (!v.empty()) v.pop(); // clear stack, which has no clear() member
42 
43  unsigned int stack_size;
44  std::stack<T> tmp_stack;
45  short ver;
46  vsl_b_read(is, ver);
47  switch (ver)
48  {
49  case 1:
50  vsl_b_read(is, stack_size);
51 
52  // We need to reverse the order of the values before we load them
53  // back into the stack, so use another temporary stack for this:
54  for (unsigned int i=0; i<stack_size; i++)
55  {
56  T tmp;
57  vsl_b_read(is,tmp);
58  tmp_stack.push(tmp);
59  }
60  for (unsigned int i=0; i<stack_size; i++)
61  {
62  v.push(tmp_stack.top());
63  tmp_stack.pop();
64  }
65  break;
66  default:
67  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, std::stack<T>&)\n"
68  << " Unknown version number "<< ver << '\n';
69  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
70  return;
71  }
72 }
73 
74 //====================================================================================
75 //: Output a human readable summary to the stream
76 template <class T>
77 void vsl_print_summary(std::ostream& os, const std::stack<T> &v)
78 {
79  std::stack<T> tmp_stack = v;
80  os << "Stack length: " << v.size() << '\n';
81 
82  unsigned int stack_size = (unsigned int)(v.size());
83  for (unsigned int i=0; i<stack_size && i<5; i++)
84  {
85  os << vsl_indent() << ' ' << i << ": ";
86  vsl_indent_inc(os);
87  vsl_print_summary(os, tmp_stack.top());
88  tmp_stack.pop();
89  os << '\n';
90  vsl_indent_dec(os);
91  }
92  if (stack_size > 5)
93  os << " ...\n";
94 }
95 
96 
97 #define VSL_STACK_IO_INSTANTIATE(T) \
98 template void vsl_print_summary(std::ostream& s, const std::stack<T >& v); \
99 template void vsl_b_write(vsl_b_ostream& s, const std::stack<T >& v); \
100 template void vsl_b_read(vsl_b_istream& s, std::stack<T >& v)
101 
102 #endif // vsl_stack_io_hxx_
A binary output adaptor for any std::ostream.
Definition: vsl_binary_io.h:37
void vsl_print_summary(std::ostream &os, const std::stack< T > &v)
Output a human readable summary to the stream.
void vsl_b_write(vsl_b_ostream &s, const std::stack< T > &v)
Write stack to binary stream.
unsigned short version_no() const
Return the version number of the IO format of the file being read.
std::istream & is() const
A reference to the adaptor's stream.
void vsl_indent_inc(std::ostream &os)
Increments current indent for given stream.
Definition: vsl_indent.cxx:38
Put indents into output streams, to produce more legible printed output.
Definition: vsl_indent.h:88
void vsl_indent_dec(std::ostream &os)
Decrements current indent for given stream.
Definition: vsl_indent.cxx:44
An adaptor for any std::istream to make it suitable for binary input.
void vsl_b_read(vsl_b_istream &is, std::stack< T > &v)
Read stack from binary stream.
binary IO functions for std::stack<T>
Set of functions, and objects to perform binary IO.