common_tool.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. //是否系统网络字节顺序 true=大端(低位在前,高位在后) false=小端(高位在前,低位在后)
  74. static bool IsBigEnd()
  75. {
  76. static int g_big = -1;
  77. if (g_big == -1)
  78. {
  79. g_big = 1;
  80. int x=0x12345678; /* 305419896 */
  81. unsigned char *p=(unsigned char *)&x;
  82. if (p[0] == 0x78) {
  83. g_big = 0;
  84. }
  85. }
  86. return g_big == 1;
  87. }
  88. static uint64_t card_id_to_u64(const std::string & cardid)
  89. {
  90. return type_id_to_u64(card_id_to_type(cardid),card_id_to_id(cardid));
  91. }
  92. };
  93. class tool_map
  94. {
  95. public:
  96. static bool try_get_value(sio::message::ptr& out_data,
  97. const char* key, sio::message::ptr const& data)
  98. {
  99. auto map=data->get_map()[key];
  100. if(map && sio::message::flag_object == map->get_flag())
  101. {
  102. out_data = map;
  103. return true;
  104. }
  105. return false;
  106. }
  107. static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
  108. {
  109. auto map=data->get_map()[key];
  110. if(map && sio::message::flag_integer == map->get_flag())
  111. {
  112. out_data = map->get_int();
  113. return true;
  114. }
  115. return false;
  116. }
  117. static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
  118. {
  119. auto map=data->get_map()[key];
  120. if(map && sio::message::flag_integer == map->get_flag())
  121. {
  122. out_data = static_cast<uint32_t>(map->get_int());
  123. return true;
  124. }
  125. return false;
  126. }
  127. static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
  128. {
  129. auto map=data->get_map()[key];
  130. if(map && sio::message::flag_integer == map->get_flag())
  131. {
  132. out_data = static_cast<int>(map->get_int());
  133. return true;
  134. }
  135. return false;
  136. }
  137. static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
  138. {
  139. auto map=data->get_map()[key];
  140. if(map && sio::message::flag_string == map->get_flag())
  141. {
  142. out_data = map->get_string();
  143. return true;
  144. }
  145. return false;
  146. }
  147. static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
  148. {
  149. auto map=data->get_map()[key];
  150. if(map && sio::message::flag_double == map->get_flag())
  151. {
  152. out_data = map->get_double();
  153. return true;
  154. }
  155. return false;
  156. }
  157. static bool try_get_value(std::vector<sio::message::ptr>& out_data,
  158. const char* key, sio::message::ptr const& data)
  159. {
  160. auto map=data->get_map()[key];
  161. if(map && sio::message::flag_array == map->get_flag())
  162. {
  163. out_data = map->get_vector();
  164. return true;
  165. }
  166. return false;
  167. }
  168. };
  169. class tool_json
  170. {
  171. public:
  172. static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
  173. rapidjson::Document::AllocatorType& allocator)
  174. {
  175. rapidjson::Value name;
  176. name.SetString(key, allocator);
  177. rapidjson::Value data;
  178. data.SetString(value.c_str(), allocator);
  179. out_data.AddMember(name, data, allocator);
  180. }
  181. static void push_back(rapidjson::Value& out_data, std::string value,
  182. rapidjson::Document::AllocatorType& allocator)
  183. {
  184. rapidjson::Value data;
  185. data.SetString(value.c_str(), allocator);
  186. out_data.PushBack(data, allocator);
  187. }
  188. static bool try_get_iter(const char* key, const rapidjson::Value& node,
  189. rapidjson::Value::ConstMemberIterator& out_iter)
  190. {
  191. if(node.IsObject())
  192. {
  193. out_iter = node.FindMember(key);
  194. if(node.MemberEnd() == out_iter)
  195. {
  196. return false;
  197. }
  198. return true;
  199. }
  200. return false;
  201. }
  202. static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
  203. {
  204. rapidjson::Value::ConstMemberIterator iter;
  205. if(try_get_iter(key, node, iter))
  206. {
  207. if(iter->value.IsInt())
  208. {
  209. d = iter->value.GetInt();
  210. return true;
  211. }
  212. }
  213. return false;
  214. }
  215. static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
  216. {
  217. rapidjson::Value::ConstMemberIterator iter;
  218. if(try_get_iter(key, node, iter))
  219. {
  220. if(iter->value.IsUint64())
  221. {
  222. d = iter->value.GetUint64();
  223. return true;
  224. }
  225. }
  226. return false;
  227. }
  228. static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
  229. {
  230. rapidjson::Value::ConstMemberIterator iter;
  231. if(try_get_iter(key, node, iter))
  232. {
  233. if(iter->value.IsDouble())
  234. {
  235. d = iter->value.GetDouble();
  236. return true;
  237. }
  238. }
  239. return false;
  240. }
  241. static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
  242. {
  243. rapidjson::Value::ConstMemberIterator iter;
  244. if(try_get_iter(key, node, iter))
  245. {
  246. if(iter->value.IsString())
  247. {
  248. d = iter->value.GetString();
  249. return true;
  250. }
  251. }
  252. return false;
  253. }
  254. static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
  255. {
  256. rapidjson::Value::ConstMemberIterator iter;
  257. if(try_get_iter(key, node, iter))
  258. {
  259. if(iter->value.IsInt())
  260. {
  261. return iter->value.GetInt();
  262. }
  263. }
  264. return default_data;
  265. }
  266. static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
  267. {
  268. rapidjson::Value::ConstMemberIterator iter;
  269. if(try_get_iter(key, node, iter))
  270. {
  271. if(iter->value.IsString())
  272. {
  273. return iter->value.GetString();
  274. }
  275. }
  276. return default_data;
  277. }
  278. static std::string doc_to_json(rapidjson::Document& doc)
  279. {
  280. rapidjson::StringBuffer sb;
  281. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  282. doc.Accept(writer);
  283. return sb.GetString();
  284. }
  285. };
  286. #endif // COMMON_TOOL_H