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>
75 BOOST_STATIC_ASSERT(
sizeof(T) == 1 ||
sizeof(T) == 2 ||
sizeof(T) == 4 ||
89 return SwapBytes<T, sizeof(T)>(value);
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);
142 for (bix = 0; bix < nbytes; bix++) {
143 tc = (char)(val & 255);
151 boost::uint32_t val, num;
154 ss.read(&tmp,
sizeof(tmp));
156 throw std::runtime_error(
"failed to read from stream");
161 if ((val & 1) == 0) {
163 }
else if ((val & 3) == 1) {
164 ss.read((
char *)&tmp,
sizeof(tmp));
166 throw std::runtime_error(
"failed to read from stream");
169 val |= (
UCHAR(tmp) << 8);
172 }
else if ((val & 7) == 3) {
173 ss.read((
char *)&tmp,
sizeof(tmp));
175 throw std::runtime_error(
"failed to read from stream");
178 val |= (
UCHAR(tmp) << 8);
179 ss.read((
char *)&tmp,
sizeof(tmp));
181 throw std::runtime_error(
"failed to read from stream");
184 val |= (
UCHAR(tmp) << 16);
186 offset = (1 << 7) + (1 << 14);
188 ss.read((
char *)&tmp,
sizeof(tmp));
190 throw std::runtime_error(
"failed to read from stream");
193 val |= (
UCHAR(tmp) << 8);
194 ss.read((
char *)&tmp,
sizeof(tmp));
196 throw std::runtime_error(
"failed to read from stream");
199 val |= (
UCHAR(tmp) << 16);
200 ss.read((
char *)&tmp,
sizeof(tmp));
202 throw std::runtime_error(
"failed to read from stream");
205 val |= (
UCHAR(tmp) << 24);
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) {
229 val |= (
UCHAR(tmp) << 8);
232 }
else if ((val & 7) == 3) {
235 val |= (
UCHAR(tmp) << 8);
238 val |= (
UCHAR(tmp) << 16);
240 offset = (1 << 7) + (1 << 14);
244 val |= (
UCHAR(tmp) << 8);
247 val |= (
UCHAR(tmp) << 16);
250 val |= (
UCHAR(tmp) << 24);
252 offset = (1 << 7) + (1 << 14) + (1 << 21);
254 num = (val >> shift) + offset;
262 T tval = EndianSwapBytes<HOST_ENDIAN_ORDER, LITTLE_ENDIAN_ORDER>(val);
263 ss.write((
const char *)&tval,
sizeof(T));
267inline void streamWrite(std::ostream &ss,
const std::string &what) {
268 unsigned int l = rdcast<unsigned int>(what.length());
269 ss.write((
const char *)&l,
sizeof(l));
270 ss.write(what.c_str(),
sizeof(
char) * l);
275 streamWrite(ss,
static_cast<boost::uint64_t
>(val.size()));
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");
289 loc = EndianSwapBytes<LITTLE_ENDIAN_ORDER, HOST_ENDIAN_ORDER>(tloc);
299inline void streamRead(std::istream &ss, std::string &what,
int version) {
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) {
338inline std::string
getLine(std::istream *inStream) {
340 std::getline(*inStream, res);
341 if (!res.empty() && (res.back() ==
'\r')) {
342 res.resize(res.length() - 1);
347inline std::string
getLine(std::istream &inStream) {
381typedef std::vector<std::shared_ptr<const CustomPropHandler>>
401 for (
auto &handler : handlers) {
402 if (handler->canSerialize(pair.
val)) {
466 for (
auto &handler : handlers) {
467 if (handler->canSerialize(pair.
val)) {
473 streamWrite(ss, std::string(handler->getPropName()));
474 handler->write(ss, pair.
val);
484template <
typename COUNT_TYPE =
unsigned int>
486 std::ostream &ss,
const RDProps &props,
bool savePrivate =
false,
488 const std::unordered_set<std::string> &ignore = {}) {
490 std::unordered_set<std::string> propnames;
491 for (
const auto &pn : propsToSave) {
492 if (ignore.empty() || ignore.find(pn) == ignore.end()) {
493 propnames.insert(pn);
497 const Dict &dict = props.
getDict();
498 COUNT_TYPE count = 0;
499 for (
const auto &elem : dict.getData()) {
500 if (propnames.find(elem.key) != propnames.end()) {
511 COUNT_TYPE writtenCount = 0;
512 for (
const auto &elem : dict.getData()) {
513 if (propnames.find(elem.key) != propnames.end()) {
524 "Estimated property count not equal to written");
550 std::vector<std::string> v;
566 readRDValue<int>(ss, pair.
val);
569 readRDValue<unsigned int>(ss, pair.
val);
572 readRDValue<bool>(ss, pair.
val);
575 readRDValue<float>(ss, pair.
val);
578 readRDValue<double>(ss, pair.
val);
583 dictHasNonPOD =
true;
587 dictHasNonPOD =
true;
590 readRDVecValue<int>(ss, pair.
val);
591 dictHasNonPOD =
true;
594 readRDVecValue<unsigned int>(ss, pair.
val);
595 dictHasNonPOD =
true;
598 readRDVecValue<float>(ss, pair.
val);
599 dictHasNonPOD =
true;
602 readRDVecValue<double>(ss, pair.
val);
603 dictHasNonPOD =
true;
606 std::string propType;
609 for (
auto &handler : handlers) {
610 if (propType == handler->getPropName()) {
611 handler->read(ss, pair.
val);
612 dictHasNonPOD =
true;
625template <
typename COUNT_TYPE =
unsigned int>
636 auto startSz = dict.getData().size();
637 dict.getData().resize(startSz + count);
638 for (
unsigned index = 0; index < count; ++index) {
640 dict.getNonPODStatus(), handlers),
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
void reset()
Clears all keys (and values) from the dictionary.
const Dict & getDict() const
gets the underlying Dictionary
STR_VECT getPropList(bool includePrivate=true, bool includeComputed=true) const
returns a list with the names of our properties
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)
boost::uint32_t pullPackedIntFromString(const char *&text)
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)
boost::uint64_t getTag() const