12#ifndef _RD_STREAMOPS_H
13#define _RD_STREAMOPS_H
21#include <unordered_set>
22#include <boost/cstdint.hpp>
23#include <boost/predef.h>
31#if defined(BOOST_ENDIAN_LITTLE_BYTE) || defined(BOOST_ENDIAN_LITTLE_WORD)
33#elif defined(BOOST_ENDIAN_BIG_BYTE)
35#elif defined(BOOST_ENDIAN_BIG_WORD)
36#error "Cannot compile on word-swapped big-endian systems"
38#error "Failed to determine the system endian value"
44template <
class T,
unsigned int size>
57 for (
unsigned int i = 0; i < size; ++i) {
58 out.bytes[i] =
in.bytes[size - 1 - i];
72template <EEndian from, EEndian to,
class T>
91template <EEndian from, EEndian to>
95template <EEndian from, EEndian to>
99template <EEndian from, EEndian to>
107 boost::uint32_t num) {
109 unsigned int val,
res;
114 if (
res < (1 << 7)) {
120 if (
res < (1 << 14)) {
121 val = ((
res << 2) | 1);
126 if (
res < (1 << 21)) {
127 val = ((
res << 3) | 3);
132 if (
res < (1 << 29)) {
133 val = ((
res << 3) | 7);
151 boost::uint32_t val, num;
156 throw std::runtime_error(
"failed to read from stream");
161 if ((val & 1) == 0) {
163 }
else if ((val & 3) == 1) {
166 throw std::runtime_error(
"failed to read from stream");
172 }
else if ((val & 7) == 3) {
175 throw std::runtime_error(
"failed to read from stream");
181 throw std::runtime_error(
"failed to read from stream");
186 offset = (1 << 7) + (1 << 14);
190 throw std::runtime_error(
"failed to read from stream");
196 throw std::runtime_error(
"failed to read from stream");
202 throw std::runtime_error(
"failed to read from stream");
207 offset = (1 << 7) + (1 << 14) + (1 << 21);
209 num = (val >>
shift) + offset;
217 boost::uint32_t val, num;
224 if ((val & 1) == 0) {
226 }
else if ((val & 3) == 1) {
232 }
else if ((val & 7) == 3) {
240 offset = (1 << 7) + (1 << 14);
252 offset = (1 << 7) + (1 << 14) + (1 << 21);
254 num = (val >>
shift) + offset;
263 ss.write((
const char *)&
tval,
sizeof(T));
269 ss.write((
const char *)&
l,
sizeof(
l));
270 ss.write(what.c_str(),
sizeof(
char) *
l);
276 for (
size_t i = 0; i < val.size(); ++i) {
285 ss.read((
char *)&
tloc,
sizeof(T));
287 throw std::runtime_error(
"failed to read from stream");
302 ss.read((
char *)&
l,
sizeof(
l));
304 throw std::runtime_error(
"failed to read from stream");
306 char *
buff =
new char[
l];
307 ss.read(
buff,
sizeof(
char) *
l);
309 throw std::runtime_error(
"failed to read from stream");
311 what = std::string(
buff,
l);
317 boost::uint64_t size;
319 val.resize(boost::numeric_cast<size_t>(size));
321 for (
size_t i = 0; i < size; ++i) {
328 boost::uint64_t size;
332 for (
size_t i = 0; i < size; ++i) {
341 if (!
res.empty() && (
res.back() ==
'\r')) {
342 res.resize(
res.length() - 1);
381typedef std::vector<std::shared_ptr<const CustomPropHandler>>
386 switch (
pair.val.getTag()) {
419 switch (
pair.val.getTag()) {
484template <
typename COUNT_TYPE =
unsigned int>
488 const std::unordered_set<std::string> &
ignore = {}) {
490 std::unordered_set<std::string>
propnames;
499 for (
const auto &
elem :
dict.getData()) {
512 for (
const auto &
elem :
dict.getData()) {
524 "Estimated property count not equal to written");
550 std::vector<std::string> v;
625template <
typename COUNT_TYPE =
unsigned int>
638 for (
unsigned index = 0; index <
count; ++index) {
641 "Corrupted property serialization detected");
644 return static_cast<unsigned int>(
count);
#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)
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.
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 streamWriteProps(std::ostream &ss, const RDProps &props, bool savePrivate=false, bool saveComputed=false, const CustomPropHandlerVec &handlers={}, const std::unordered_set< std::string > &ignore={})
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
unsigned int streamReadProps(std::istream &ss, RDProps &props, const CustomPropHandlerVec &handlers={}, bool reset=true)