-
Notifications
You must be signed in to change notification settings - Fork 729
/
sio_message.h
569 lines (474 loc) · 13.2 KB
/
sio_message.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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//
// sio_message.h
//
// Created by Melo Yao on 3/25/15.
//
#ifndef __SIO_MESSAGE_H__
#define __SIO_MESSAGE_H__
#include <string>
#include <memory>
#include <vector>
#include <map>
#include <cassert>
#include <type_traits>
namespace sio
{
class message
{
public:
enum flag
{
flag_integer,
flag_double,
flag_string,
flag_binary,
flag_array,
flag_object,
flag_boolean,
flag_null
};
virtual ~message(){};
class list;
flag get_flag() const
{
return _flag;
}
typedef std::shared_ptr<message> ptr;
virtual bool get_bool() const
{
assert(false);
return false;
}
virtual int64_t get_int() const
{
assert(false);
return 0;
}
virtual double get_double() const
{
assert(false);
return 0;
}
virtual std::string const& get_string() const
{
assert(false);
static std::string s_empty_string;
s_empty_string.clear();
return s_empty_string;
}
virtual std::shared_ptr<const std::string> const& get_binary() const
{
assert(false);
static std::shared_ptr<const std::string> s_empty_binary;
s_empty_binary = nullptr;
return s_empty_binary;
}
virtual const std::vector<ptr>& get_vector() const
{
assert(false);
static std::vector<ptr> s_empty_vector;
s_empty_vector.clear();
return s_empty_vector;
}
virtual std::vector<ptr>& get_vector()
{
assert(false);
static std::vector<ptr> s_empty_vector;
s_empty_vector.clear();
return s_empty_vector;
}
virtual const std::map<std::string,message::ptr>& get_map() const
{
assert(false);
static std::map<std::string,message::ptr> s_empty_map;
s_empty_map.clear();
return s_empty_map;
}
virtual std::map<std::string,message::ptr>& get_map()
{
assert(false);
static std::map<std::string,message::ptr> s_empty_map;
s_empty_map.clear();
return s_empty_map;
}
private:
flag _flag;
protected:
message(flag f):_flag(f){}
};
class null_message : public message
{
protected:
null_message()
:message(flag_null)
{
}
public:
static message::ptr create()
{
return ptr(new null_message());
}
};
class bool_message : public message
{
bool _v;
protected:
bool_message(bool v)
:message(flag_boolean),_v(v)
{
}
public:
static message::ptr create(bool v)
{
return ptr(new bool_message(v));
}
bool get_bool() const
{
return _v;
}
};
class int_message : public message
{
int64_t _v;
protected:
int_message(int64_t v)
:message(flag_integer),_v(v)
{
}
public:
static message::ptr create(int64_t v)
{
return ptr(new int_message(v));
}
int64_t get_int() const
{
return _v;
}
double get_double() const//add double accessor for integer.
{
return static_cast<double>(_v);
}
};
class double_message : public message
{
double _v;
double_message(double v)
:message(flag_double),_v(v)
{
}
public:
static message::ptr create(double v)
{
return ptr(new double_message(v));
}
double get_double() const
{
return _v;
}
};
class string_message : public message
{
std::string _v;
string_message(std::string const& v)
:message(flag_string),_v(v)
{
}
string_message(std::string&& v)
:message(flag_string),_v(std::move(v))
{
}
public:
static message::ptr create(std::string const& v)
{
return ptr(new string_message(v));
}
static message::ptr create(std::string&& v)
{
return ptr(new string_message(std::move(v)));
}
std::string const& get_string() const
{
return _v;
}
};
class binary_message : public message
{
std::shared_ptr<const std::string> _v;
binary_message(std::shared_ptr<const std::string> const& v)
:message(flag_binary),_v(v)
{
}
public:
static message::ptr create(std::shared_ptr<const std::string> const& v)
{
return ptr(new binary_message(v));
}
std::shared_ptr<const std::string> const& get_binary() const
{
return _v;
}
};
class array_message : public message
{
std::vector<message::ptr> _v;
array_message():message(flag_array)
{
}
public:
static message::ptr create()
{
return ptr(new array_message());
}
void push(message::ptr const& message)
{
if(message)
_v.push_back(message);
}
void push(const std::string& text)
{
_v.push_back(string_message::create(text));
}
void push(std::string&& text)
{
_v.push_back(string_message::create(std::move(text)));
}
void push(std::shared_ptr<std::string> const& binary)
{
if(binary)
_v.push_back(binary_message::create(binary));
}
void push(std::shared_ptr<const std::string> const& binary)
{
if(binary)
_v.push_back(binary_message::create(binary));
}
void insert(size_t pos,message::ptr const& message)
{
_v.insert(_v.begin()+pos, message);
}
void insert(size_t pos,const std::string& text)
{
_v.insert(_v.begin()+pos, string_message::create(text));
}
void insert(size_t pos,std::string&& text)
{
_v.insert(_v.begin()+pos, string_message::create(std::move(text)));
}
void insert(size_t pos,std::shared_ptr<std::string> const& binary)
{
if(binary)
_v.insert(_v.begin()+pos, binary_message::create(binary));
}
void insert(size_t pos,std::shared_ptr<const std::string> const& binary)
{
if(binary)
_v.insert(_v.begin()+pos, binary_message::create(binary));
}
size_t size() const
{
return _v.size();
}
const message::ptr& at(size_t i) const
{
return _v[i];
}
const message::ptr& operator[] (size_t i) const
{
return _v[i];
}
std::vector<ptr>& get_vector()
{
return _v;
}
const std::vector<ptr>& get_vector() const
{
return _v;
}
};
class object_message : public message
{
std::map<std::string,message::ptr> _v;
object_message() : message(flag_object)
{
}
public:
static message::ptr create()
{
return ptr(new object_message());
}
void insert(const std::string & key,message::ptr const& message)
{
_v[key] = message;
}
void insert(const std::string & key,const std::string& text)
{
_v[key] = string_message::create(text);
}
void insert(const std::string & key,std::string&& text)
{
_v[key] = string_message::create(std::move(text));
}
void insert(const std::string & key,std::shared_ptr<std::string> const& binary)
{
if(binary)
_v[key] = binary_message::create(binary);
}
void insert(const std::string & key,std::shared_ptr<const std::string> const& binary)
{
if(binary)
_v[key] = binary_message::create(binary);
}
bool has(const std::string & key)
{
return _v.find(key) != _v.end();
}
const message::ptr& at(const std::string & key) const
{
static std::shared_ptr<message> not_found;
std::map<std::string,message::ptr>::const_iterator it = _v.find(key);
if (it != _v.cend()) return it->second;
return not_found;
}
const message::ptr& operator[] (const std::string & key) const
{
return at(key);
}
bool has(const std::string & key) const
{
return _v.find(key) != _v.end();
}
std::map<std::string,message::ptr>& get_map()
{
return _v;
}
const std::map<std::string,message::ptr>& get_map() const
{
return _v;
}
};
class message::list
{
public:
list()
{
}
list(std::nullptr_t)
{
}
list(message::list&& rhs):
m_vector(std::move(rhs.m_vector))
{
}
list & operator= (const message::list && rhs)
{
m_vector = std::move(rhs.m_vector);
return *this;
}
template <typename T>
list(T&& content,
typename std::enable_if<std::is_same<std::vector<message::ptr>,typename std::remove_reference<T>::type>::value>::type* = 0):
m_vector(std::forward<T>(content))
{
}
list(message::list const& rhs):
m_vector(rhs.m_vector)
{
}
list(message::ptr const& message)
{
if(message)
m_vector.push_back(message);
}
list(const std::string& text)
{
m_vector.push_back(string_message::create(text));
}
list(std::string&& text)
{
m_vector.push_back(string_message::create(std::move(text)));
}
list(std::shared_ptr<std::string> const& binary)
{
if(binary)
m_vector.push_back(binary_message::create(binary));
}
list(std::shared_ptr<const std::string> const& binary)
{
if(binary)
m_vector.push_back(binary_message::create(binary));
}
void push(message::ptr const& message)
{
if(message)
m_vector.push_back(message);
}
void push(const std::string& text)
{
m_vector.push_back(string_message::create(text));
}
void push(std::string&& text)
{
m_vector.push_back(string_message::create(std::move(text)));
}
void push(std::shared_ptr<std::string> const& binary)
{
if(binary)
m_vector.push_back(binary_message::create(binary));
}
void push(std::shared_ptr<const std::string> const& binary)
{
if(binary)
m_vector.push_back(binary_message::create(binary));
}
void insert(size_t pos,message::ptr const& message)
{
m_vector.insert(m_vector.begin()+pos, message);
}
void insert(size_t pos,const std::string& text)
{
m_vector.insert(m_vector.begin()+pos, string_message::create(text));
}
void insert(size_t pos,std::string&& text)
{
m_vector.insert(m_vector.begin()+pos, string_message::create(std::move(text)));
}
void insert(size_t pos,std::shared_ptr<std::string> const& binary)
{
if(binary)
m_vector.insert(m_vector.begin()+pos, binary_message::create(binary));
}
void insert(size_t pos,std::shared_ptr<const std::string> const& binary)
{
if(binary)
m_vector.insert(m_vector.begin()+pos, binary_message::create(binary));
}
size_t size() const
{
return m_vector.size();
}
const message::ptr& at(size_t i) const
{
return m_vector[i];
}
const message::ptr& operator[] (size_t i) const
{
return m_vector[i];
}
message::ptr to_array_message(std::string const& event_name) const
{
message::ptr arr = array_message::create();
arr->get_vector().push_back(string_message::create(event_name));
arr->get_vector().insert(arr->get_vector().end(),m_vector.begin(),m_vector.end());
return arr;
}
message::ptr to_array_message() const
{
message::ptr arr = array_message::create();
arr->get_vector().insert(arr->get_vector().end(),m_vector.begin(),m_vector.end());
return arr;
}
private:
std::vector<message::ptr> m_vector;
};
}
#endif