12#ifndef _RD_STREAMOPS_H
13#define _RD_STREAMOPS_H
21#include <boost/cstdint.hpp>
22#include <boost/predef.h>
30#if defined(BOOST_ENDIAN_LITTLE_BYTE) || defined(BOOST_ENDIAN_LITTLE_WORD)
32#elif defined(BOOST_ENDIAN_BIG_BYTE)
34#elif defined(BOOST_ENDIAN_BIG_WORD)
35#error "Cannot compile on word-swapped big-endian systems"
37#error "Failed to determine the system endian value"
43template <
class T,
unsigned int size>
56 for (
unsigned int i = 0; i < size; ++i) {
57 out.bytes[i] =
in.bytes[size - 1 - i];
71template <EEndian from, EEndian to,
class T>
90template <EEndian from, EEndian to>
94template <EEndian from, EEndian to>
98template <EEndian from, EEndian to>
106 boost::uint32_t num) {
108 unsigned int val,
res;
113 if (
res < (1 << 7)) {
119 if (
res < (1 << 14)) {
120 val = ((
res << 2) | 1);
125 if (
res < (1 << 21)) {
126 val = ((
res << 3) | 3);
131 if (
res < (1 << 29)) {
132 val = ((
res << 3) | 7);
150 boost::uint32_t val, num;
155 throw std::runtime_error(
"failed to read from stream");
160 if ((val & 1) == 0) {
162 }
else if ((val & 3) == 1) {
165 throw std::runtime_error(
"failed to read from stream");
171 }
else if ((val & 7) == 3) {
174 throw std::runtime_error(
"failed to read from stream");
180 throw std::runtime_error(
"failed to read from stream");
185 offset = (1 << 7) + (1 << 14);
189 throw std::runtime_error(
"failed to read from stream");
195 throw std::runtime_error(
"failed to read from stream");
201 throw std::runtime_error(
"failed to read from stream");
206 offset = (1 << 7) + (1 << 14) + (1 << 21);
208 num = (val >>
shift) + offset;
216 boost::uint32_t val, num;
223 if ((val & 1) == 0) {
225 }
else if ((val & 3) == 1) {
231 }
else if ((val & 7) == 3) {
239 offset = (1 << 7) + (1 << 14);
251 offset = (1 << 7) + (1 << 14) + (1 << 21);
253 num = (val >>
shift) + offset;
262 ss.write((
const char *)&
tval,
sizeof(T));
268 ss.write((
const char *)&
l,
sizeof(
l));
269 ss.write(what.c_str(),
sizeof(
char) *
l);
275 for (
size_t i = 0; i < val.size(); ++i) {
284 ss.read((
char *)&
tloc,
sizeof(T));
286 throw std::runtime_error(
"failed to read from stream");
301 ss.read((
char *)&
l,
sizeof(
l));
303 throw std::runtime_error(
"failed to read from stream");
305 char *
buff =
new char[
l];
306 ss.read(
buff,
sizeof(
char) *
l);
308 throw std::runtime_error(
"failed to read from stream");
310 what = std::string(
buff,
l);
316 boost::uint64_t size;
318 val.resize(boost::numeric_cast<size_t>(size));
320 for (
size_t i = 0; i < size; ++i) {
327 boost::uint64_t size;
331 for (
size_t i = 0; i < size; ++i) {
340 if (!
res.empty() && (
res.back() ==
'\r')) {
341 res.resize(
res.length() - 1);
380typedef std::vector<std::shared_ptr<const CustomPropHandler>>
385 switch (
pair.val.getTag()) {
418 switch (
pair.val.getTag()) {
491 unsigned int count = 0;
492 for (Dict::DataType::const_iterator
it =
dict.getData().begin();
504 for (Dict::DataType::const_iterator
it =
dict.getData().begin();
517 "Estimated property count not equal to written");
543 std::vector<std::string> v;
626 for (
unsigned index = 0; index <
count; ++index) {
629 "Corrupted property serialization detected");
#define CHECK_INVARIANT(expr, mess)
#define POSTCONDITION(expr, mess)
#define RDUNUSED_PARAM(x)
virtual bool read(std::istream &ss, RDValue &value) const =0
virtual bool write(std::ostream &ss, const RDValue &value) const =0
virtual const char * getPropName() const =0
virtual CustomPropHandler * clone() const =0
virtual ~CustomPropHandler()
virtual bool canSerialize(const RDValue &value) const =0
static const boost::uint64_t UnsignedIntTag
static const boost::uint64_t StringTag
static const boost::uint64_t VecStringTag
static const boost::uint64_t VecIntTag
static const boost::uint64_t FloatTag
static const boost::uint64_t VecUnsignedIntTag
static const boost::uint64_t DoubleTag
static const boost::uint64_t IntTag
static const boost::uint64_t AnyTag
static const boost::uint64_t VecFloatTag
static const boost::uint64_t VecDoubleTag
static const boost::uint64_t BoolTag
std::vector< std::string > STR_VECT
int rdvalue_cast< int >(RDValue_cast_t v)
unsigned int rdvalue_cast< unsigned int >(RDValue_cast_t v)
std::string rdvalue_cast< std::string >(RDValue_cast_t v)
unsigned int streamReadProps(std::istream &ss, RDProps &props, const CustomPropHandlerVec &handlers={})
boost::uint32_t pullPackedIntFromString(const char *&text)
bool rdvalue_is(const RDValue_cast_t)
double rdvalue_cast< double >(RDValue_cast_t v)
void readRDStringVecValue(std::istream &ss, RDValue &value)
void streamRead(std::istream &ss, T &loc)
does a binary read of an object from a stream
std::string getLine(std::istream *inStream)
grabs the next line from an instream and returns it.
bool streamWriteProps(std::ostream &ss, const RDProps &props, bool savePrivate=false, bool saveComputed=false, const CustomPropHandlerVec &handlers={})
void readRDValueString(std::istream &ss, RDValue &value)
boost::uint32_t readPackedIntFromStream(std::stringstream &ss)
Reads an integer from a stream in packed format and returns the result.
bool isSerializable(const Dict::Pair &pair, const CustomPropHandlerVec &handlers={})
void streamReadStringVec(std::istream &ss, std::vector< std::string > &val, int version)
void readRDVecValue(std::istream &ss, RDValue &value)
void streamWriteVec(std::ostream &ss, const T &val)
T rdvalue_cast(RDValue_cast_t v)
void streamReadVec(std::istream &ss, T &val)
void readRDValue(std::istream &ss, RDValue &value)
T EndianSwapBytes(T value)
bool streamReadProp(std::istream &ss, Dict::Pair &pair, bool &dictHasNonPOD, const CustomPropHandlerVec &handlers={})
bool streamWriteProp(std::ostream &ss, const Dict::Pair &pair, const CustomPropHandlerVec &handlers={})
bool rdvalue_cast< bool >(RDValue_cast_t v)
void streamWrite(std::ostream &ss, const T &val)
does a binary write of an object to a stream
void appendPackedIntToStream(std::stringstream &ss, boost::uint32_t num)
Packs an integer and outputs it to a stream.
float rdvalue_cast< float >(RDValue_cast_t v)
std::vector< std::shared_ptr< const CustomPropHandler > > CustomPropHandlerVec