1
0

sio_message.h 14 KB

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