-
Notifications
You must be signed in to change notification settings - Fork 3
/
serializable.h
133 lines (106 loc) · 3.81 KB
/
serializable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef DYSCO_SERIALIZABLE_H
#define DYSCO_SERIALIZABLE_H
#include <complex>
#include <iostream>
#include <stdint.h>
class Serializable {
public:
virtual ~Serializable() {}
virtual void Serialize(std::ostream &stream) const = 0;
virtual void Unserialize(std::istream &stream) = 0;
template <typename T>
static void SerializeToUInt64(std::ostream &stream, T value) {
uint64_t val64t = value;
stream.write(reinterpret_cast<char *>(&val64t), sizeof(val64t));
}
template <typename T>
static void SerializeToUInt32(std::ostream &stream, T value) {
uint32_t val32t = value;
stream.write(reinterpret_cast<char *>(&val32t), sizeof(val32t));
}
template <typename T>
static void SerializeToUInt16(std::ostream &stream, T value) {
uint16_t val16t = value;
stream.write(reinterpret_cast<char *>(&val16t), sizeof(val16t));
}
template <typename T>
static void SerializeToUInt8(std::ostream &stream, T value) {
uint8_t val8t = value;
stream.write(reinterpret_cast<char *>(&val8t), sizeof(val8t));
}
static void SerializeToBool8(std::ostream &stream, bool value) {
uint8_t val8t = value;
stream.write(reinterpret_cast<char *>(&val8t), sizeof(val8t));
}
static void SerializeToFloat(std::ostream &stream, float value) {
stream.write(reinterpret_cast<char *>(&value), sizeof(value));
}
static void SerializeToDouble(std::ostream &stream, double value) {
stream.write(reinterpret_cast<char *>(&value), sizeof(value));
}
static void SerializeToLDouble(std::ostream &stream, long double value) {
stream.write(reinterpret_cast<char *>(&value), sizeof(value));
}
static void SerializeToLDoubleC(std::ostream &stream,
std::complex<long double> value) {
stream.write(reinterpret_cast<char *>(&value), sizeof(value));
}
static void SerializeToString(std::ostream &stream, const std::string &str) {
SerializeToUInt64(stream, str.size());
stream.write(str.c_str(), str.size());
}
static void SerializeTo32bString(std::ostream &stream,
const std::string &str) {
SerializeToUInt32(stream, str.size());
stream.write(str.c_str(), str.size());
}
static uint64_t UnserializeUInt64(std::istream &stream) {
return Unserialize<uint64_t>(stream);
}
static uint32_t UnserializeUInt32(std::istream &stream) {
return Unserialize<uint32_t>(stream);
}
static uint16_t UnserializeUInt16(std::istream &stream) {
return Unserialize<uint16_t>(stream);
}
static uint8_t UnserializeUInt8(std::istream &stream) {
return Unserialize<uint8_t>(stream);
}
static bool UnserializeBool8(std::istream &stream) {
return (bool)Unserialize<uint8_t>(stream);
}
static double UnserializeFloat(std::istream &stream) {
return Unserialize<float>(stream);
}
static double UnserializeDouble(std::istream &stream) {
return Unserialize<double>(stream);
}
static long double UnserializeLDouble(std::istream &stream) {
return Unserialize<long double>(stream);
}
static std::complex<long double> UnserializeLDoubleC(std::istream &stream) {
return Unserialize<std::complex<long double>>(stream);
}
static void UnserializeString(std::istream &stream, std::string &destStr) {
size_t size = UnserializeUInt64(stream);
char *str = new char[size];
stream.read(str, size);
destStr = std::string(str, size);
delete[] str;
}
static void Unserialize32bString(std::istream &stream, std::string &destStr) {
size_t size = UnserializeUInt32(stream);
char *str = new char[size];
stream.read(str, size);
destStr = std::string(str, size);
delete[] str;
}
private:
template <typename T>
static T Unserialize(std::istream &stream) {
T val;
stream.read(reinterpret_cast<char *>(&val), sizeof(val));
return val;
}
};
#endif