123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- #ifndef COMMON_TOOL_H
- #define COMMON_TOOL_H
- #include <chrono>
- #include <string>
- #include <sys/time.h>
- #include <rapidjson/document.h>
- #include <rapidjson/writer.h>
- #include <rapidjson/stringbuffer.h>
- #include <rapidjson/prettywriter.h>
- #include <boost/function.hpp>
- #include <boost/bind.hpp>
- #include "websocket/sio/sio_client.h"
- #include "common.h"
- class tool_other
- {
- public:
- static double round(double dVal, int iPlaces)
- {
- double dRetval;
- double dMod = 0.0000001;
- if (dVal<0.0) dMod=-0.0000001;
- dRetval=dVal;
- dRetval+=(5.0/pow(10.0,iPlaces+1.0));
- dRetval*=pow(10.0,iPlaces);
- dRetval=floor(dRetval+dMod);
- dRetval/=pow(10.0,iPlaces);
- return(dRetval);
- }
- static std::string to13str(std::string& str)
- {
- if(str.size()<13)
- str.insert(0,13-str.size(),'0');
- return str;
- }
- static std::string to13str(uint64_t data)
- {
- char ss[20]={0};
- sprintf(ss, "%013ld", data);
- return std::string(ss);
- }
- static uint32_t id64_to_id(std::string& str)
- {
- return static_cast<uint32_t>(std::stoul(to13str(str).substr(3)));
- }
- static int id64_to_type(std::string& str)
- {
- return std::stoi(to13str(str).substr(0, 3));
- }
- static int card_id_to_type(const std::string &cardid)
- {
- return std::stoi(cardid.substr(0,3));
- }
- static int card_id_to_id(const std::string &cardid)
- {
- return atoi(cardid.substr(3).c_str());
- }
- static std::string type_id_to_str(int32_t type,uint32_t id)
- {
- char sql[15] = {0};
- snprintf(sql, 15,"%03d%010d", type, id);
- return std::string(sql);
- }
- static uint64_t type_id_to_u64(uint64_t type,uint32_t id)
- {
- return type<<32|id;
- }
- static bool is_person(int32_t type)
- {
- return CT_PERSON == type;
- }
- static bool is_vehicle(int32_t type)
- {
- return CT_VEHICLE == type || CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
- }
- static std::string get_string_cardid(uint64_t id)
- {
- uint64_t type = id>>32;
- uint32_t cid = id & (~(type<<32));
- return type_id_to_str(type,cid);
- }
- static uint64_t card_id_to_u64(const std::string & cardid)
- {
- return type_id_to_u64(card_id_to_type(cardid),card_id_to_id(cardid));
- }
- static bool is_coal(int32_t type,int32_t vtype)
- {
- return CT_COAL_CUTTER == type || vtype==VT_COAL_CUTTER;
- }
- static bool is_driving(int32_t type,int32_t vtype)
- {
- return CT_HEADING_MACHINE == type || vtype==VT_HEADING_MACHINE;
- }
- static bool is_coal_or_driving(int32_t type,int32_t vtype)
- {
- return is_coal(type,vtype) || is_driving(type,vtype);
- }
- };
- class tool_map
- {
- public:
- static bool try_get_value(sio::message::ptr& out_data,
- const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_object == map->get_flag())
- {
- out_data = map;
- return true;
- }
- return false;
- }
- static bool try_get_value(int64_t& out_data, const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_integer == map->get_flag())
- {
- out_data = map->get_int();
- return true;
- }
- return false;
- }
- static bool try_get_value(uint32_t& out_data, const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_integer == map->get_flag())
- {
- out_data = static_cast<uint32_t>(map->get_int());
- return true;
- }
- return false;
- }
- static bool try_get_value(int& out_data, const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_integer == map->get_flag())
- {
- out_data = static_cast<int>(map->get_int());
- return true;
- }
- return false;
- }
- static bool try_get_value(std::string& out_data, const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_string == map->get_flag())
- {
- out_data = map->get_string();
- return true;
- }
- return false;
- }
- static bool try_get_value(double& out_data, const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_double == map->get_flag())
- {
- out_data = map->get_double();
- return true;
- }
- return false;
- }
- static bool try_get_value(std::vector<sio::message::ptr>& out_data,
- const char* key, sio::message::ptr const& data)
- {
- auto map=data->get_map()[key];
- if(map && sio::message::flag_array == map->get_flag())
- {
- out_data = map->get_vector();
- return true;
- }
- return false;
- }
- };
- class tool_json
- {
- public:
- static void add_member(rapidjson::Value& out_data, const char* key, std::string value,
- rapidjson::Document::AllocatorType& allocator)
- {
- rapidjson::Value name;
- name.SetString(key, allocator);
- rapidjson::Value data;
- data.SetString(value.c_str(), allocator);
- out_data.AddMember(name, data, allocator);
- }
- static void push_back(rapidjson::Value& out_data, std::string value,
- rapidjson::Document::AllocatorType& allocator)
- {
- rapidjson::Value data;
- data.SetString(value.c_str(), allocator);
- out_data.PushBack(data, allocator);
- }
- static bool try_get_iter(const char* key, const rapidjson::Value& node,
- rapidjson::Value::ConstMemberIterator& out_iter)
- {
- if(node.IsObject())
- {
- out_iter = node.FindMember(key);
- if(node.MemberEnd() == out_iter)
- {
- return false;
- }
- return true;
- }
- return false;
- }
- static bool try_get_value(int& d, const char* key, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsInt())
- {
- d = iter->value.GetInt();
- return true;
- }
- }
- return false;
- }
- static bool try_get_value(uint64_t& d, const char* key, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsUint64())
- {
- d = iter->value.GetUint64();
- return true;
- }
- }
- return false;
- }
- static bool try_get_value(double& d, const char* key, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsDouble())
- {
- d = iter->value.GetDouble();
- return true;
- }
- }
- return false;
- }
- static bool try_get_value(std::string& d, const char* key, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsString())
- {
- d = iter->value.GetString();
- return true;
- }
- }
- return false;
- }
- static int get_value(const char* key, const int& default_data, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsInt())
- {
- return iter->value.GetInt();
- }
- }
- return default_data;
- }
- static std::string get_value(const char* key, const std::string& default_data, const rapidjson::Value& node)
- {
- rapidjson::Value::ConstMemberIterator iter;
- if(try_get_iter(key, node, iter))
- {
- if(iter->value.IsString())
- {
- return iter->value.GetString();
- }
- }
- return default_data;
- }
- static std::string doc_to_json(rapidjson::Document& doc)
- {
- rapidjson::StringBuffer sb;
- rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
- doc.Accept(writer);
- return sb.GetString();
- }
- };
- #endif // COMMON_TOOL_H
|