binaryIO.cc
Go to the documentation of this file.
/* binaryIO.cc
*/
#include "osl/bits/binaryIO.h"
#include "osl/oslConfig.h"
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <iostream>
namespace osl
{
namespace
{
const size_t split_limit = 8*1024;
template <class T>
void write_vector(std::ostream& os, const std::vector<T>& data)
{
boost::iostreams::filtering_streambuf<boost::iostreams::output> filter;
filter.push(boost::iostreams::bzip2_compressor());
filter.push(os);
std::ostream out(&filter);
boost::archive::text_oarchive oa(out);
if (data.size() <= split_limit) {
oa << data;
}
else {
for (size_t p=0; p<data.size(); p+=split_limit) {
std::vector<T> tmp(data.begin()+p,
data.begin()+std::min(p+split_limit,
data.size()));
oa << tmp;
}
}
}
}
}
void osl::misc::BinaryWriter::
write(std::ostream& os, const std::vector<int>& data)
{
write_vector(os, data);
}
void osl::misc::BinaryWriter::
write(std::ostream& os, const std::vector<double>& data)
{
write_vector(os, data);
}
␌
template <class T>
osl::misc::BinaryReader<T>::BinaryReader(std::istream& is)
: state(new State(is))
{
}
template <class T>
osl::misc::BinaryReader<T>::~BinaryReader()
{
}
template <class T>
struct osl::misc::BinaryReader<T>::State
{
boost::iostreams::filtering_streambuf<boost::iostreams::input> filter;
std::unique_ptr<std::istream> in;
std::unique_ptr<boost::archive::text_iarchive> ia;
explicit State(std::istream &is)
{
if (!is)
return;
filter.push(boost::iostreams::bzip2_decompressor());
filter.push(is);
in.reset(new std::istream(&filter));
ia.reset(new boost::archive::text_iarchive(*in));
}
bool read_vector(std::vector<T>& data)
{
if (! in || ! *in)
return false;
(*ia) >> data;
return static_cast<bool>(*in);
}
};
template <class T>
bool osl::misc::BinaryReader<T>::
read(std::vector<T>& data)
{
return state->read_vector(data);
}
template <class T>
size_t osl::misc::BinaryReader<T>::blockSize()
{
return split_limit;
}
␌
template <class T>
struct osl::misc::BinaryElementReader<T>::State
{
BinaryReader<T> reader;
std::vector<T> data;
size_t cur;
bool failed;
explicit State(std::istream& is) : reader(is), cur(0), failed(!is)
{
}
bool hasNext()
{
tryRead();
return cur < data.size();
}
T read()
{
if (! hasNext())
throw std::logic_error("no data in BinaryReader::read");
return data[cur++];
}
void tryRead()
{
if (cur < data.size())
return;
data.clear();
cur = 0;
try {
failed = ! reader.read(data);
} catch (boost::archive::archive_exception& e) {
if (OslConfig::verbose() || 1)
std::cerr << "read failed in BinaryReader " << e.what();
cur = data.size();
failed = true;
}
}
};
template <class T>
osl::misc::BinaryElementReader<T>::BinaryElementReader(std::istream& is)
: state(new State(is))
{
}
template <class T>
osl::misc::BinaryElementReader<T>::~BinaryElementReader()
{
}
template <class T>
bool osl::misc::BinaryElementReader<T>::
hasNext() const
{
return state->hasNext();
}
template <class T>
bool osl::misc::BinaryElementReader<T>::
failed() const
{
return state->failed;
}
template <class T>
T osl::misc::BinaryElementReader<T>::read()
{
return state->read();
}
␌
template class osl::misc::BinaryReader<int>;
template class osl::misc::BinaryReader<double>;
template class osl::misc::BinaryElementReader<int>;
template class osl::misc::BinaryElementReader<double>;
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
binaryIO.h
osl::misc::BinaryElementReader
Definition binaryIO.h:34
osl::misc::BinaryElementReader::BinaryElementReader
BinaryElementReader(std::istream &is)
Definition binaryIO.cc:141
osl::misc::BinaryElementReader::read
T read()
Definition binaryIO.cc:162
osl::misc::BinaryElementReader::hasNext
bool hasNext() const
Definition binaryIO.cc:151
osl::misc::BinaryElementReader::state
std::unique_ptr< State > state
Definition binaryIO.h:44
osl::misc::BinaryElementReader::failed
bool failed() const
Definition binaryIO.cc:157
osl::misc::BinaryElementReader::~BinaryElementReader
~BinaryElementReader()
Definition binaryIO.cc:146
osl::misc::BinaryReader
Definition binaryIO.h:20
osl::misc::BinaryReader::~BinaryReader
~BinaryReader()
Definition binaryIO.cc:60
osl::misc::BinaryReader::state
std::unique_ptr< State > state
Definition binaryIO.h:29
osl::misc::BinaryReader::blockSize
static size_t blockSize()
Definition binaryIO.cc:96
osl::misc::BinaryReader::BinaryReader
BinaryReader(std::istream &is)
Definition binaryIO.cc:55
osl::misc::BinaryReader::read
bool read(std::vector< T > &data)
Definition binaryIO.cc:90
osl
Definition additionalEffect.h:6
oslConfig.h
osl::OslConfig::verbose
static bool verbose()
Definition oslConfig.cc:145
osl::misc::BinaryElementReader::State
Definition binaryIO.cc:104
osl::misc::BinaryElementReader::State::hasNext
bool hasNext()
Definition binaryIO.cc:112
osl::misc::BinaryElementReader::State::tryRead
void tryRead()
Definition binaryIO.cc:123
osl::misc::BinaryElementReader::State::reader
BinaryReader< T > reader
Definition binaryIO.cc:105
osl::misc::BinaryElementReader::State::State
State(std::istream &is)
Definition binaryIO.cc:109
osl::misc::BinaryElementReader::State::cur
size_t cur
Definition binaryIO.cc:107
osl::misc::BinaryElementReader::State::data
std::vector< T > data
Definition binaryIO.cc:106
osl::misc::BinaryElementReader::State::failed
bool failed
Definition binaryIO.cc:108
osl::misc::BinaryElementReader::State::read
T read()
Definition binaryIO.cc:117
osl::misc::BinaryReader::State
Definition binaryIO.cc:66
osl::misc::BinaryReader::State::filter
boost::iostreams::filtering_streambuf< boost::iostreams::input > filter
Definition binaryIO.cc:67
osl::misc::BinaryReader::State::State
State(std::istream &is)
Definition binaryIO.cc:70
osl::misc::BinaryReader::State::read_vector
bool read_vector(std::vector< T > &data)
Definition binaryIO.cc:79
osl::misc::BinaryReader::State::in
std::unique_ptr< std::istream > in
Definition binaryIO.cc:68
osl::misc::BinaryReader::State::ia
std::unique_ptr< boost::archive::text_iarchive > ia
Definition binaryIO.cc:69
osl::misc::BinaryWriter::write
static void write(std::ostream &, const std::vector< int > &data)
Definition binaryIO.cc:43