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