-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectorBinarySerializer.h
71 lines (61 loc) · 1.75 KB
/
VectorBinarySerializer.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
/*
* File: VectorBinarySerializer.h
* Author: michal
*
* Created on March 1, 2016, 3:44 AM
*/
#ifndef VECTORBINARYSERIALIZER_H
#define VECTORBINARYSERIALIZER_H
#include <vector>
namespace Serialize
{
template <class T, bool raw = is_raw_binary_serializable<T>::value>
struct VectorSerializer
{
void serialize(IOutputStream &os, const std::vector<T> &v)
{
BinaryLengthFormat l = v.size();
os.write(&l, sizeof(l));
for (int i = 0; i < l; i++)
binary(os) << v[i];
}
};
template <class T>
struct VectorSerializer<T, true>
{
void serialize(IOutputStream &os, const std::vector<T> &v)
{
BinaryLengthFormat l = v.size();
os.write(&l, sizeof(l));
os.write(v.data(), l*sizeof(T));
}
};
template <class T, bool raw = is_raw_binary_serializable<T>::value>
struct VectorDeserializer
{
void serialize(IInputStream &os, std::vector<T> &v)
{
BinaryLengthFormat l;
os.read(&l, sizeof(l));
v.resize(l);
for (int i = 0; i < l; i++)
binary(os) >> v[i];
}
};
template <class T>
struct VectorDeserializer<T, true>
{
void serialize(IInputStream &os, std::vector<T> &v)
{
BinaryLengthFormat l;
os.read(&l, sizeof(l));
v.resize(l);
os.read(v.data(), l*sizeof(T));
}
};
template <class T>
struct Serializer<BinaryFormat, std::vector<T>> : VectorSerializer<T> {};
template <class T>
struct Deserializer<BinaryFormat, std::vector<T>> : VectorDeserializer<T> {};
}
#endif /* VECTORBINARYSERIALIZER_H */