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>
74 BOOST_STATIC_ASSERT(
sizeof(T) == 1 ||
sizeof(T) == 2 ||
sizeof(T) == 4 ||
88 return SwapBytes<T, sizeof(T)>(value);
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);
141 for (bix = 0; bix < nbytes; bix++) {
142 tc = (char)(val & 255);
150 boost::uint32_t val, num;
153 ss.read(&tmp,
sizeof(tmp));
155 throw std::runtime_error(
"failed to read from stream");
160 if ((val & 1) == 0) {
162 }
else if ((val & 3) == 1) {
163 ss.read((
char *)&tmp,
sizeof(tmp));
165 throw std::runtime_error(
"failed to read from stream");
168 val |= (
UCHAR(tmp) << 8);
171 }
else if ((val & 7) == 3) {
172 ss.read((
char *)&tmp,
sizeof(tmp));
174 throw std::runtime_error(
"failed to read from stream");
177 val |= (
UCHAR(tmp) << 8);
178 ss.read((
char *)&tmp,
sizeof(tmp));
180 throw std::runtime_error(
"failed to read from stream");
183 val |= (
UCHAR(tmp) << 16);
185 offset = (1 << 7) + (1 << 14);
187 ss.read((
char *)&tmp,
sizeof(tmp));
189 throw std::runtime_error(
"failed to read from stream");
192 val |= (
UCHAR(tmp) << 8);
193 ss.read((
char *)&tmp,
sizeof(tmp));
195 throw std::runtime_error(
"failed to read from stream");
198 val |= (
UCHAR(tmp) << 16);
199 ss.read((
char *)&tmp,
sizeof(tmp));
201 throw std::runtime_error(
"failed to read from stream");
204 val |= (
UCHAR(tmp) << 24);
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) {
228 val |= (
UCHAR(tmp) << 8);
231 }
else if ((val & 7) == 3) {
234 val |= (
UCHAR(tmp) << 8);
237 val |= (
UCHAR(tmp) << 16);
239 offset = (1 << 7) + (1 << 14);
243 val |= (
UCHAR(tmp) << 8);
246 val |= (
UCHAR(tmp) << 16);
249 val |= (
UCHAR(tmp) << 24);
251 offset = (1 << 7) + (1 << 14) + (1 << 21);
253 num = (val >> shift) + offset;
261 T tval = EndianSwapBytes<HOST_ENDIAN_ORDER, LITTLE_ENDIAN_ORDER>(val);
262 ss.write((
const char *)&tval,
sizeof(T));
266inline void streamWrite(std::ostream &ss,
const std::string &what) {
267 unsigned int l = rdcast<unsigned int>(what.length());
268 ss.write((
const char *)&l,
sizeof(l));
269 ss.write(what.c_str(),
sizeof(
char) * l);
274 streamWrite(ss,
static_cast<boost::uint64_t
>(val.size()));
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");
288 loc = EndianSwapBytes<LITTLE_ENDIAN_ORDER, HOST_ENDIAN_ORDER>(tloc);
298inline void streamRead(std::istream &ss, std::string &what,
int version) {
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) {
337inline std::string
getLine(std::istream *inStream) {
339 std::getline(*inStream, res);
340 if (!res.empty() && (res.back() ==
'\r')) {
341 res.resize(res.length() - 1);
346inline std::string
getLine(std::istream &inStream) {
380typedef std::vector<std::shared_ptr<const CustomPropHandler>>
400 for (
auto &handler : handlers) {
401 if (handler->canSerialize(pair.
val)) {
465 for (
auto &handler : handlers) {
466 if (handler->canSerialize(pair.
val)) {
472 streamWrite(ss, std::string(handler->getPropName()));
473 handler->write(ss, pair.
val);
484 bool savePrivate =
false,
485 bool saveComputed =
false,
488 std::set<std::string> propnames(propsToSave.begin(), propsToSave.end());
490 const Dict &dict = props.
getDict();
491 unsigned int count = 0;
492 for (Dict::DataType::const_iterator it = dict.getData().begin();
493 it != dict.getData().end(); ++it) {
494 if (propnames.find(it->key) != propnames.end()) {
503 unsigned int writtenCount = 0;
504 for (Dict::DataType::const_iterator it = dict.getData().begin();
505 it != dict.getData().end(); ++it) {
506 if (propnames.find(it->key) != propnames.end()) {
517 "Estimated property count not equal to written");
543 std::vector<std::string> v;
559 readRDValue<int>(ss, pair.
val);
562 readRDValue<unsigned int>(ss, pair.
val);
565 readRDValue<bool>(ss, pair.
val);
568 readRDValue<float>(ss, pair.
val);
571 readRDValue<double>(ss, pair.
val);
576 dictHasNonPOD =
true;
580 dictHasNonPOD =
true;
583 readRDVecValue<int>(ss, pair.
val);
584 dictHasNonPOD =
true;
587 readRDVecValue<unsigned int>(ss, pair.
val);
588 dictHasNonPOD =
true;
591 readRDVecValue<float>(ss, pair.
val);
592 dictHasNonPOD =
true;
595 readRDVecValue<double>(ss, pair.
val);
596 dictHasNonPOD =
true;
599 std::string propType;
602 for (
auto &handler : handlers) {
603 if (propType == handler->getPropName()) {
604 handler->read(ss, pair.
val);
605 dictHasNonPOD =
true;
625 dict.getData().resize(count);
626 for (
unsigned index = 0; index < count; ++index) {
628 dict.getNonPODStatus(), handlers),
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
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)
unsigned int streamReadProps(std::istream &ss, RDProps &props, const CustomPropHandlerVec &handlers={})
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.
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
boost::uint64_t getTag() const