common_tool.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #ifndef COMMON_TOOL_H
  2. #define COMMON_TOOL_H
  3. /**
  4. * @brief 基楚的工具类
  5. * @author 戴月腾
  6. * @date 2018-09-15
  7. */
  8. #include <chrono>
  9. #include <string>
  10. #include <sys/time.h>
  11. #include <rapidjson/document.h>
  12. #include <rapidjson/writer.h>
  13. #include <rapidjson/stringbuffer.h>
  14. #include <rapidjson/prettywriter.h>
  15. #include <boost/function.hpp>
  16. #include <boost/bind.hpp>
  17. #include "websocket/sio/sio_client.h"
  18. #include "common.h"
  19. class tool_other
  20. {
  21. public:
  22. static std::string to13str(std::string& str)
  23. {
  24. uint64_t tmp = std::stoull(str);
  25. return to13str(tmp);
  26. }
  27. static std::string to13str(uint64_t data)
  28. {
  29. char ss[20]={0};
  30. sprintf(ss, "%013ld", data);
  31. return std::string(ss);
  32. }
  33. static uint32_t id64_to_id(std::string& str)
  34. {
  35. return static_cast<uint32_t>(std::stoul(to13str(str).substr(3)));
  36. }
  37. static int id64_to_type(std::string& str)
  38. {
  39. return std::stoi(to13str(str).substr(0, 3));
  40. }
  41. static int card_id_to_type(const std::string &cardid)
  42. {
  43. return std::stoi(cardid.substr(0,3));
  44. }
  45. static int card_id_to_id(const std::string &cardid)
  46. {
  47. return atoi(cardid.substr(3).c_str());
  48. }
  49. static std::string type_id_to_str(int32_t type,uint32_t id)
  50. {
  51. char sql[15] = {0};
  52. snprintf(sql, 15,"%03d%010d", type, id);
  53. return std::string(sql);
  54. }
  55. static uint64_t type_id_to_u64(uint64_t type,uint32_t id)
  56. {
  57. return type<<32|id;
  58. }
  59. static bool is_person(int32_t type)
  60. {
  61. return CT_PERSON == type;
  62. }
  63. static bool is_vehicle(int32_t type)
  64. {
  65. return CT_VEHICLE == type || CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
  66. }
  67. static std::string get_string_cardid(uint64_t id)
  68. {
  69. uint64_t type = id>>32;
  70. uint32_t cid = id & (~(type<<32));
  71. return type_id_to_str(type,cid);
  72. }
  73. static uint64_t card_id_to_u64(const std::string & cardid)
  74. {
  75. return type_id_to_u64(card_id_to_type(cardid),card_id_to_id(cardid));
  76. }
  77. };
  78. class tool_map
  79. {
  80. public:
  81. static bool try_get_value(sio::message::ptr& out_data,
  82. const char* key, sio::message::ptr const& data)
  83. {
  84. auto map=data->get_map()[key];
  85. if(map && sio::message::flag_object == map->get_flag())
  86. {
  87. out_data = map;
  88. return true;
  89. }
  90. return false;
  91. }
  92. static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
  93. {
  94. auto map=data->get_map()[key];
  95. if(map && sio::message::flag_integer == map->get_flag())
  96. {
  97. out_data = map->get_int();
  98. return true;
  99. }
  100. return false;
  101. }
  102. static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
  103. {
  104. auto map=data->get_map()[key];
  105. if(map && sio::message::flag_integer == map->get_flag())
  106. {
  107. out_data = static_cast<uint32_t>(map->get_int());
  108. return true;
  109. }
  110. return false;
  111. }
  112. static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
  113. {
  114. auto map=data->get_map()[key];
  115. if(map && sio::message::flag_integer == map->get_flag())
  116. {
  117. out_data = static_cast<int>(map->get_int());
  118. return true;
  119. }
  120. return false;
  121. }
  122. static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
  123. {
  124. auto map=data->get_map()[key];
  125. if(map && sio::message::flag_string == map->get_flag())
  126. {
  127. out_data = map->get_string();
  128. return true;
  129. }
  130. return false;
  131. }
  132. static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
  133. {
  134. auto map=data->get_map()[key];
  135. if(map && sio::message::flag_double == map->get_flag())
  136. {
  137. out_data = map->get_double();
  138. return true;
  139. }
  140. return false;
  141. }
  142. static bool try_get_value(std::vector<sio::message::ptr>& out_data,
  143. const char* key, sio::message::ptr const& data)
  144. {
  145. auto map=data->get_map()[key];
  146. if(map && sio::message::flag_array == map->get_flag())
  147. {
  148. out_data = map->get_vector();
  149. return true;
  150. }
  151. return false;
  152. }
  153. };
  154. class tool_json
  155. {
  156. public:
  157. static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
  158. rapidjson::Document::AllocatorType& allocator)
  159. {
  160. rapidjson::Value name;
  161. name.SetString(key, allocator);
  162. rapidjson::Value data;
  163. data.SetString(value.c_str(), allocator);
  164. out_data.AddMember(name, data, allocator);
  165. }
  166. static void push_back(rapidjson::Value& out_data, std::string value,
  167. rapidjson::Document::AllocatorType& allocator)
  168. {
  169. rapidjson::Value data;
  170. data.SetString(value.c_str(), allocator);
  171. out_data.PushBack(data, allocator);
  172. }
  173. static bool try_get_iter(const char* key, const rapidjson::Value& node,
  174. rapidjson::Value::ConstMemberIterator& out_iter)
  175. {
  176. if(node.IsObject())
  177. {
  178. out_iter = node.FindMember(key);
  179. if(node.MemberEnd() == out_iter)
  180. {
  181. return false;
  182. }
  183. return true;
  184. }
  185. return false;
  186. }
  187. static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
  188. {
  189. rapidjson::Value::ConstMemberIterator iter;
  190. if(try_get_iter(key, node, iter))
  191. {
  192. if(iter->value.IsInt())
  193. {
  194. d = iter->value.GetInt();
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
  201. {
  202. rapidjson::Value::ConstMemberIterator iter;
  203. if(try_get_iter(key, node, iter))
  204. {
  205. if(iter->value.IsUint64())
  206. {
  207. d = iter->value.GetUint64();
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
  214. {
  215. rapidjson::Value::ConstMemberIterator iter;
  216. if(try_get_iter(key, node, iter))
  217. {
  218. if(iter->value.IsDouble())
  219. {
  220. d = iter->value.GetDouble();
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226. static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
  227. {
  228. rapidjson::Value::ConstMemberIterator iter;
  229. if(try_get_iter(key, node, iter))
  230. {
  231. if(iter->value.IsString())
  232. {
  233. d = iter->value.GetString();
  234. return true;
  235. }
  236. }
  237. return false;
  238. }
  239. static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
  240. {
  241. rapidjson::Value::ConstMemberIterator iter;
  242. if(try_get_iter(key, node, iter))
  243. {
  244. if(iter->value.IsInt())
  245. {
  246. return iter->value.GetInt();
  247. }
  248. }
  249. return default_data;
  250. }
  251. static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
  252. {
  253. rapidjson::Value::ConstMemberIterator iter;
  254. if(try_get_iter(key, node, iter))
  255. {
  256. if(iter->value.IsString())
  257. {
  258. return iter->value.GetString();
  259. }
  260. }
  261. return default_data;
  262. }
  263. static std::string doc_to_json(rapidjson::Document& doc)
  264. {
  265. rapidjson::StringBuffer sb;
  266. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  267. doc.Accept(writer);
  268. return sb.GetString();
  269. }
  270. };
  271. #endif // COMMON_TOOL_H