-
Notifications
You must be signed in to change notification settings - Fork 30.2k
/
blob_serializer_deserializer.h
132 lines (99 loc) Β· 3.76 KB
/
blob_serializer_deserializer.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
#ifndef SRC_BLOB_SERIALIZER_DESERIALIZER_H_
#define SRC_BLOB_SERIALIZER_DESERIALIZER_H_
#include <string>
#include <vector>
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
// This is related to the blob that is used in snapshots and single executable
// applications and has nothing to do with `node_blob.h`.
namespace node {
class BlobSerializerDeserializer {
public:
explicit BlobSerializerDeserializer(bool is_debug_v) : is_debug(is_debug_v) {}
template <typename... Args>
void Debug(const char* format, Args&&... args) const;
template <typename T>
std::string ToStr(const T& arg) const;
template <typename T>
std::string GetName() const;
bool is_debug = false;
};
enum class StringLogMode {
kAddressOnly, // Can be used when the string contains binary content.
kAddressAndContent,
};
// Child classes are expected to implement T Read<T>() where
// !std::is_arithmetic_v<T> && !std::is_same_v<T, std::string>
template <typename Impl>
class BlobDeserializer : public BlobSerializerDeserializer {
public:
explicit BlobDeserializer(bool is_debug_v, std::string_view s)
: BlobSerializerDeserializer(is_debug_v), sink(s) {}
~BlobDeserializer() = default;
size_t read_total = 0;
std::string_view sink;
Impl* impl() { return static_cast<Impl*>(this); }
const Impl* impl() const { return static_cast<const Impl*>(this); }
// Helper for reading numeric types.
template <typename T>
T ReadArithmetic();
// Layout of vectors:
// [ 4/8 bytes ] count
// [ ... ] contents (count * size of individual elements)
template <typename T>
std::vector<T> ReadVector();
// ReadString() creates a copy of the data. ReadStringView() doesn't.
std::string ReadString();
std::string_view ReadStringView(StringLogMode mode);
// Helper for reading an array of numeric types.
template <typename T>
void ReadArithmetic(T* out, size_t count);
// Helper for reading numeric vectors.
template <typename Number>
std::vector<Number> ReadArithmeticVector(size_t count);
private:
// Helper for reading non-numeric vectors.
template <typename T>
std::vector<T> ReadNonArithmeticVector(size_t count);
template <typename T>
T ReadElement();
};
// Child classes are expected to implement size_t Write<T>(const T&) where
// !std::is_arithmetic_v<T> && !std::is_same_v<T, std::string>
template <typename Impl>
class BlobSerializer : public BlobSerializerDeserializer {
public:
explicit BlobSerializer(bool is_debug_v)
: BlobSerializerDeserializer(is_debug_v) {}
~BlobSerializer() = default;
Impl* impl() { return static_cast<Impl*>(this); }
const Impl* impl() const { return static_cast<const Impl*>(this); }
std::vector<char> sink;
// Helper for writing numeric types.
template <typename T>
size_t WriteArithmetic(const T& data);
// Layout of vectors:
// [ 4/8 bytes ] count
// [ ... ] contents (count * size of individual elements)
template <typename T>
size_t WriteVector(const std::vector<T>& data);
// The layout of a written string:
// [ 4/8 bytes ] length
// [ |length| bytes ] contents
size_t WriteStringView(std::string_view data, StringLogMode mode);
size_t WriteString(const std::string& data);
// Helper for writing an array of numeric types.
template <typename T>
size_t WriteArithmetic(const T* data, size_t count);
// Helper for writing numeric vectors.
template <typename Number>
size_t WriteArithmeticVector(const std::vector<Number>& data);
private:
// Helper for writing non-numeric vectors.
template <typename T>
size_t WriteNonArithmeticVector(const std::vector<T>& data);
template <typename T>
size_t WriteElement(const T& data);
};
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_BLOB_SERIALIZER_DESERIALIZER_H_