-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuple_serializer.cpp
More file actions
198 lines (170 loc) · 5.67 KB
/
tuple_serializer.cpp
File metadata and controls
198 lines (170 loc) · 5.67 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// HOMEWORK: implement these two functions below and make them work properly
// - doesn't have to be JSON, you can come up with your own protocol
// - remember "fold expressions" and "pack expansion" -- they might come useful for the variadics
#include <string>
#include <tuple>
#include <sstream>
#include <iostream>
#include <vector>
#include <variant>
#include <cassert>
#include <charconv>
#include <utility>
#include <boost/convert.hpp>
#include <boost/convert/strtol.hpp>
using tNumber = long;
using tBool = bool;
using tDouble = double;
using tString = std::string;
using tTypes = std::variant<tNumber, tDouble, tString, tBool>;
template<int N, typename... Ts> using NthTypeOf =
typename std::tuple_element_t<N, std::tuple<Ts...>>;
const char DELIM = '\n';
const char NUMBER_TYPE = 'l';
const char DOUBLE_TYPE = 'd';
const char STRING_TYPE = 's';
const char BOOL_TYPE = 'b';
template<typename T>
void print_tuple(std::stringstream& ss, const T& t)
{
ss << STRING_TYPE << t << DELIM;
}
template<>
void print_tuple(std::stringstream& ss, const tDouble& t)
{
ss << DOUBLE_TYPE << t << DELIM;
}
template<>
void print_tuple(std::stringstream& ss, const tNumber& t)
{
ss << NUMBER_TYPE << t << DELIM;
}
template<>
void print_tuple(std::stringstream& ss, const tBool& b)
{
ss << BOOL_TYPE << ( b ? "1" : "0" ) << DELIM;
}
template <typename... Ts>
std::string serialize_from_tuple(const std::tuple<Ts...>& t)
{
std::stringstream ss;
std::apply([&](const auto&... t)
{
((print_tuple(ss, t)), ...); //FOLD over the param pack
},
t); //Q: still bit of a mental block about going from tuple<Ts...> to auto&... t ?
//is it each param pack?
return ss.str();
}
/**
* Test function: each element in the seralised string matches the strings expected
*/
void test_serialised(const std::string& s, const std::vector<std::string>& expected)
{
int line = 0;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ((pos = s.find(DELIM, prev)) != std::string::npos) {
const std::string_view el = s.substr(prev, pos - prev);
prev = pos + 1;
std::cout << "Test:" << el << "=" << expected[line] << std::endl;
assert(el == expected[line]);
line++;
}
}
template <typename... Ts>
std::tuple<Ts...> deserialize_into_tuple(const std::string& s)
{
std::array<tTypes, sizeof...(Ts)> variants;
//1. tokenize our string to get type and data, into an array of variants
{
size_t left = 0;
size_t right = 0;
size_t elementIndex = 0;
//Tokenize - sequence scan and and create tokens of correct type
//c++ ranges in c++23
while ((right = s.find('\n', left)) != std::string::npos) {
char type = s.at(left);
const std::string data = s.substr(left+1, (right - (left+1)));
std::cout << "deserialize_into_tuple: type " << type << " got " << data << std::endl;
switch (type)
{
case STRING_TYPE:
variants[elementIndex++] = data;
break;
case NUMBER_TYPE:
variants[elementIndex++] = atol(data.c_str());
//string view version could be : boost::convert<long>(data, boost::cnv::strtol()).get();
break;
case DOUBLE_TYPE:
variants[elementIndex++] = atof(data.c_str());
break;
case BOOL_TYPE:
variants[elementIndex++] = (data=="true" ? true : false);
break;
default:
std::cout << "Error unknown type " << type << std::endl;
throw(std::exception());
};
left = right + 1;
}
}
//2. Array to Tuple
std::tuple<Ts...> tup;
{
//need the compile time size of the tuple to convert array to tuple
auto seq = std::make_index_sequence<sizeof...(Ts)>{};
//do NOT use apply on the index_sequence
[&]<std::size_t... I>(std::index_sequence<I...>) {
auto setElem = [](auto& tuple_el, const auto& variant_el) {
tuple_el = variant_el;
};
( setElem(std::get<I>(tup), std::get<NthTypeOf<I, Ts...>>(variants[I])),...);
}(seq);
}
return tup;
}
/**
* Debug function to print a tuple contents to cout
*/
template <typename... Ts>
void tuple_print(const std::tuple<Ts...>& t)
{
std::apply([](auto&... te) {
std::cout << "tuple_print: ";
auto fn = [](auto& el) { std::cout << el << ","; };
((fn(te),...));
std::cout << std::endl;
}, t);
}
/**
* MAIN
*/
int main()
{
std::cout << "start" << std::endl;
//Test 1
{
using tTuple1 = std::tuple<tNumber, tDouble, tString, tBool>;
const tTuple1 t1a = std::make_tuple(1L, 2.3, std::string("three"), false);
const std::string s1 = serialize_from_tuple(t1a);
test_serialised(s1, {"l1","d2.3","sthree","b0"});
tuple_print(t1a);
tTuple1 t1b = deserialize_into_tuple<tNumber, tDouble, tString, tBool>(s1);
tuple_print(t1b);
assert( t1b == t1a );
}
//Test 2: same type in several elements
{
using tTuple2 = std::tuple<tNumber, tNumber, tNumber>;
const tTuple2 t2a = std::make_tuple(12L, 22L, 32L);
const std::string s2 = serialize_from_tuple(t2a);
test_serialised(s2, {"l12","l22","l32"});
tuple_print(t2a);
tTuple2 t2b = deserialize_into_tuple<tNumber, tNumber, tNumber>(s2);
tuple_print(t2b);
assert( t2b == t2a );
}
std::cout << "end" << std::endl;
}