All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
binaryIO.cc
Go to the documentation of this file.
1 /* binaryIO.cc
2  */
3 #include "osl/misc/binaryIO.h"
4 #include "osl/oslConfig.h"
5 #include <boost/serialization/serialization.hpp>
6 #include <boost/serialization/vector.hpp>
7 #include <boost/archive/text_iarchive.hpp>
8 #include <boost/archive/text_oarchive.hpp>
9 #include <boost/iostreams/filtering_streambuf.hpp>
10 #include <boost/iostreams/filter/bzip2.hpp>
11 #include <iostream>
12 
13 namespace osl
14 {
15  namespace
16  {
17  const size_t split_limit = 8*1024;
18  template <class T>
19  void write_vector(std::ostream& os, const std::vector<T>& data)
20  {
21  boost::iostreams::filtering_streambuf<boost::iostreams::output> filter;
22  filter.push(boost::iostreams::bzip2_compressor());
23  filter.push(os);
24  std::ostream out(&filter);
25 
26  boost::archive::text_oarchive oa(out);
27  if (data.size() <= split_limit) {
28  oa << data;
29  }
30  else {
31  for (size_t p=0; p<data.size(); p+=split_limit) {
32  std::vector<T> tmp(data.begin()+p,
33  data.begin()+std::min(p+split_limit,
34  data.size()));
35  oa << tmp;
36  }
37  }
38  }
39  }
40 }
41 
43 write(std::ostream& os, const std::vector<int>& data)
44 {
45  write_vector(os, data);
46 }
48 write(std::ostream& os, const std::vector<double>& data)
49 {
50  write_vector(os, data);
51 }
52 
53 
54 template <class T>
56  : state(new State(is))
57 {
58 }
59 template <class T>
61 {
62 }
63 
64 template <class T>
66 {
67  boost::iostreams::filtering_streambuf<boost::iostreams::input> filter;
68  boost::scoped_ptr<std::istream> in;
69  boost::scoped_ptr<boost::archive::text_iarchive> ia;
70  explicit State(std::istream &is)
71  {
72  if (!is)
73  return;
74  filter.push(boost::iostreams::bzip2_decompressor());
75  filter.push(is);
76  in.reset(new std::istream(&filter));
77  ia.reset(new boost::archive::text_iarchive(*in));
78  }
79  bool read_vector(std::vector<T>& data)
80  {
81  if (! in || ! *in)
82  return false;
83  return (*ia) >> data, *in;
84  }
85 };
86 
87 template <class T>
89 read(std::vector<T>& data)
90 {
91  return state->read_vector(data);
92 }
93 
94 template <class T>
96 {
97  return split_limit;
98 }
99 
100 
101 template <class T>
103 {
105  std::vector<T> data;
106  size_t cur;
107  bool failed;
108  explicit State(std::istream& is) : reader(is), cur(0), failed(!is)
109  {
110  }
111  bool hasNext()
112  {
113  tryRead();
114  return cur < data.size();
115  }
116  T read()
117  {
118  if (! hasNext())
119  throw std::logic_error("no data in BinaryReader::read");
120  return data[cur++];
121  }
122  void tryRead()
123  {
124  if (cur < data.size())
125  return;
126  data.clear();
127  cur = 0;
128  try {
129  failed = ! reader.read(data);
130  } catch (boost::archive::archive_exception& e) {
131  if (OslConfig::verbose() || 1)
132  std::cerr << "read failed in BinaryReader " << e.what();
133  cur = data.size();
134  failed = true;
135  }
136  }
137 };
138 
139 template <class T>
141  : state(new State(is))
142 {
143 }
144 template <class T>
146 {
147 }
148 template <class T>
150 hasNext() const
151 {
152  return state->hasNext();
153 }
154 template <class T>
156 failed() const
157 {
158  return state->failed;
159 }
160 template <class T>
162 {
163  return state->read();
164 }
165 
166 
167 template class osl::misc::BinaryReader<int>;
168 template class osl::misc::BinaryReader<double>;
171 
172 // ;;; Local Variables:
173 // ;;; mode:c++
174 // ;;; c-basic-offset:2
175 // ;;; End: