Commit 6a8faff0 authored by Alexander Lapshin's avatar Alexander Lapshin

+ svalue

parent 73eac811
......@@ -10,6 +10,7 @@
#include "rapidjson/document.h"
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/prettywriter.h>
#include "svalue.h"
#define RAPIDJSON_HAS_STDSTRING 1
......@@ -810,3 +811,39 @@ static void loadStrJson(const std::string& data, rapidjson::Document& document)
throw Exp() << "json parse error " << ", code: " << ok.Code() << ", offset: " << ok.Offset() << " at " << __FILE__ << ":" << __LINE__;
}
}
static bool loadjson(const jsonval& parent, const std::string& key, svalue<double>& value, const bool exist = true)
{
jsonval::ConstMemberIterator itr;
bool* loaded = nullptr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
if (a.IsDouble()) {
value = a.GetDouble();
}else if (a.IsString()) {
std::string str = a.GetString();
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;
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
return true;
}
#include "svalue.h"
#pragma once
template <typename T> class svalue
{
public:
svalue()
{
}
svalue(const T& v)
:
m_has_data(true),
m_value(v)
{
}
friend bool operator<=(double l, const svalue& r) = delete;
friend bool operator>=(double l, const svalue& r) = delete;
friend bool operator==(double l, const svalue& r) = delete;
friend bool operator!=(double l, const svalue& r) = delete;
friend bool operator<=(float l, const svalue& r) = delete;
friend bool operator>=(float l, const svalue& r) = delete;
friend bool operator==(float l, const svalue& r) = delete;
friend bool operator!=(float l, const svalue& r) = delete;
friend bool operator<=(const svalue& l, double r) = delete;
friend bool operator>=(const svalue& l, double r) = delete;
friend bool operator==(const svalue& l, double r) = delete;
friend bool operator!=(const svalue& l, double r) = delete;
friend bool operator<=(const svalue& l, float r) = delete;
friend bool operator>=(const svalue& l, float r) = delete;
friend bool operator==(const svalue& l, float r) = delete;
friend bool operator!=(const svalue& l, float r) = delete;
friend bool operator!=(const svalue& l, const svalue& r) { return l.get() != r.get(); }
friend bool operator==(const svalue& l, const svalue& r) { return l.get() == r.get(); }
friend bool operator<(const svalue& l, const svalue& r) { return l.get() < r.get(); }
friend bool operator<=(const svalue& l, const svalue& r) { return l.get() <= r.get(); }
friend bool operator>(const svalue& l, const svalue& r) { return l.get() > r.get(); }
friend bool operator>=(const svalue& l, const svalue& r) { return l.get() >= r.get(); }
friend bool operator*(const svalue& l, const svalue& r) { return l.get() * r.get(); }
friend bool operator/(const svalue& l, const svalue& r) { return l.get() / r.get(); }
friend bool operator+(const svalue& l, const svalue& r) { return l.get() + r.get(); }
friend bool operator-(const svalue& l, const svalue& r) { return l.get() - r.get(); }
template <typename Tc> friend bool operator==(const Tc& l, const svalue& r) { return l == r.get(); }
template <typename Tc> friend bool operator!=(const Tc& l, const svalue& r) { return l != r.get(); }
template <typename Tc> friend bool operator<(const Tc& l, const svalue& r) { return l < r.get(); }
template <typename Tc> friend bool operator>(const Tc& l, const svalue& r) { return l > r.get(); }
template <typename Tc> friend bool operator<=(const Tc& l, const svalue& r) { return l <= r.get(); }
template <typename Tc> friend bool operator>=(const Tc& l, const svalue& r) { return l >= r.get(); }
template <typename Tc> friend bool operator*(const Tc& l, const svalue& r) { return l * r.get(); }
template <typename Tc> friend bool operator/(const Tc& l, const svalue& r) { return l / r.get(); }
template <typename Tc> friend bool operator+(const Tc& l, const svalue& r) { return l + r.get(); }
template <typename Tc> friend bool operator-(const Tc& l, const svalue& r) { return l - r.get(); }
template <typename Tc> friend bool operator==(const svalue& l, const Tc& r) { return l.get() == r; }
//template <typename Tc> friend bool operator!=(const svalue& l, const Tc& r) { return l.get() != r; }
template <typename Tc> friend bool operator<(const svalue& l, const Tc& r) { return l.get() < r; }
template <typename Tc> friend bool operator>(const svalue& l, const Tc& r) { return l.get() > r; }
template <typename Tc> friend bool operator<=(const svalue& l, const Tc& r) { return l.get() <= r; }
template <typename Tc> friend bool operator>=(const svalue& l, const Tc& r) { return l.get() >= r; }
template <typename Tc> friend bool operator*(const svalue& l, const Tc& r) { return l.get() * r; }
template <typename Tc> friend bool operator/(const svalue& l, const Tc& r) { return l.get() / r; }
template <typename Tc> friend bool operator+(const svalue& l, const Tc& r) { return l.get() + r; }
template <typename Tc> friend bool operator-(const svalue& l, const Tc& r) { return l.get() - r; }
bool has_data() const { return m_has_data; }
const T& get() const { return m_value; }
void set(const T& v)
{
m_value = v;
m_has_data = true;
}
void unset()
{
m_has_data = false;
}
private:
bool m_has_data{false};
T m_value;
};
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