Commit 74e024b9 authored by Alexander Lapshin's avatar Alexander Lapshin

+ get_ts, json loading data types, refactoring

parent c309791e
...@@ -55,6 +55,30 @@ inline bool extract_value(const jsonval& parent, const std::string& key, bool ex ...@@ -55,6 +55,30 @@ inline bool extract_value(const jsonval& parent, const std::string& key, bool ex
return true; return true;
} }
inline bool extract_value(const std::map<std::string, std::string>& parent, const std::string& key, bool exist, bool* loaded, std::map<std::string, std::string>::const_iterator& result)
{
const auto itr = parent.find(key);
if (itr == parent.end() && exist) {
throw Exp() << "error: element '" << key << "' not found";
}
if (itr == parent.end() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
result = itr;
if (loaded) {
*loaded = true;
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<std::string>& vec, const bool exist = true, bool* loaded = nullptr) static bool loadjson(const jsonval& parent, const std::string& key, std::vector<std::string>& vec, const bool exist = true, bool* loaded = nullptr)
{ {
jsonval::ConstMemberIterator itr; jsonval::ConstMemberIterator itr;
...@@ -162,7 +186,8 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector< ...@@ -162,7 +186,8 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) { for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
if (it->IsDouble()) { if (it->IsDouble()) {
vec.push_back(it->GetDouble()); vec.push_back(it->GetDouble());
}else if (it->IsInt()) { }
else if (it->IsInt()) {
vec.push_back(it->GetInt()); vec.push_back(it->GetInt());
} }
else { else {
...@@ -173,8 +198,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector< ...@@ -173,8 +198,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
return true; return true;
} }
template <typename T> template <typename T> bool loadjson(const jsonval& parent, std::string key, std::vector<T>& vec, const bool exist = true, bool* loaded = nullptr)
bool loadjson(const jsonval& parent, std::string key, std::vector<T>& vec, const bool exist = true, bool* loaded = nullptr)
{ {
jsonval::ConstMemberIterator itr; jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) { if (!extract_value(parent, key, exist, loaded, itr)) {
...@@ -196,8 +220,7 @@ bool loadjson(const jsonval& parent, std::string key, std::vector<T>& vec, const ...@@ -196,8 +220,7 @@ bool loadjson(const jsonval& parent, std::string key, std::vector<T>& vec, const
return true; return true;
} }
template <typename T, typename Tid> template <typename T, typename Tid> bool loadjson(const jsonval& parent, const std::string key, std::map<Tid, T>& map, const bool exist = true, bool* loaded = nullptr)
bool loadjson(const jsonval& parent, const std::string key, std::map<Tid, T>& map, const bool exist = true, bool* loaded = nullptr)
{ {
jsonval::ConstMemberIterator itr; jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) { if (!extract_value(parent, key, exist, loaded, itr)) {
...@@ -219,8 +242,7 @@ bool loadjson(const jsonval& parent, const std::string key, std::map<Tid, T>& ma ...@@ -219,8 +242,7 @@ bool loadjson(const jsonval& parent, const std::string key, std::map<Tid, T>& ma
return true; return true;
} }
template <typename T> template <typename T> bool loadjson(const jsonval& parent, std::string key, T& item, void (*f)(const jsonval&, T&), const bool exist = true, bool* loaded = nullptr)
bool loadjson(const jsonval& parent, std::string key, T& item, void (*f)(const jsonval&, T&), const bool exist = true, bool* loaded = nullptr)
{ {
jsonval::ConstMemberIterator itr; jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) { if (!extract_value(parent, key, exist, loaded, itr)) {
...@@ -238,8 +260,7 @@ bool loadjson(const jsonval& parent, std::string key, T& item, void (*f)(const j ...@@ -238,8 +260,7 @@ bool loadjson(const jsonval& parent, std::string key, T& item, void (*f)(const j
return true; return true;
} }
template <typename T> template <typename T> bool loadjson(const jsonval& parent, const std::string& key, T& item, const bool exist = true, bool* loaded = nullptr)
bool loadjson(const jsonval& parent, const std::string& key, T& item, const bool exist = true, bool* loaded = nullptr)
{ {
jsonval::ConstMemberIterator itr; jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) { if (!extract_value(parent, key, exist, loaded, itr)) {
...@@ -257,8 +278,7 @@ bool loadjson(const jsonval& parent, const std::string& key, T& item, const bool ...@@ -257,8 +278,7 @@ bool loadjson(const jsonval& parent, const std::string& key, T& item, const bool
return true; return true;
} }
template <typename T> template <typename T> void loadjson(const jsonval& parent, std::vector<T>& vec)
void loadjson(const jsonval& parent, std::vector<T>& vec)
{ {
if (parent.IsArray()) { if (parent.IsArray()) {
for (rapidjson::SizeType i = 0; i < parent.Size(); i++) { for (rapidjson::SizeType i = 0; i < parent.Size(); i++) {
...@@ -279,8 +299,7 @@ void loadjson(const jsonval& parent, std::vector<T>& vec) ...@@ -279,8 +299,7 @@ void loadjson(const jsonval& parent, std::vector<T>& vec)
} }
} }
template <typename T, typename T2> template <typename T, typename T2> void loadjson(const jsonval& parent, std::map<T2, T>& map)
void loadjson(const jsonval& parent, std::map<T2, T>& map)
{ {
if (parent.IsArray()) { if (parent.IsArray()) {
for (rapidjson::SizeType i = 0; i < parent.Size(); i++) { for (rapidjson::SizeType i = 0; i < parent.Size(); i++) {
...@@ -526,8 +545,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::string& ...@@ -526,8 +545,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::string&
return true; return true;
} }
template <typename T> template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const T& object, void (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const T& object, void (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{ {
jsonval obj(rapidjson::kObjectType); jsonval obj(rapidjson::kObjectType);
f(alc, obj, object); f(alc, obj, object);
...@@ -536,8 +554,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const T& ...@@ -536,8 +554,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const T&
ss.AddMember(jname, obj, alc); ss.AddMember(jname, obj, alc);
} }
template <typename T> template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const std::vector<T>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const std::vector<T>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{ {
jsonval jsvec(rapidjson::kArrayType); jsonval jsvec(rapidjson::kArrayType);
for (auto it = vec.begin(); it != vec.end(); ++it) { for (auto it = vec.begin(); it != vec.end(); ++it) {
...@@ -551,8 +568,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const std ...@@ -551,8 +568,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const std
ss.AddMember(jname, jsvec, alc); ss.AddMember(jname, jsvec, alc);
} }
template <typename T> template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std::vector<T*>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std::vector<T*>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{ {
jsonval jsvec(rapidjson::kArrayType); jsonval jsvec(rapidjson::kArrayType);
for (typename std::vector<T*>::const_iterator it = vec.begin(); it != vec.end(); ++it) { for (typename std::vector<T*>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
...@@ -566,8 +582,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std: ...@@ -566,8 +582,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std:
ss.AddMember(jname, jsvec, alc); ss.AddMember(jname, jsvec, alc);
} }
template <typename T, typename TK> template <typename T, typename TK> void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std::map<TK, T>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std::map<TK, T>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{ {
jsonval jsvec(rapidjson::kObjectType); jsonval jsvec(rapidjson::kObjectType);
for (typename std::map<TK, T>::const_iterator it = vec.begin(); it != vec.end(); ++it) { for (typename std::map<TK, T>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
...@@ -586,8 +601,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std: ...@@ -586,8 +601,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std:
ss.AddMember(jname, jsvec, alc); ss.AddMember(jname, jsvec, alc);
} }
template <typename T, typename TK> template <typename T, typename TK> void PutMapAsVec(jsonalloc& alc, jsonval& ss, const std::string conName, const std::map<TK, T>& vec, bool (*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
void PutMapAsVec(jsonalloc& alc, jsonval& ss, const std::string conName, const std::map<TK, T>& vec, bool(*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{ {
jsonval jsvec(rapidjson::kArrayType); jsonval jsvec(rapidjson::kArrayType);
for (typename std::map<TK, T>::const_iterator it = vec.begin(); it != vec.end(); ++it) { for (typename std::map<TK, T>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
...@@ -650,8 +664,7 @@ inline void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, co ...@@ -650,8 +664,7 @@ inline void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, co
ss.AddMember(jname, jsvec, alc); ss.AddMember(jname, jsvec, alc);
} }
template <typename T> template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std::vector<T>& vec)
void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std::vector<T>& vec)
{ {
jsonval jsvec(rapidjson::kArrayType); jsonval jsvec(rapidjson::kArrayType);
for (typename std::vector<T>::const_iterator it = vec.begin(); it != vec.end(); ++it) { for (typename std::vector<T>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
...@@ -664,8 +677,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std: ...@@ -664,8 +677,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std:
ss.AddMember(jname, jsvec, alc); ss.AddMember(jname, jsvec, alc);
} }
template <typename T> template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const T& object)
void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const T& object)
{ {
jsonval obj(rapidjson::kObjectType); jsonval obj(rapidjson::kObjectType);
PutValue(alc, obj, object); PutValue(alc, obj, object);
...@@ -890,3 +902,168 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<int>& ...@@ -890,3 +902,168 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<int>&
return true; return true;
} }
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, double& value, const bool exist = true, bool* loaded = nullptr)
{
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const std::string& str = itr->second;
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
return true;
}
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, int& value, const bool exist = true, bool* loaded = nullptr)
{
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const std::string& str = itr->second;
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
return true;
}
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, long long& value, const bool exist = true, bool* loaded = nullptr)
{
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const std::string& str = itr->second;
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
return true;
}
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, bool& value, const bool exist = true, bool* loaded = nullptr)
{
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const std::string& str = itr->second;
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
return true;
}
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, std::string& value, const bool exist = true, bool* loaded = nullptr)
{
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
value = itr->second;
return true;
}
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, svalue<double>& value, const bool exist = true)
{
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const std::string& str = itr->second;
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
double val;
if (!Converter::ToNumber(str, val)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
value = val;
return true;
}
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, svalue<int>& value, const bool exist = true)
{
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const std::string& str = itr->second;
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
int val;
if (!Converter::ToNumber(str, val)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
value = val;
return true;
}
...@@ -112,3 +112,38 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJ ...@@ -112,3 +112,38 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJ
return true; return true;
} }
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, svalue<TimeJD>& value, const bool exist = true)
{
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
value = StrToDate(itr->second);
return true;
}
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, TimeJD& value, const bool exist = true, bool* loaded = nullptr)
{
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const std::string& str = itr->second;
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
value = StrToDate(str);
return true;
}
...@@ -81,4 +81,10 @@ public: ...@@ -81,4 +81,10 @@ public:
val = std::stoi(str, nullptr); val = std::stoi(str, nullptr);
return true; return true;
} }
static bool ToNumber(const std::string& str, long long& val)
{
val = std::stoll(str, nullptr);
return true;
}
}; };
...@@ -144,7 +144,7 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute, ...@@ -144,7 +144,7 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute,
std::ostringstream ss; std::ostringstream ss;
if(!year_first) { if (!year_first) {
ss << std::setfill('0'); ss << std::setfill('0');
ss << std::setw(2) << day << "/"; ss << std::setw(2) << day << "/";
ss << std::setw(2) << month << "/"; ss << std::setw(2) << month << "/";
...@@ -152,7 +152,8 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute, ...@@ -152,7 +152,8 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute,
ss << std::setw(2) << d << ":"; ss << std::setw(2) << d << ":";
ss << std::setw(2) << m << ":";; ss << std::setw(2) << m << ":";;
ss << std::setw(2) << s; ss << std::setw(2) << s;
}else { }
else {
ss << std::setfill('0'); ss << std::setfill('0');
ss << std::setw(4) << year << "/"; ss << std::setw(4) << year << "/";
ss << std::setw(2) << month << "/"; ss << std::setw(2) << month << "/";
...@@ -503,3 +504,29 @@ inline TimeJD get_current_time() ...@@ -503,3 +504,29 @@ inline TimeJD get_current_time()
return now; return now;
} }
inline long long get_ts()
{
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count() / 1000;
}
inline long long get_ts(const TimeJD& date)
{
const TimeGD gd = date;
int jd;
double jf;
convertTimeGJ(
gd.Year,
gd.Month,
gd.Day,
gd.Hour,
gd.Min,
gd.Sec,
gd.Fraction,
jd,
jf);
return (jd + jf - 2440587.5) * 86400.0 * 1000;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment