MetaSoul

Loading...
Searching...
No Matches
sio_message.h
Go to the documentation of this file.
1
2
3#ifndef __EMOSHAPEIO_MESSAGE_H__
4#define __EMOSHAPEIO_MESSAGE_H__
5#include <string>
6#include <memory>
7#include <vector>
8#include <map>
9#include <cassert>
10#include <type_traits>
11 namespace emoshapeio
12{
13 class message
14 {
15 public:
16 enum flag
17 {
18 flag_integer,
19 flag_double,
20 flag_string,
21 flag_binary,
22 flag_array,
23 flag_object,
24 flag_boolean,
25 flag_null
26 };
27
28 virtual ~message(){};
29
30 class list;
31
32 flag get_flag() const
33 {
34 return _flag;
35 }
36
37 typedef std::shared_ptr<message> ptr;
38
39 virtual bool get_bool() const
40 {
41 assert(false);
42 return false;
43 }
44
45 virtual int64_t get_int() const
46 {
47 assert(false);
48 return 0;
49 }
50
51 virtual double get_double() const
52 {
53 assert(false);
54 return 0;
55 }
56
57 virtual std::string const& get_string() const
58 {
59 assert(false);
60 static std::string s_empty_string;
61 s_empty_string.clear();
62 return s_empty_string;
63 }
64
65 virtual std::shared_ptr<const std::string> const& get_binary() const
66 {
67 assert(false);
68 static std::shared_ptr<const std::string> s_empty_binary;
69 s_empty_binary = nullptr;
70 return s_empty_binary;
71 }
72
73 virtual const std::vector<ptr>& get_vector() const
74 {
75 assert(false);
76 static std::vector<ptr> s_empty_vector;
77 s_empty_vector.clear();
78 return s_empty_vector;
79 }
80
81 virtual std::vector<ptr>& get_vector()
82 {
83 assert(false);
84 static std::vector<ptr> s_empty_vector;
85 s_empty_vector.clear();
86 return s_empty_vector;
87 }
88
89 virtual const std::map<std::string,message::ptr>& get_map() const
90 {
91 assert(false);
92 static std::map<std::string,message::ptr> s_empty_map;
93 s_empty_map.clear();
94 return s_empty_map;
95 }
96
97 virtual std::map<std::string,message::ptr>& get_map()
98 {
99 assert(false);
100 static std::map<std::string,message::ptr> s_empty_map;
101 s_empty_map.clear();
102 return s_empty_map;
103 }
104 private:
105 flag _flag;
106
107 protected:
108 message(flag f):_flag(f){}
109 };
110
111 class null_message : public message
112 {
113 protected:
114 null_message()
115 :message(flag_null)
116 {
117 }
118
119 public:
120 static message::ptr create()
121 {
122 return ptr(new null_message());
123 }
124 };
125
126 class bool_message : public message
127 {
128 bool _v;
129
130 protected:
131 bool_message(bool v)
132 :message(flag_boolean),_v(v)
133 {
134 }
135
136 public:
137 static message::ptr create(bool v)
138 {
139 return ptr(new bool_message(v));
140 }
141
142 bool get_bool() const
143 {
144 return _v;
145 }
146 };
147
148 class int_message : public message
149 {
150 int64_t _v;
151 protected:
152 int_message(int64_t v)
153 :message(flag_integer),_v(v)
154 {
155 }
156
157 public:
158 static message::ptr create(int64_t v)
159 {
160 return ptr(new int_message(v));
161 }
162
163 int64_t get_int() const
164 {
165 return _v;
166 }
167
168 double get_double() const//add double accessor for integer.
169 {
170 return static_cast<double>(_v);
171 }
172 };
173
174 class double_message : public message
175 {
176 double _v;
177 double_message(double v)
178 :message(flag_double),_v(v)
179 {
180 }
181
182 public:
183 static message::ptr create(double v)
184 {
185 return ptr(new double_message(v));
186 }
187
188 double get_double() const
189 {
190 return _v;
191 }
192 };
193
194 class string_message : public message
195 {
196 std::string _v;
197 string_message(std::string const& v)
198 :message(flag_string),_v(v)
199 {
200 }
201
202 string_message(std::string&& v)
203 :message(flag_string),_v(move(v))
204 {
205 }
206 public:
207 static message::ptr create(std::string const& v)
208 {
209 return ptr(new string_message(v));
210 }
211
212 static message::ptr create(std::string&& v)
213 {
214 return ptr(new string_message(move(v)));
215 }
216
217 std::string const& get_string() const
218 {
219 return _v;
220 }
221 };
222
223 class binary_message : public message
224 {
225 std::shared_ptr<const std::string> _v;
226 binary_message(std::shared_ptr<const std::string> const& v)
227 :message(flag_binary),_v(v)
228 {
229 }
230 public:
231 static message::ptr create(std::shared_ptr<const std::string> const& v)
232 {
233 return ptr(new binary_message(v));
234 }
235
236 std::shared_ptr<const std::string> const& get_binary() const
237 {
238 return _v;
239 }
240 };
241
242 class array_message : public message
243 {
244 std::vector<message::ptr> _v;
245 array_message():message(flag_array)
246 {
247 }
248
249 public:
250 static message::ptr create()
251 {
252 return ptr(new array_message());
253 }
254
255 void push(message::ptr const& message)
256 {
257 if(message)
258 _v.push_back(message);
259 }
260
261 void push(const std::string& text)
262 {
263 _v.push_back(string_message::create(text));
264 }
265
266 void push(std::string&& text)
267 {
268 _v.push_back(string_message::create(move(text)));
269 }
270
271 void push(std::shared_ptr<std::string> const& binary)
272 {
273 if(binary)
274 _v.push_back(binary_message::create(binary));
275 }
276
277 void push(std::shared_ptr<const std::string> const& binary)
278 {
279 if(binary)
280 _v.push_back(binary_message::create(binary));
281 }
282
283 void insert(size_t pos,message::ptr const& message)
284 {
285 _v.insert(_v.begin()+pos, message);
286 }
287
288 void insert(size_t pos,const std::string& text)
289 {
290 _v.insert(_v.begin()+pos, string_message::create(text));
291 }
292
293 void insert(size_t pos,std::string&& text)
294 {
295 _v.insert(_v.begin()+pos, string_message::create(move(text)));
296 }
297
298 void insert(size_t pos,std::shared_ptr<std::string> const& binary)
299 {
300 if(binary)
301 _v.insert(_v.begin()+pos, binary_message::create(binary));
302 }
303
304 void insert(size_t pos,std::shared_ptr<const std::string> const& binary)
305 {
306 if(binary)
307 _v.insert(_v.begin()+pos, binary_message::create(binary));
308 }
309
310 size_t size() const
311 {
312 return _v.size();
313 }
314
315 const message::ptr& at(size_t i) const
316 {
317 return _v[i];
318 }
319
320 const message::ptr& operator[] (size_t i) const
321 {
322 return _v[i];
323 }
324
325 std::vector<ptr>& get_vector()
326 {
327 return _v;
328 }
329
330 const std::vector<ptr>& get_vector() const
331 {
332 return _v;
333 }
334 };
335
336 class object_message : public message
337 {
338 std::map<std::string,message::ptr> _v;
339 object_message() : message(flag_object)
340 {
341 }
342 public:
343 static message::ptr create()
344 {
345 return ptr(new object_message());
346 }
347
348 void insert(const std::string & key,message::ptr const& message)
349 {
350 _v[key] = message;
351 }
352
353 void insert(const std::string & key,const std::string& text)
354 {
355 _v[key] = string_message::create(text);
356 }
357
358 void insert(const std::string & key,std::string&& text)
359 {
360 _v[key] = string_message::create(move(text));
361 }
362
363 void insert(const std::string & key,std::shared_ptr<std::string> const& binary)
364 {
365 if(binary)
366 _v[key] = binary_message::create(binary);
367 }
368
369 void insert(const std::string & key,std::shared_ptr<const std::string> const& binary)
370 {
371 if(binary)
372 _v[key] = binary_message::create(binary);
373 }
374
375 bool has(const std::string & key)
376 {
377 return _v.find(key) != _v.end();
378 }
379
380 const message::ptr& at(const std::string & key) const
381 {
382 static std::shared_ptr<message> not_found;
383
384 std::map<std::string,message::ptr>::const_iterator it = _v.find(key);
385 if (it != _v.cend()) return it->second;
386 return not_found;
387 }
388
389 const message::ptr& operator[] (const std::string & key) const
390 {
391 return at(key);
392 }
393
394 bool has(const std::string & key) const
395 {
396 return _v.find(key) != _v.end();
397 }
398
399 std::map<std::string,message::ptr>& get_map()
400 {
401 return _v;
402 }
403
404 const std::map<std::string,message::ptr>& get_map() const
405 {
406 return _v;
407 }
408 };
409
410 class message::list
411 {
412 public:
413 list()
414 {
415 }
416
417 list(std::nullptr_t)
418 {
419 }
420
421 list(message::list&& rhs):
422 m_vector(std::move(rhs.m_vector))
423 {
424
425 }
426
427 list & operator= (const message::list && rhs)
428 {
429 m_vector = std::move(rhs.m_vector);
430 return *this;
431 }
432
433 template <typename T>
434 list(T&& content,
435 typename std::enable_if<std::is_same<std::vector<message::ptr>,typename std::remove_reference<T>::type>::value>::type* = 0):
436 m_vector(std::forward<T>(content))
437 {
438 }
439
440 list(message::list const& rhs):
441 m_vector(rhs.m_vector)
442 {
443
444 }
445
446 list(message::ptr const& message)
447 {
448 if(message)
449 m_vector.push_back(message);
450
451 }
452
453 list(const std::string& text)
454 {
455 m_vector.push_back(string_message::create(text));
456 }
457
458 list(std::string&& text)
459 {
460 m_vector.push_back(string_message::create(move(text)));
461 }
462
463 list(std::shared_ptr<std::string> const& binary)
464 {
465 if(binary)
466 m_vector.push_back(binary_message::create(binary));
467 }
468
469 list(std::shared_ptr<const std::string> const& binary)
470 {
471 if(binary)
472 m_vector.push_back(binary_message::create(binary));
473 }
474
475 void push(message::ptr const& message)
476 {
477 if(message)
478 m_vector.push_back(message);
479 }
480
481 void push(const std::string& text)
482 {
483 m_vector.push_back(string_message::create(text));
484 }
485
486 void push(std::string&& text)
487 {
488 m_vector.push_back(string_message::create(move(text)));
489 }
490
491 void push(std::shared_ptr<std::string> const& binary)
492 {
493 if(binary)
494 m_vector.push_back(binary_message::create(binary));
495 }
496
497 void push(std::shared_ptr<const std::string> const& binary)
498 {
499 if(binary)
500 m_vector.push_back(binary_message::create(binary));
501 }
502
503 void insert(size_t pos,message::ptr const& message)
504 {
505 m_vector.insert(m_vector.begin()+pos, message);
506 }
507
508 void insert(size_t pos,const std::string& text)
509 {
510 m_vector.insert(m_vector.begin()+pos, string_message::create(text));
511 }
512
513 void insert(size_t pos,std::string&& text)
514 {
515 m_vector.insert(m_vector.begin()+pos, string_message::create(move(text)));
516 }
517
518 void insert(size_t pos,std::shared_ptr<std::string> const& binary)
519 {
520 if(binary)
521 m_vector.insert(m_vector.begin()+pos, binary_message::create(binary));
522 }
523
524 void insert(size_t pos,std::shared_ptr<const std::string> const& binary)
525 {
526 if(binary)
527 m_vector.insert(m_vector.begin()+pos, binary_message::create(binary));
528 }
529
530 size_t size() const
531 {
532 return m_vector.size();
533 }
534
535 const message::ptr& at(size_t i) const
536 {
537 return m_vector[i];
538 }
539
540 const message::ptr& operator[] (size_t i) const
541 {
542 return m_vector[i];
543 }
544
545 message::ptr to_array_message(std::string const& event_name) const
546 {
547 message::ptr arr = array_message::create();
548 arr->get_vector().push_back(string_message::create(event_name));
549 arr->get_vector().insert(arr->get_vector().end(),m_vector.begin(),m_vector.end());
550 return arr;
551 }
552
553 message::ptr to_array_message() const
554 {
555 message::ptr arr = array_message::create();
556 arr->get_vector().insert(arr->get_vector().end(),m_vector.begin(),m_vector.end());
557 return arr;
558 }
559
560 private:
561 std::vector<message::ptr> m_vector;
562 };
563}
564
565#endif
EEmoShapeIORequestContentType::binary
@ binary
emoshapeio::array_message
Definition sio_message.h:243
emoshapeio::array_message::insert
void insert(size_t pos, std::shared_ptr< std::string > const &binary)
Definition sio_message.h:298
emoshapeio::array_message::push
void push(const std::string &text)
Definition sio_message.h:261
emoshapeio::array_message::push
void push(std::shared_ptr< const std::string > const &binary)
Definition sio_message.h:277
emoshapeio::array_message::operator[]
const message::ptr & operator[](size_t i) const
Definition sio_message.h:320
emoshapeio::array_message::get_vector
std::vector< ptr > & get_vector()
Definition sio_message.h:325
emoshapeio::array_message::push
void push(std::string &&text)
Definition sio_message.h:266
emoshapeio::array_message::size
size_t size() const
Definition sio_message.h:310
emoshapeio::array_message::push
void push(std::shared_ptr< std::string > const &binary)
Definition sio_message.h:271
emoshapeio::array_message::at
const message::ptr & at(size_t i) const
Definition sio_message.h:315
emoshapeio::array_message::insert
void insert(size_t pos, const std::string &text)
Definition sio_message.h:288
emoshapeio::array_message::push
void push(message::ptr const &message)
Definition sio_message.h:255
emoshapeio::array_message::insert
void insert(size_t pos, std::string &&text)
Definition sio_message.h:293
emoshapeio::array_message::create
static message::ptr create()
Definition sio_message.h:250
emoshapeio::array_message::insert
void insert(size_t pos, message::ptr const &message)
Definition sio_message.h:283
emoshapeio::array_message::insert
void insert(size_t pos, std::shared_ptr< const std::string > const &binary)
Definition sio_message.h:304
emoshapeio::array_message::get_vector
const std::vector< ptr > & get_vector() const
Definition sio_message.h:330
emoshapeio::binary_message
Definition sio_message.h:224
emoshapeio::binary_message::get_binary
std::shared_ptr< const std::string > const & get_binary() const
Definition sio_message.h:236
emoshapeio::binary_message::create
static message::ptr create(std::shared_ptr< const std::string > const &v)
Definition sio_message.h:231
emoshapeio::bool_message
Definition sio_message.h:127
emoshapeio::bool_message::get_bool
bool get_bool() const
Definition sio_message.h:142
emoshapeio::bool_message::bool_message
bool_message(bool v)
Definition sio_message.h:131
emoshapeio::bool_message::create
static message::ptr create(bool v)
Definition sio_message.h:137
emoshapeio::client
Definition sio_client.h:13
emoshapeio::double_message
Definition sio_message.h:175
emoshapeio::double_message::get_double
double get_double() const
Definition sio_message.h:188
emoshapeio::double_message::create
static message::ptr create(double v)
Definition sio_message.h:183
emoshapeio::int_message
Definition sio_message.h:149
emoshapeio::int_message::get_int
int64_t get_int() const
Definition sio_message.h:163
emoshapeio::int_message::int_message
int_message(int64_t v)
Definition sio_message.h:152
emoshapeio::int_message::create
static message::ptr create(int64_t v)
Definition sio_message.h:158
emoshapeio::int_message::get_double
double get_double() const
Definition sio_message.h:168
emoshapeio::message::list
Definition sio_message.h:411
emoshapeio::message::list::list
list(message::ptr const &message)
Definition sio_message.h:446
emoshapeio::message::list::to_array_message
message::ptr to_array_message() const
Definition sio_message.h:553
emoshapeio::message::list::list
list(const std::string &text)
Definition sio_message.h:453
emoshapeio::message::list::list
list(std::string &&text)
Definition sio_message.h:458
emoshapeio::message::list::size
size_t size() const
Definition sio_message.h:530
emoshapeio::message::list::insert
void insert(size_t pos, const std::string &text)
Definition sio_message.h:508
emoshapeio::message::list::push
void push(const std::string &text)
Definition sio_message.h:481
emoshapeio::message::list::operator[]
const message::ptr & operator[](size_t i) const
Definition sio_message.h:540
emoshapeio::message::list::list
list(std::shared_ptr< std::string > const &binary)
Definition sio_message.h:463
emoshapeio::message::list::operator=
list & operator=(const message::list &&rhs)
Definition sio_message.h:427
emoshapeio::message::list::insert
void insert(size_t pos, std::shared_ptr< std::string > const &binary)
Definition sio_message.h:518
emoshapeio::message::list::to_array_message
message::ptr to_array_message(std::string const &event_name) const
Definition sio_message.h:545
emoshapeio::message::list::at
const message::ptr & at(size_t i) const
Definition sio_message.h:535
emoshapeio::message::list::insert
void insert(size_t pos, std::shared_ptr< const std::string > const &binary)
Definition sio_message.h:524
emoshapeio::message::list::push
void push(std::shared_ptr< const std::string > const &binary)
Definition sio_message.h:497
emoshapeio::message::list::list
list(message::list const &rhs)
Definition sio_message.h:440
emoshapeio::message::list::list
list(message::list &&rhs)
Definition sio_message.h:421
emoshapeio::message::list::list
list()
Definition sio_message.h:413
emoshapeio::message::list::insert
void insert(size_t pos, std::string &&text)
Definition sio_message.h:513
emoshapeio::message::list::list
list(T &&content, typename std::enable_if< std::is_same< std::vector< message::ptr >, typename std::remove_reference< T >::type >::value >::type *=0)
Definition sio_message.h:434
emoshapeio::message::list::list
list(std::shared_ptr< const std::string > const &binary)
Definition sio_message.h:469
emoshapeio::message::list::list
list(std::nullptr_t)
Definition sio_message.h:417
emoshapeio::message::list::push
void push(std::string &&text)
Definition sio_message.h:486
emoshapeio::message::list::insert
void insert(size_t pos, message::ptr const &message)
Definition sio_message.h:503
emoshapeio::message::list::push
void push(std::shared_ptr< std::string > const &binary)
Definition sio_message.h:491
emoshapeio::message::list::push
void push(message::ptr const &message)
Definition sio_message.h:475
emoshapeio::message
Definition sio_message.h:14
emoshapeio::message::get_int
virtual int64_t get_int() const
Definition sio_message.h:45
emoshapeio::message::get_map
virtual const std::map< std::string, message::ptr > & get_map() const
Definition sio_message.h:89
emoshapeio::message::get_double
virtual double get_double() const
Definition sio_message.h:51
emoshapeio::message::message
message(flag f)
Definition sio_message.h:108
emoshapeio::message::~message
virtual ~message()
Definition sio_message.h:28
emoshapeio::message::get_bool
virtual bool get_bool() const
Definition sio_message.h:39
emoshapeio::message::get_vector
virtual const std::vector< ptr > & get_vector() const
Definition sio_message.h:73
emoshapeio::message::get_string
virtual std::string const & get_string() const
Definition sio_message.h:57
emoshapeio::message::flag
flag
Definition sio_message.h:17
emoshapeio::message::flag_object
@ flag_object
Definition sio_message.h:23
emoshapeio::message::flag_string
@ flag_string
Definition sio_message.h:20
emoshapeio::message::flag_boolean
@ flag_boolean
Definition sio_message.h:24
emoshapeio::message::flag_null
@ flag_null
Definition sio_message.h:25
emoshapeio::message::flag_integer
@ flag_integer
Definition sio_message.h:18
emoshapeio::message::flag_double
@ flag_double
Definition sio_message.h:19
emoshapeio::message::flag_array
@ flag_array
Definition sio_message.h:22
emoshapeio::message::flag_binary
@ flag_binary
Definition sio_message.h:21
emoshapeio::message::get_flag
flag get_flag() const
Definition sio_message.h:32
emoshapeio::message::get_vector
virtual std::vector< ptr > & get_vector()
Definition sio_message.h:81
emoshapeio::message::get_map
virtual std::map< std::string, message::ptr > & get_map()
Definition sio_message.h:97
emoshapeio::message::get_binary
virtual std::shared_ptr< const std::string > const & get_binary() const
Definition sio_message.h:65
emoshapeio::message::ptr
std::shared_ptr< message > ptr
Definition sio_message.h:37
emoshapeio::null_message
Definition sio_message.h:112
emoshapeio::null_message::null_message
null_message()
Definition sio_message.h:114
emoshapeio::null_message::create
static message::ptr create()
Definition sio_message.h:120
emoshapeio::object_message
Definition sio_message.h:337
emoshapeio::object_message::get_map
const std::map< std::string, message::ptr > & get_map() const
Definition sio_message.h:404
emoshapeio::object_message::insert
void insert(const std::string &key, const std::string &text)
Definition sio_message.h:353
emoshapeio::object_message::operator[]
const message::ptr & operator[](const std::string &key) const
Definition sio_message.h:389
emoshapeio::object_message::insert
void insert(const std::string &key, std::shared_ptr< std::string > const &binary)
Definition sio_message.h:363
emoshapeio::object_message::at
const message::ptr & at(const std::string &key) const
Definition sio_message.h:380
emoshapeio::object_message::get_map
std::map< std::string, message::ptr > & get_map()
Definition sio_message.h:399
emoshapeio::object_message::insert
void insert(const std::string &key, message::ptr const &message)
Definition sio_message.h:348
emoshapeio::object_message::insert
void insert(const std::string &key, std::shared_ptr< const std::string > const &binary)
Definition sio_message.h:369
emoshapeio::object_message::has
bool has(const std::string &key) const
Definition sio_message.h:394
emoshapeio::object_message::has
bool has(const std::string &key)
Definition sio_message.h:375
emoshapeio::object_message::create
static message::ptr create()
Definition sio_message.h:343
emoshapeio::object_message::insert
void insert(const std::string &key, std::string &&text)
Definition sio_message.h:358
emoshapeio::string_message
Definition sio_message.h:195
emoshapeio::string_message::create
static message::ptr create(std::string const &v)
Definition sio_message.h:207
emoshapeio::string_message::get_string
std::string const & get_string() const
Definition sio_message.h:217
emoshapeio::string_message::create
static message::ptr create(std::string &&v)
Definition sio_message.h:212
emoshapeio
Definition sio_client_impl.cpp:51