1
0

sio_message.h 13 KB

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