11 #ifndef _RD_STREAMOPS_H 12 #define _RD_STREAMOPS_H 20 #include <boost/cstdint.hpp> 21 #include <boost/detail/endian.hpp> 29 #if defined(BOOST_LITTLE_ENDIAN) 31 #elif defined(BOOST_BIG_ENDIAN) 34 #error "Failed to determine the system endian value" 40 template <
class T,
unsigned int size>
49 for (
unsigned int i = 0; i < size / 2; ++i) {
50 out.bytes[i] = in.bytes[size - 1 - i];
51 out.bytes[size - 1 - i] = in.bytes[i];
65 template <EEndian from, EEndian to,
class T>
68 BOOST_STATIC_ASSERT(
sizeof(T) == 1 ||
sizeof(T) == 2 ||
sizeof(T) == 4 ||
70 if (
sizeof(T) == 1)
return value;
76 if (from == to)
return value;
78 return SwapBytes<T, sizeof(T)>(value);
80 template <EEndian from, EEndian to>
84 template <EEndian from, EEndian to>
88 template <EEndian from, EEndian to>
96 boost::uint32_t num) {
98 unsigned int val, res;
103 if (res < (1 << 7)) {
109 if (res < (1 << 14)) {
110 val = ((res << 2) | 1);
115 if (res < (1 << 21)) {
116 val = ((res << 3) | 3);
121 if (res < (1 << 29)) {
122 val = ((res << 3) | 7);
131 for (bix = 0; bix < nbytes; bix++) {
132 tc = (char)(val & 255);
140 boost::uint32_t val, num;
143 ss.read(&tmp,
sizeof(tmp));
146 if ((val & 1) == 0) {
148 }
else if ((val & 3) == 1) {
149 ss.read((
char *)&tmp,
sizeof(tmp));
150 val |= (
UCHAR(tmp) << 8);
153 }
else if ((val & 7) == 3) {
154 ss.read((
char *)&tmp,
sizeof(tmp));
155 val |= (
UCHAR(tmp) << 8);
156 ss.read((
char *)&tmp,
sizeof(tmp));
157 val |= (
UCHAR(tmp) << 16);
159 offset = (1 << 7) + (1 << 14);
161 ss.read((
char *)&tmp,
sizeof(tmp));
162 val |= (
UCHAR(tmp) << 8);
163 ss.read((
char *)&tmp,
sizeof(tmp));
164 val |= (
UCHAR(tmp) << 16);
165 ss.read((
char *)&tmp,
sizeof(tmp));
166 val |= (
UCHAR(tmp) << 24);
168 offset = (1 << 7) + (1 << 14) + (1 << 21);
170 num = (val >> shift) + offset;
178 boost::uint32_t val, num;
185 if ((val & 1) == 0) {
187 }
else if ((val & 3) == 1) {
190 val |= (
UCHAR(tmp) << 8);
193 }
else if ((val & 7) == 3) {
196 val |= (
UCHAR(tmp) << 8);
199 val |= (
UCHAR(tmp) << 16);
201 offset = (1 << 7) + (1 << 14);
205 val |= (
UCHAR(tmp) << 8);
208 val |= (
UCHAR(tmp) << 16);
211 val |= (
UCHAR(tmp) << 24);
213 offset = (1 << 7) + (1 << 14) + (1 << 21);
215 num = (val >> shift) + offset;
221 template <
typename T>
223 T tval = EndianSwapBytes<HOST_ENDIAN_ORDER, LITTLE_ENDIAN_ORDER>(val);
224 ss.write((
const char *)&tval,
sizeof(T));
228 inline void streamWrite(std::ostream &ss,
const std::string &what) {
229 unsigned int l = rdcast<unsigned int>(what.length());
230 ss.write((
const char *)&l,
sizeof(l));
231 ss.write(what.c_str(),
sizeof(char) * l);
236 streamWrite(ss, static_cast<boost::uint64_t>(val.size()));
237 for(
size_t i=0;i<val.size();++i)
242 template <
typename T>
245 ss.read((
char *)&tloc,
sizeof(T));
246 loc = EndianSwapBytes<LITTLE_ENDIAN_ORDER, HOST_ENDIAN_ORDER>(tloc);
256 inline void streamRead(std::istream &ss, std::string &what,
int version) {
259 ss.read((
char *)&l,
sizeof(l));
260 char *buff =
new char[l + 1];
261 ss.read(buff,
sizeof(
char) * l);
270 boost::uint64_t size;
274 for(
size_t i=0;i<size;++i)
279 boost::uint64_t size;
283 for(
size_t i=0;i<size;++i)
288 inline std::string
getLine(std::istream *inStream) {
290 std::getline(*inStream, res);
291 if ((res.length() > 0) && (res[res.length() - 1] ==
'\r')) {
292 res.erase(res.length() - 1);
297 inline std::string
getLine(std::istream &inStream) {
393 std::cerr <<
"Failed to write " << pair.
key << std::endl;
400 bool savePrivate=
false,
bool saveComputed=
false) {
402 std::set<std::string> propnames(propsToSave.begin(), propsToSave.end());
405 unsigned int count = 0;
406 for(Dict::DataType::const_iterator it = dict.getData().begin();
407 it != dict.getData().end();
409 if(
isSerializable(*it) && propnames.find(it->key) != propnames.end()) {
416 unsigned int writtenCount = 0;
417 for(Dict::DataType::const_iterator it = dict.getData().begin();
418 it != dict.getData().end();
420 if(propnames.find(it->key) != propnames.end()) {
430 POSTCONDITION(count==writtenCount,
"Estimated property count not equal to written");
457 std::vector<std::string> v;
496 for(
unsigned index = 0; index<count; ++index) {
498 "Corrupted property serialization detected");
#define POSTCONDITION(expr, mess)
static const boost::uint64_t VecDoubleTag
static const boost::uint64_t UnsignedIntTag
T EndianSwapBytes(T value)
#define CHECK_INVARIANT(expr, mess)
const DataType & getData() const
Access to the underlying data.
T rdvalue_cast(RDValue v)
static const boost::uint64_t DoubleTag
double rdvalue_cast< double >(RDValue v)
void streamRead(std::istream &ss, T &loc)
does a binary read of an object from a stream
void readRDValueString(std::istream &ss, RDValue &value)
void readRDValue(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.
static const boost::uint64_t FloatTag
boost::uint32_t pullPackedIntFromString(const char *&text)
bool streamWriteProps(std::ostream &ss, const RDProps &props, bool savePrivate=false, bool saveComputed=false)
bool rdvalue_cast< bool >(RDValue v)
void streamReadStringVec(std::istream &ss, std::vector< std::string > &val, int version)
static const boost::uint64_t StringTag
void readRDVecValue(std::istream &ss, RDValue &value)
static const boost::uint64_t VecIntTag
static const boost::uint64_t VecUnsignedIntTag
unsigned int rdvalue_cast< unsigned int >(RDValue v)
const Dict & getDict() const
gets the underlying Dictionary
static const boost::uint64_t VecStringTag
bool streamReadProp(std::istream &ss, Dict::Pair &pair)
unsigned int streamReadProps(std::istream &ss, RDProps &props)
void streamWriteVec(std::ostream &ss, const T &val)
#define RDUNUSED_PARAM(x)
float rdvalue_cast< float >(RDValue v)
static const boost::uint64_t IntTag
int rdvalue_cast< int >(RDValue v)
bool isSerializable(const Dict::Pair &pair)
static const boost::uint64_t BoolTag
void streamWrite(std::ostream &ss, const T &val)
does a binary write of an object to a stream
std::string getLine(std::istream *inStream)
grabs the next line from an instream and returns it.
STR_VECT getPropList(bool includePrivate=true, bool includeComputed=true) const
returns a list with the names of our properties
boost::uint64_t getTag() const
static const boost::uint64_t VecFloatTag
void appendPackedIntToStream(std::stringstream &ss, boost::uint32_t num)
Packs an integer and outputs it to a stream.
void readRDStringVecValue(std::istream &ss, RDValue &value)
The Dict class can be used to store objects of arbitrary type keyed by strings.
std::vector< std::string > STR_VECT
void streamReadVec(std::istream &ss, T &val)
bool streamWriteProp(std::ostream &ss, const Dict::Pair &pair)