binaryIO.cc
Go to the documentation of this file.
1 /* binaryIO.cc
2  */
3 #include "osl/bits/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  std::unique_ptr<std::istream> in;
69  std::unique_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  (*ia) >> data;
84  return static_cast<bool>(*in);
85  }
86 };
87 
88 template <class T>
90 read(std::vector<T>& data)
91 {
92  return state->read_vector(data);
93 }
94 
95 template <class T>
97 {
98  return split_limit;
99 }
100 
101 
102 template <class T>
104 {
106  std::vector<T> data;
107  size_t cur;
108  bool failed;
109  explicit State(std::istream& is) : reader(is), cur(0), failed(!is)
110  {
111  }
112  bool hasNext()
113  {
114  tryRead();
115  return cur < data.size();
116  }
117  T read()
118  {
119  if (! hasNext())
120  throw std::logic_error("no data in BinaryReader::read");
121  return data[cur++];
122  }
123  void tryRead()
124  {
125  if (cur < data.size())
126  return;
127  data.clear();
128  cur = 0;
129  try {
130  failed = ! reader.read(data);
131  } catch (boost::archive::archive_exception& e) {
132  if (OslConfig::verbose() || 1)
133  std::cerr << "read failed in BinaryReader " << e.what();
134  cur = data.size();
135  failed = true;
136  }
137  }
138 };
139 
140 template <class T>
142  : state(new State(is))
143 {
144 }
145 template <class T>
147 {
148 }
149 template <class T>
151 hasNext() const
152 {
153  return state->hasNext();
154 }
155 template <class T>
157 failed() const
158 {
159  return state->failed;
160 }
161 template <class T>
163 {
164  return state->read();
165 }
166 
167 
168 template class osl::misc::BinaryReader<int>;
169 template class osl::misc::BinaryReader<double>;
172 
173 // ;;; Local Variables:
174 // ;;; mode:c++
175 // ;;; c-basic-offset:2
176 // ;;; End:
BinaryReader(std::istream &is)
Definition: binaryIO.cc:55
boost::iostreams::filtering_streambuf< boost::iostreams::input > filter
Definition: binaryIO.cc:67
std::unique_ptr< State > state
Definition: binaryIO.h:28
bool read_vector(std::vector< T > &data)
Definition: binaryIO.cc:79
int min(Player p, int v1, int v2)
Definition: evalTraits.h:92
std::unique_ptr< std::istream > in
Definition: binaryIO.cc:68
static void write(std::ostream &, const std::vector< int > &data)
Definition: binaryIO.cc:43
static bool verbose()
Definition: oslConfig.cc:145
State(std::istream &is)
Definition: binaryIO.cc:70
BinaryElementReader(std::istream &is)
Definition: binaryIO.cc:141
std::unique_ptr< State > state
Definition: binaryIO.h:43
bool read(std::vector< T > &data)
Definition: binaryIO.cc:90
std::unique_ptr< boost::archive::text_iarchive > ia
Definition: binaryIO.cc:69
static size_t blockSize()
Definition: binaryIO.cc:96