-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSimpleBuffer.h
More file actions
263 lines (233 loc) · 7.63 KB
/
Copy pathSimpleBuffer.h
File metadata and controls
263 lines (233 loc) · 7.63 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
@copyright Russell Standish 2023
@author Russell Standish
This file is part of Classdesc
Open source licensed under the MIT license. See LICENSE for details.
*/
/* A simple buffer implementation of the implied interface of this concept.
classdesc::json_pack_t also implements this interface
*/
#ifndef CLASSDESC_SIMPLE_BUFFER_H
#define CLASSDESC_SIMPLE_BUFFER_H
#include <json_pack_base.h>
#include <boost/variant.hpp>
#include <deque>
namespace classdesc
{
struct TypeVisitor: public boost::static_visitor<RESTProcessType::Type>, public RESTProcessType
{
Type operator()(bool) const {return boolean;}
Type operator()(int) const {return int_number;}
Type operator()(double) const {return float_number;}
Type operator()(classdesc::string) const {return string;}
Type operator()(const json_pack_t& x) const {return x.is_null()? null: object;}
Type operator()(std::deque<json_pack_t>) const {return array;}
};
struct ToJsonVisitor: public boost::static_visitor<json_pack_t>
{
json_pack_t operator()(bool x) const {return json_pack_t(x);}
json_pack_t operator()(int x) const {return json_pack_t(x);}
json_pack_t operator()(double x) const {return json_pack_t(x);}
json_pack_t operator()(const string& x) const {return json_pack_t(x);}
json_pack_t operator()(const json_pack_t& x) const {return x;}
json_pack_t operator()(const std::deque<json_pack_t>& x) const {
json5_parser::mArray r(x.begin(),x.end());
return json_pack_t(r);
}
};
using SuperVariant=boost::variant<bool,int,double,string,json_pack_t,std::deque<json_pack_t>>;
struct SimpleBuffer: public SuperVariant
{
using Super=SuperVariant;
using Array=std::deque<json_pack_t>;
SimpleBuffer(): Super(json_pack_t()) {}
explicit SimpleBuffer(RESTProcessType::Type type) {
switch (type)
{
case RESTProcessType::boolean: Super::operator=(false); return;
case RESTProcessType::int_number: Super::operator=(0); return;
case RESTProcessType::float_number: Super::operator=(0.0); return;
case RESTProcessType::string: Super::operator=(""); return;
case RESTProcessType::object: Super::operator=(json_pack_t()); return;
case RESTProcessType::array: Super::operator=(std::deque<json_pack_t>()); return;
}
}
explicit SimpleBuffer(bool x): Super(x) {}
explicit SimpleBuffer(double x): Super(x) {}
explicit SimpleBuffer(const string& x): Super(x) {}
explicit SimpleBuffer(const json_pack_t& x) {operator=(x);}
SimpleBuffer& operator=(const json_pack_t& x) {
switch (x.type())
{
case RESTProcessType::boolean:
Super::operator=(x.get_bool());
break;
case RESTProcessType::int_number:
Super::operator=(x.get_int());
break;
case RESTProcessType::float_number:
Super::operator=(x.get_real());
break;
case RESTProcessType::string:
Super::operator=(x.get_str());
break;
case RESTProcessType::array:
{
auto& arr=x.get_array();
std::deque<json_pack_t> v(arr.begin(),arr.end());
Super::operator=(v);
}
break;
default:
Super::operator=(x);
}
return *this;
}
explicit SimpleBuffer(const std::deque<json_pack_t>& x): Super(x) {}
template <class T> explicit SimpleBuffer(const T& x) {
json_pack_t j;
j<<x;
operator=(j);
}
template <class T>
SimpleBuffer& operator=(const T& x) {Super::operator=(x); return *this;}
SimpleBuffer& operator=(int64_t x) {Super::operator=(double(x)); return *this;}
SimpleBuffer& operator=(uint64_t x) {Super::operator=(double(x)); return *this;}
/// return type conforms to the sequence concept
const Array& array() const {
return boost::get<Array>(*this);
}
RESTProcessType::Type type() const {
return boost::apply_visitor(TypeVisitor(), *this);
}
};
const SimpleBuffer& operator>>(const SimpleBuffer& b, bool& x)
{x=boost::get<bool>(b); return b;}
SimpleBuffer& operator<<(SimpleBuffer& b, const bool& x)
{b=x; return b;}
const SimpleBuffer& operator>>(const SimpleBuffer& b, char& x)
{x=boost::get<string>(b)[0]; return b;}
SimpleBuffer& operator<<(SimpleBuffer& b, const char& x)
{b=string{x}; return b;}
// numbers
template <class T>
typename enable_if<
And<is_arithmetic<T>,Not<is_same<T,bool>>,Not<is_const<T>>>,
const SimpleBuffer&>::T
operator>>(const SimpleBuffer& b, T& x)
{
switch (b.type())
{
case RESTProcessType::float_number:
x=boost::get<double>(b);
break;
case RESTProcessType::int_number:
x=boost::get<int>(b);
break;
case RESTProcessType::object:
boost::get<json_pack_t>(b)>>x;
break;
default:
throw std::runtime_error("Invalid variant type");
}
return b;
}
template <class T>
typename enable_if<
And<is_arithmetic<T>,Not<is_same<T,bool>>>,
SimpleBuffer&>::T
operator<<(SimpleBuffer& b, const T& x)
{b=double(x); return b;}
// strings
template <class T>
typename enable_if<And<is_string<T>,Not<is_const<T>>>, const SimpleBuffer&>::T
operator>>(const SimpleBuffer& b, T& x)
{x=boost::get<string>(b); return b;}
template <class T>
typename enable_if<And<is_string<T>>, SimpleBuffer&>::T
operator<<(SimpleBuffer& b, const T& x)
{b=string(x); return b;}
// enums
template <class T>
typename enable_if<And<is_enum<T>,Not<is_const<T>>>, const SimpleBuffer&>::T
operator>>(const SimpleBuffer& b, T& x)
{auto tmp=boost::get<string>(b); x=enum_keys<T>()(tmp); return b;}
template <class T>
typename enable_if<And<is_enum<T>>, SimpleBuffer&>::T
operator<<(SimpleBuffer& b, const T& x)
{b=to_string(x); return b;}
// sequences
template <class T>
typename enable_if<And<is_sequence<T>,Not<is_const<T>>>, const SimpleBuffer&>::T
operator>>(const SimpleBuffer& b, T& x)
{
resize(x,0);
if (b.type()!=RESTProcessType::array) return b;
auto& arr=b.array();
for (auto& i: arr)
{
typename T::value_type e;
i>>e;
push_back(x,e);
}
return b;
}
template <class T>
typename enable_if<is_sequence<T>, SimpleBuffer&>::T
operator<<(SimpleBuffer& b, const T& x)
{
std::deque<json_pack_t> tmp;
for (auto& i: x)
{
tmp.emplace_back();
tmp.back()<<i;
}
b=SimpleBuffer(tmp);
return b;
}
// const
template <class T>
typename enable_if<is_const<T>, const SimpleBuffer&>::T
operator>>(const SimpleBuffer& b, T& x)
{return b;}
const SimpleBuffer& operator>>(const SimpleBuffer& b, const char* x)
{
throw std::runtime_error("cannot unpack to char*, please use string instead");
}
// everything else
template <class T>
typename enable_if<
And<
Not<is_arithmetic<T>>,
Not<is_string<T>>,
Not<is_sequence<T>>,
Not<is_enum<T>>,
Not<is_const<T>>
>, const SimpleBuffer&>::T
operator>>(const SimpleBuffer& b, T& x)
{
boost::get<json_pack_t>(b)>>x;
return b;
}
template <class T>
typename enable_if<
And<
Not<is_arithmetic<T>>,
Not<is_string<T>>,
Not<is_sequence<T>>,
Not<is_enum<T>>
>, SimpleBuffer&>::T
operator<<(SimpleBuffer& b, const T& x)
{
json_pack_t tmp; tmp<<x;
b=tmp;
return b;
}
template <>
std::string typeName<SimpleBuffer>() {return "SimpleBuffer";}
}
// need to define this explicitly - see SimpleBuffer.cc
#pragma omit json_pack classdesc::SimpleBuffer
#pragma omit json_unpack classdesc::SimpleBuffer
#pragma omit RESTProcess classdesc::SimpleBuffer
#endif