Changeset View
Changeset View
Standalone View
Standalone View
src/serialize.h
Show First 20 Lines • Show All 131 Lines • ▼ Show 20 Lines | template <typename Stream> inline uint32_t ser_readdata32be(Stream &s) { | ||||
return be32toh(obj); | return be32toh(obj); | ||||
} | } | ||||
template <typename Stream> inline uint64_t ser_readdata64(Stream &s) { | template <typename Stream> inline uint64_t ser_readdata64(Stream &s) { | ||||
uint64_t obj; | uint64_t obj; | ||||
s.read((char *)&obj, 8); | s.read((char *)&obj, 8); | ||||
return le64toh(obj); | return le64toh(obj); | ||||
} | } | ||||
inline uint64_t ser_double_to_uint64(double x) { | inline uint64_t ser_double_to_uint64(double x) { | ||||
union { | uint64_t tmp; | ||||
double x; | std::memcpy(&tmp, &x, sizeof(x)); | ||||
uint64_t y; | static_assert(sizeof(tmp) == sizeof(x), | ||||
} tmp; | "double and uint64_t assumed to have the same size"); | ||||
tmp.x = x; | return tmp; | ||||
return tmp.y; | |||||
} | } | ||||
inline uint32_t ser_float_to_uint32(float x) { | inline uint32_t ser_float_to_uint32(float x) { | ||||
union { | uint32_t tmp; | ||||
float x; | std::memcpy(&tmp, &x, sizeof(x)); | ||||
uint32_t y; | static_assert(sizeof(tmp) == sizeof(x), | ||||
} tmp; | "float and uint32_t assumed to have the same size"); | ||||
tmp.x = x; | return tmp; | ||||
return tmp.y; | |||||
} | } | ||||
inline double ser_uint64_to_double(uint64_t y) { | inline double ser_uint64_to_double(uint64_t y) { | ||||
union { | double tmp; | ||||
double x; | std::memcpy(&tmp, &y, sizeof(y)); | ||||
uint64_t y; | static_assert(sizeof(tmp) == sizeof(y), | ||||
} tmp; | "double and uint64_t assumed to have the same size"); | ||||
tmp.y = y; | return tmp; | ||||
return tmp.x; | |||||
} | } | ||||
inline float ser_uint32_to_float(uint32_t y) { | inline float ser_uint32_to_float(uint32_t y) { | ||||
union { | float tmp; | ||||
float x; | std::memcpy(&tmp, &y, sizeof(y)); | ||||
uint32_t y; | static_assert(sizeof(tmp) == sizeof(y), | ||||
} tmp; | "float and uint32_t assumed to have the same size"); | ||||
tmp.y = y; | return tmp; | ||||
return tmp.x; | |||||
} | } | ||||
///////////////////////////////////////////////////////////////// | ///////////////////////////////////////////////////////////////// | ||||
// | // | ||||
// Templates for serializing to anything that looks like a stream, | // Templates for serializing to anything that looks like a stream, | ||||
// i.e. anything that supports .read(char*, size_t) and .write(char*, size_t) | // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t) | ||||
// | // | ||||
class CSizeComputer; | class CSizeComputer; | ||||
▲ Show 20 Lines • Show All 1,000 Lines • Show Last 20 Lines |