forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializer.hh
160 lines (132 loc) · 4.62 KB
/
serializer.hh
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
/*
* Copyright 2016 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <array>
#include "core/sstring.hh"
#include <unordered_map>
#include <experimental/optional>
#include "enum_set.hh"
#include "utils/managed_bytes.hh"
#include "bytes_ostream.hh"
#include "core/simple-stream.hh"
#include "boost/variant/variant.hpp"
#include "bytes_ostream.hh"
#include "utils/input_stream.hh"
namespace ser {
using size_type = uint32_t;
template<typename T, typename Input>
inline T deserialize_integral(Input& input) {
static_assert(std::is_integral<T>::value, "T should be integral");
T data;
input.read(reinterpret_cast<char*>(&data), sizeof(T));
return le_to_cpu(data);
}
template<typename T, typename Output>
inline void serialize_integral(Output& output, T data) {
static_assert(std::is_integral<T>::value, "T should be integral");
data = cpu_to_le(data);
output.write(reinterpret_cast<const char*>(&data), sizeof(T));
}
template<typename T>
struct serializer;
template<typename T>
struct integral_serializer {
template<typename Input>
static T read(Input& v) {
return deserialize_integral<T>(v);
}
template<typename Output>
static void write(Output& out, T v) {
serialize_integral(out, v);
}
template<typename Input>
static void skip(Input& v) {
read(v);
}
};
template<> struct serializer<bool> : public integral_serializer<int8_t> {};
template<> struct serializer<int8_t> : public integral_serializer<int8_t> {};
template<> struct serializer<uint8_t> : public integral_serializer<uint8_t> {};
template<> struct serializer<int16_t> : public integral_serializer<int16_t> {};
template<> struct serializer<uint16_t> : public integral_serializer<uint16_t> {};
template<> struct serializer<int32_t> : public integral_serializer<int32_t> {};
template<> struct serializer<uint32_t> : public integral_serializer<uint32_t> {};
template<> struct serializer<int64_t> : public integral_serializer<int64_t> {};
template<> struct serializer<uint64_t> : public integral_serializer<uint64_t> {};
template<typename Output>
void safe_serialize_as_uint32(Output& output, uint64_t data);
template<typename T, typename Output>
inline void serialize(Output& out, const T& v) {
serializer<T>::write(out, v);
};
template<typename T, typename Input>
inline auto deserialize(Input& in, boost::type<T> t) {
return serializer<T>::read(in);
};
template<typename T, typename Input>
inline void skip(Input& v, boost::type<T>) {
return serializer<T>::skip(v);
}
template<typename T>
size_type get_sizeof(const T& obj);
template<typename T>
void set_size(seastar::measuring_output_stream& os, const T& obj);
template<typename Stream, typename T>
void set_size(Stream& os, const T& obj);
template<typename Buffer, typename T>
Buffer serialize_to_buffer(const T& v, size_t head_space = 0);
template<typename T, typename Buffer>
T deserialize_from_buffer(const Buffer&, boost::type<T>, size_t head_space = 0);
template<typename Output, typename ...T>
void serialize(Output& out, const boost::variant<T...>& v);
template<typename Input, typename ...T>
boost::variant<T...> deserialize(Input& in, boost::type<boost::variant<T...>>);
struct unknown_variant_type {
size_type index;
sstring data;
};
template<typename Output>
void serialize(Output& out, const unknown_variant_type& v);
template<typename Input>
unknown_variant_type deserialize(Input& in, boost::type<unknown_variant_type>);
template <typename T>
struct normalize {
using type = T;
};
template <>
struct normalize<bytes_view> {
using type = bytes;
};
template <>
struct normalize<managed_bytes> {
using type = bytes;
};
template <>
struct normalize<bytes_ostream> {
using type = bytes;
};
template <typename T, typename U>
struct is_equivalent : std::is_same<typename normalize<std::remove_const_t<std::remove_reference_t<T>>>::type, typename normalize<std::remove_const_t <std::remove_reference_t<U>>>::type> {
};
}
/*
* Import the auto generated forward decleration code
*/