My Project
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
13namespace 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
43write(std::ostream& os, const std::vector<int>& data)
44{
45 write_vector(os, data);
46}
48write(std::ostream& os, const std::vector<double>& data)
49{
50 write_vector(os, data);
51}
52
53
54template <class T>
56 : state(new State(is))
57{
58}
59template <class T>
63
64template <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
88template <class T>
90read(std::vector<T>& data)
91{
92 return state->read_vector(data);
93}
94
95template <class T>
97{
98 return split_limit;
99}
100
101
102template <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 }
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
140template <class T>
142 : state(new State(is))
143{
144}
145template <class T>
149template <class T>
151hasNext() const
152{
153 return state->hasNext();
154}
155template <class T>
157failed() const
158{
159 return state->failed;
160}
161template <class T>
163{
164 return state->read();
165}
166
167
168template class osl::misc::BinaryReader<int>;
172
173// ;;; Local Variables:
174// ;;; mode:c++
175// ;;; c-basic-offset:2
176// ;;; End:
BinaryElementReader(std::istream &is)
Definition binaryIO.cc:141
static size_t blockSize()
Definition binaryIO.cc:96
BinaryReader(std::istream &is)
Definition binaryIO.cc:55
bool read(std::vector< T > &data)
Definition binaryIO.cc:90
static bool verbose()
Definition oslConfig.cc:145
boost::iostreams::filtering_streambuf< boost::iostreams::input > filter
Definition binaryIO.cc:67
State(std::istream &is)
Definition binaryIO.cc:70
bool read_vector(std::vector< T > &data)
Definition binaryIO.cc:79
std::unique_ptr< std::istream > in
Definition binaryIO.cc:68
std::unique_ptr< boost::archive::text_iarchive > ia
Definition binaryIO.cc:69
static void write(std::ostream &, const std::vector< int > &data)
Definition binaryIO.cc:43