Skip to content
Navigation Menu
{{ message }}
forked from actor-framework/actor-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_serialization.cpp
More file actions
533 lines (469 loc) · 16.3 KB
/
Copy pathstring_serialization.cpp
File metadata and controls
533 lines (469 loc) · 16.3 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011-2013 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, *
* or (at your option) any later version. *
* *
* libcppa 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#include <stack>
#include <cctype>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include "cppa/atom.hpp"
#include "cppa/object.hpp"
#include "cppa/to_string.hpp"
#include "cppa/serializer.hpp"
#include "cppa/singletons.hpp"
#include "cppa/from_string.hpp"
#include "cppa/deserializer.hpp"
#include "cppa/skip_message.hpp"
#include "cppa/actor_namespace.hpp"
#include "cppa/primitive_variant.hpp"
#include "cppa/uniform_type_info.hpp"
#include "cppa/util/algorithm.hpp"
#include "cppa/detail/uniform_type_info_map.hpp"
using namespace std;
namespace cppa {
namespace {
bool isbuiltin(const string& type_name) {
return type_name == "@str" || type_name == "@atom" || type_name == "@tuple";
}
// serializes types as type_name(...) except:
// - strings are serialized "..."
// - atoms are serialized '...'
class string_serializer : public serializer {
typedef serializer super;
ostream& out;
actor_namespace m_namespace;
struct pt_writer {
ostream& out;
bool suppress_quotes;
pt_writer(ostream& mout, bool suppress_quotes = false) : out(mout), suppress_quotes(suppress_quotes) { }
template<typename T>
void operator()(const T& value) {
out << value;
}
// make sure char's are treated as int8_t number, not as character
inline void operator()(const char& value) {
out << static_cast<int>(value);
}
// make sure char's are treated as int8_t number, not as character
inline void operator()(const unsigned char& value) {
out << static_cast<unsigned int>(value);
}
void operator()(const string& str) {
if (!suppress_quotes) out << "\"";
for (char c : str) {
if (c == '"') out << "\\\"";
else out << c;
}
if (!suppress_quotes) out << "\"";
}
void operator()(const u16string&) { }
void operator()(const u32string&) { }
void operator()(const atom_value& value) {
out << "'" << to_string(value) << "'";
}
};
bool m_after_value;
bool m_obj_just_opened;
stack<size_t> m_object_pos;
stack<string> m_open_objects;
inline void clear() {
if (m_after_value) {
out << ", ";
m_after_value = false;
}
else if (m_obj_just_opened) {
if (!m_open_objects.empty() && !isbuiltin(m_open_objects.top())) {
out << " ( ";
}
m_obj_just_opened = false;
}
}
public:
string_serializer(ostream& mout)
: super(&m_namespace), out(mout), m_after_value(false)
, m_obj_just_opened(false) { }
void begin_object(const uniform_type_info* uti) {
clear();
std::string tname = uti->name();
m_open_objects.push(tname);
// do not print type names for strings and atoms
if (!isbuiltin(tname)) out << tname;
else if (tname.compare(0, 3, "@<>") == 0) {
auto subtypes = util::split(tname, '+', false);
}
m_obj_just_opened = true;
}
void end_object() {
if (m_obj_just_opened) {
// no open brackets to close
m_obj_just_opened = false;
}
m_after_value = true;
if (!m_open_objects.empty()) {
if (!isbuiltin(m_open_objects.top())) {
out << (m_after_value ? " )" : ")");
}
m_open_objects.pop();
}
}
void begin_sequence(size_t) {
clear();
out << "{ ";
}
void end_sequence() {
out << (m_after_value ? " }" : "}");
m_after_value = true;
}
void write_value(const primitive_variant& value) {
clear();
if (m_open_objects.empty()) {
throw runtime_error("write_value(): m_open_objects.empty()");
}
value.apply(pt_writer(out));
m_after_value = true;
}
void write_tuple(size_t size, const primitive_variant* values) {
clear();
out << "{";
const primitive_variant* end = values + size;
for ( ; values != end; ++values) {
write_value(*values);
}
out << (m_after_value ? " }" : "}");
}
void write_raw(size_t num_bytes, const void* buf) {
clear();
auto first = reinterpret_cast<const unsigned char*>(buf);
auto last = first + num_bytes;
out << hex;
out << setfill('0');
for (; first != last; ++first) {
out << setw(2) << static_cast<size_t>(*first);
}
out << dec;
m_after_value = true;
}
};
class string_deserializer : public deserializer {
typedef deserializer super;
string m_str;
string::iterator m_pos;
//size_t m_obj_count;
stack<bool> m_obj_had_left_parenthesis;
stack<string> m_open_objects;
actor_namespace m_namespace;
void skip_space_and_comma() {
while (*m_pos == ' ' || *m_pos == ',') ++m_pos;
}
void throw_malformed(const string& error_msg) {
throw logic_error("malformed string: " + error_msg);
}
void consume(char c) {
skip_space_and_comma();
if (*m_pos != c) {
string error;
error += "expected '";
error += c;
error += "' found '";
error += *m_pos;
error += "'";
if (m_open_objects.empty() == false) {
error += "during deserialization an instance of ";
error += m_open_objects.top();
}
throw_malformed(error);
}
++m_pos;
}
bool try_consume(char c) {
skip_space_and_comma();
if (*m_pos == c) {
++m_pos;
return true;
}
return false;
}
inline string::iterator next_delimiter() {
return find_if(m_pos, m_str.end(), [] (char c) -> bool {
switch (c) {
case '(':
case ')':
case '{':
case '}':
case ' ':
case ',': {
return true;
}
default: {
return false;
}
}
});
}
void integrity_check() {
if (m_open_objects.empty() || m_obj_had_left_parenthesis.empty()) {
throw_malformed("missing begin_object()");
}
if ( m_obj_had_left_parenthesis.top() == false
&& !isbuiltin(m_open_objects.top())) {
throw_malformed("expected left parenthesis after "
"begin_object call or void value");
}
}
public:
string_deserializer(string str) : super(&m_namespace), m_str(move(str)) {
m_pos = m_str.begin();
}
const uniform_type_info* begin_object() override {
skip_space_and_comma();
string type_name;
// shortcuts for builtin types
if (*m_pos == '"') {
type_name = "@str";
}
else if (*m_pos == '\'') {
type_name = "@atom";
}
else if (*m_pos == '{') {
type_name = "@tuple";
}
else {
auto substr_end = next_delimiter();
if (m_pos == substr_end) {
throw_malformed("could not seek object type name");
}
type_name = string(m_pos, substr_end);
m_pos = substr_end;
}
m_open_objects.push(type_name);
//++m_obj_count;
skip_space_and_comma();
// suppress leading parenthesis for builtin types
m_obj_had_left_parenthesis.push(try_consume('('));
//consume('(');
return get_uniform_type_info_map()->by_uniform_name(type_name);
}
void end_object() {
if (m_open_objects.empty()) {
throw runtime_error("no object to end");
}
if (m_obj_had_left_parenthesis.top() == true) {
consume(')');
}
m_open_objects.pop();
m_obj_had_left_parenthesis.pop();
if (m_open_objects.empty()) {
skip_space_and_comma();
if (m_pos != m_str.end()) {
throw_malformed(string("expected end of of string, found: ") + *m_pos);
}
}
}
size_t begin_sequence() {
integrity_check();
consume('{');
return count(m_pos, find(m_pos, m_str.end(), '}'), ',') + 1;
}
void end_sequence() {
integrity_check();
consume('}');
}
struct from_string {
const string& str;
from_string(const string& s) : str(s) { }
template<typename T>
void operator()(T& what) {
istringstream s(str);
s >> what;
}
void operator()(char& what) {
istringstream s(str);
int tmp;
s >> tmp;
what = static_cast<char>(tmp);
}
void operator()(unsigned char& what) {
istringstream s(str);
unsigned int tmp;
s >> tmp;
what = static_cast<unsigned char>(tmp);
}
void operator()(string& what) {
what = str;
}
void operator()(atom_value& what) {
what = static_cast<atom_value>(detail::atom_val(str.c_str(), 0xF));
}
void operator()(u16string&) { }
void operator()(u32string&) { }
};
primitive_variant read_value(primitive_type ptype) {
integrity_check();
skip_space_and_comma();
string::iterator substr_end;
auto find_if_cond = [] (char c) -> bool {
switch (c) {
case ')':
case '}':
case ' ':
case ',': return true;
default : return false;
}
};
if (ptype == pt_u8string || ptype == pt_atom) {
char needle = (ptype == pt_u8string) ? '"' : '\'';
if (*m_pos == needle) {
// skip leading "
++m_pos;
char last_char = needle;
auto find_if_str_cond = [&last_char, needle] (char c) -> bool {
if (c == needle && last_char != '\\') {
return true;
}
last_char = c;
return false;
};
substr_end = find_if(m_pos, m_str.end(), find_if_str_cond);
}
else {
substr_end = find_if(m_pos, m_str.end(), find_if_cond);
}
}
else {
substr_end = find_if(m_pos, m_str.end(), find_if_cond);
}
if (substr_end == m_str.end()) {
throw logic_error("malformed string (unterminated value)");
}
string substr(m_pos, substr_end);
m_pos += substr.size();
if (ptype == pt_u8string || ptype == pt_atom) {
char needle = (ptype == pt_u8string) ? '"' : '\'';
// skip trailing "
if (*m_pos != needle) {
string error_msg;
error_msg = "malformed string, expected '";
error_msg += needle;
error_msg += "' found '";
error_msg += *m_pos;
error_msg += "'";
throw logic_error(error_msg);
}
++m_pos;
// replace '\"' by '"'
char last_char = ' ';
auto cond = [&last_char, needle] (char c) -> bool {
if (c == needle && last_char == '\\') {
return true;
}
last_char = c;
return false;
};
string tmp;
auto sbegin = substr.begin();
auto send = substr.end();
for (auto i = find_if(sbegin, send, cond);
i != send;
i = find_if(i, send, cond)) {
--i;
tmp.append(sbegin, i);
tmp += needle;
i += 2;
sbegin = i;
}
if (sbegin != substr.begin()) {
tmp.append(sbegin, send);
}
if (!tmp.empty()) {
substr = move(tmp);
}
}
primitive_variant result(ptype);
result.apply(from_string(substr));
return result;
}
void read_tuple(size_t size,
const primitive_type* begin,
primitive_variant* storage) {
integrity_check();
consume('{');
const primitive_type* end = begin + size;
for ( ; begin != end; ++begin) {
*storage = move(read_value(*begin));
++storage;
}
consume('}');
}
void read_raw(size_t buf_size, void* vbuf) {
auto buf = reinterpret_cast<unsigned char*>(vbuf);
integrity_check();
skip_space_and_comma();
auto next_nibble = [&]() -> size_t {
if (*m_pos == '\0') {
throw_malformed("unexpected end-of-string");
}
char c = *m_pos++;
if (!isxdigit(c)) {
throw_malformed("unexpected character, expected [0-9a-f]");
}
return static_cast<size_t>(isdigit(c) ? c - '0' : (c - 'a' + 10));
};
for (size_t i = 0; i < buf_size; ++i) {
auto nibble = next_nibble();
*buf++ = static_cast<unsigned char>((nibble << 4) | next_nibble());
}
}
};
} // namespace <anonymous>
object from_string(const string& what) {
string_deserializer strd(what);
auto utype = strd.begin_object();
auto result = utype->deserialize(&strd);
strd.end_object();
return result;
}
namespace detail {
string to_string_impl(const void *what, const uniform_type_info *utype) {
ostringstream osstr;
string_serializer strs(osstr);
strs.begin_object(utype);
utype->serialize(what, &strs);
strs.end_object();
return osstr.str();
}
} // namespace detail
string to_verbose_string(const exception& e) {
std::ostringstream oss;
oss << detail::demangle(typeid(e)) << ": " << e.what();
return oss.str();
}
std::ostream& operator<<(std::ostream& out, skip_message_t) {
return out << "skip_message";
}
} // namespace cppa
You can’t perform that action at this time.
