-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.h
169 lines (139 loc) · 4.31 KB
/
binary.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma once
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
// 自制序列化工具
class BinaryWriter;
class BinaryReader;
// 如果要支持新类型,请进行偏特化(下方)
template<typename T>
struct Serializer;
template <class T, std::size_t = sizeof(T)>
std::true_type is_complete_impl(T*);
std::false_type is_complete_impl(...);
template <class T>
using is_complete = decltype(is_complete_impl(std::declval<T*>()));
// 二进制写入器
// 注意:支持基础类型、可序列化(实现了Serializer<T>)类型的直接写入
class BinaryWriter
{
uint8_t* buffer;
int size;
int head = 0;
void assert_enough(int l) {
if (head + l > size) {
throw std::out_of_range("no data remain");
}
}
public:
BinaryWriter(uint8_t* buffer, int size) : buffer(buffer), size(size) {};
// 基础类型
template<typename T, std::enable_if_t<!is_complete<Serializer<T>>::value && std::is_trivially_copyable<T>::value, int> = 0>
void write(const T& x) {
assert_enough(sizeof(T));
*(T*)(buffer + head) = x;
head += sizeof(T);
}
void write(void* src, int len) {
assert_enough(len);
memcpy(buffer + head, src, len);
head += len;
}
// 可序列化类型
template<typename T, std::enable_if_t<is_complete<Serializer<T>>::value, int> = 0>
void write(const T& t) {
Serializer<T>().serialize(*this, t);
}
int length() const
{
return head;
}
};
// 二进制读取器
// 注意:支持基础类型、可序列化(实现了Serializer<T>)类型的直接读取
class BinaryReader
{
uint8_t* buffer;
int size;
int head = 0;
void assert_enough(int l) {
if (head + l > size) {
throw std::out_of_range("no data remain");
}
}
public:
BinaryReader(uint8_t* buffer, int size) : buffer(buffer), size(size) {};
// 基础类型
template<typename T, std::enable_if_t<!is_complete<Serializer<T>>::value && std::is_trivially_copyable<T>::value, int> = 0>
void read(T& x) {
assert_enough(sizeof(T));
x = *(T*)(buffer + head);
head += sizeof(T);
}
void read(void* dest, int len) {
assert_enough(len);
memcpy(dest, buffer + head, len);
head += len;
}
// 可序列化类型
template<typename T, std::enable_if_t<is_complete<Serializer<T>>::value, int> = 0>
void read(T& t) {
Serializer<T>().deserialize(*this, t);
}
int length() const
{
return head;
}
};
// 支持string的序列化
template<>
struct Serializer<std::string> {
void serialize(BinaryWriter& bw, const std::string& x) {
int len = x.length();
bw.write(len);
bw.write((void*)x.data(), len);
}
void deserialize(BinaryReader& br, std::string& t) {
int len;
br.read(len);
t.resize(len);
br.read(t.data(), len);
}
};
// 支持vector<T>的序列化
template<typename T>
struct Serializer<std::vector<T>> {
void serialize(BinaryWriter& bw, const std::vector<T>& x) {
int len = x.size();
bw.write(len);
for (auto& elem : x) {
bw.write(elem);
}
}
void deserialize(BinaryReader& br, std::vector<T>& x) {
int len;
br.read(len);
x.resize(len);
for (auto& elem : x) {
br.read(elem);
}
}
};
// 组合定义序列化器的辅助宏
// looks evil, but could save boilerplate code
#define SERIALIZER(type, ser, deser) template<> struct Serializer<type> { \
void serialize(BinaryWriter& bw, const type& obj) { do ser while(0); } \
void deserialize(BinaryReader& br, type& obj) { do deser while (0); } \
};
#define FIELD0()
#define FIELD1(p, fld) p(obj.##fld);
#define FIELD2(p, fld, ...) _CONCAT(FIELD1(p, fld), FIELD1(p, __VA_ARGS__))
#define FIELD3(p, fld, ...) _CONCAT(FIELD1(p, fld), FIELD2(p, __VA_ARGS__))
#define FIELD4(p, fld, ...) _CONCAT(FIELD1(p, fld), FIELD3(p, __VA_ARGS__))
#define _SERIALIZERN(n, type, ...) SERIALIZER(type, { FIELD##n(bw.write, __VA_ARGS__) }, { FIELD##n(br.read, __VA_ARGS__) })
#define SERIALIZER0(type, ...) _SERIALIZERN(0, type, __VA_ARGS__)
#define SERIALIZER1(type, ...) _SERIALIZERN(1, type, __VA_ARGS__)
#define SERIALIZER2(type, ...) _SERIALIZERN(2, type, __VA_ARGS__)
#define SERIALIZER3(type, ...) _SERIALIZERN(3, type, __VA_ARGS__)
#define SERIALIZER4(type, ...) _SERIALIZERN(4, type, __VA_ARGS__)