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>
92template <EEndian from, EEndian to>
97template <EEndian from, EEndian to>
102template <EEndian from, EEndian to>
111 boost::uint32_t num) {
113 unsigned int val,
res;
118 if (
res < (1 << 7)) {
124 if (
res < (1 << 14)) {
125 val = ((
res << 2) | 1);
130 if (
res < (1 << 21)) {
131 val = ((
res << 3) | 3);
136 if (
res < (1 << 29)) {
137 val = ((
res << 3) | 7);
155 boost::uint32_t val, num;
160 throw std::runtime_error(
"failed to read from stream");
165 if ((val & 1) == 0) {
167 }
else if ((val & 3) == 1) {
170 throw std::runtime_error(
"failed to read from stream");
176 }
else if ((val & 7) == 3) {
179 throw std::runtime_error(
"failed to read from stream");
185 throw std::runtime_error(
"failed to read from stream");
190 offset = (1 << 7) + (1 << 14);
194 throw std::runtime_error(
"failed to read from stream");
200 throw std::runtime_error(
"failed to read from stream");
206 throw std::runtime_error(
"failed to read from stream");
211 offset = (1 << 7) + (1 << 14) + (1 << 21);
213 num = (val >>
shift) + offset;
221 boost::uint32_t val, num;
228 if ((val & 1) == 0) {
230 }
else if ((val & 3) == 1) {
236 }
else if ((val & 7) == 3) {
244 offset = (1 << 7) + (1 << 14);
256 offset = (1 << 7) + (1 << 14) + (1 << 21);
258 num = (val >>
shift) + offset;
267 ss.write((
const char *)&
tval,
sizeof(T));
272 unsigned int l =
static_cast<unsigned int>(what.length());
274 ss.write(what.c_str(),
sizeof(
char) *
l);
280 for (
size_t i = 0; i < val.size(); ++i) {
289 ss.read((
char *)&
tloc,
sizeof(T));
291 throw std::runtime_error(
"failed to read from stream");
307 auto buff = std::make_unique<char[]>(
l);
308 ss.read(
buff.get(),
sizeof(
char) *
l);
310 throw std::runtime_error(
"failed to read from stream");
312 what = std::string(
buff.get(),
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);
387typedef std::vector<std::shared_ptr<const CustomPropHandler>>
392 switch (
pair.val.getTag()) {
425 switch (
pair.val.getTag()) {
490template <
typename COUNT_TYPE =
unsigned int>
494 const std::unordered_set<std::string> &
ignore = {}) {
496 std::unordered_set<std::string>
propnames;
505 for (
const auto &
elem :
dict.getData()) {
518 for (
const auto &
elem :
dict.getData()) {
530 "Estimated property count not equal to written");
556 std::vector<std::string> v;
631template <
typename COUNT_TYPE =
unsigned int>
644 for (
unsigned index = 0; index <
count; ++index) {
647 "Corrupted property serialization detected");
650 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)