added to Space: long double to Space::Value and methods for converting from/to long double

added global methods for converting float/string double/string and long double/string (convert/double.h|cpp):
      float to_float(const char * str, const char ** after = nullptr);
      float to_float(const wchar_t * str, const wchar_t ** after = nullptr);
      double to_double(const char * str, const char ** after = nullptr);
      double to_double(const wchar_t * str, const wchar_t ** after = nullptr);
      long double to_long_double(const char * str, const char ** after = nullptr);
      long double to_long_double(const wchar_t * str, const wchar_t ** after = nullptr);
      float to_float(const std::string & str, const char ** after = nullptr);
      float to_float(const std::wstring & str, const wchar_t ** after = nullptr);
      double to_double(const std::string & str, const char ** after = nullptr);
      double to_double(const std::wstring & str, const wchar_t ** after = nullptr);
      long double to_long_double(const std::string & str, const char ** after = nullptr);
      long double to_long_double(const std::wstring & str, const wchar_t ** after = nullptr);
      std::string to_str(float val);
      std::wstring to_wstr(float val);
      std::string to_str(double val);
      std::wstring to_wstr(double val);
      std::string to_str(long double val);
      std::wstring to_wstr(long double val);
This commit is contained in:
2021-06-23 17:01:43 +02:00
parent c1f1dc96df
commit 3c0b59e115
9 changed files with 588 additions and 19 deletions

View File

@@ -152,6 +152,11 @@ Space::Space(double val)
set(val);
}
Space::Space(long double val)
{
initialize();
set(val);
}
Space::Space(const char * str)
{
@@ -355,6 +360,12 @@ void Space::set(double val)
value.value_double = val;
}
void Space::set(long double val)
{
initialize_value_long_double_if_needed();
value.value_long_double = val;
}
void Space::set(const char * str)
{
@@ -473,6 +484,11 @@ Space & Space::add(double val)
return add_generic(val);
}
Space & Space::add(long double val)
{
return add_generic(val);
}
Space & Space::add(const char * val)
{
return add_generic(val);
@@ -575,6 +591,11 @@ Space & Space::add(const wchar_t * field, double val)
return add_generic(field, val);
}
Space & Space::add(const wchar_t * field, long double val)
{
return add_generic(field, val);
}
Space & Space::add(const wchar_t * field, const char * val)
{
return add_generic(field, val);
@@ -678,6 +699,11 @@ Space & Space::add(const std::wstring & field, double val)
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, long double val)
{
return add_generic(field, val);
}
Space & Space::add(const std::wstring & field, const char * val)
{
return add_generic(field, val);
@@ -753,9 +779,14 @@ bool Space::is_double() const
return type == type_double;
}
bool Space::is_long_double() const
{
return type == type_long_double;
}
bool Space::is_numeric() const
{
return is_long_long() || is_float() || is_double();
return is_long_long() || is_float() || is_double() || is_long_double();
}
bool Space::is_str() const
@@ -835,6 +866,9 @@ long long Space::to_long_long() const
case type_double:
return static_cast<long long>(value.value_double);
case type_long_double:
return static_cast<long long>(value.value_long_double);
case type_string:
return convert_string_to_long_long();
@@ -931,6 +965,9 @@ unsigned long long Space::to_ulong_long() const
case type_double:
return static_cast<unsigned long long>(value.value_double);
case type_long_double:
return static_cast<unsigned long long>(value.value_long_double);
case type_string:
return convert_string_to_ulong_long();
@@ -943,6 +980,112 @@ unsigned long long Space::to_ulong_long() const
float Space::to_float() const
{
switch(type)
{
case type_null:
case type_object:
case type_table:
return 0;
case type_bool:
return value.value_bool ? 1.0f : 0.0f;
case type_long:
return static_cast<float>(value.value_long);
case type_float:
return value.value_float;
case type_double:
return static_cast<float>(value.value_double);
case type_long_double:
return static_cast<float>(value.value_long_double);
case type_string:
return pt::to_float(value.value_string);
case type_wstring:
return pt::to_float(value.value_wstring);
}
return 0;
}
double Space::to_double() const
{
switch(type)
{
case type_null:
case type_object:
case type_table:
return 0;
case type_bool:
return value.value_bool ? 1.0 : 0.0;
case type_long:
return static_cast<double>(value.value_long);
case type_float:
return static_cast<double>(value.value_float);
case type_double:
return value.value_double;
case type_long_double:
return static_cast<double>(value.value_long_double);
case type_string:
return pt::to_double(value.value_string);
case type_wstring:
return pt::to_double(value.value_wstring);
}
return 0;
}
long double Space::to_long_double() const
{
switch(type)
{
case type_null:
case type_object:
case type_table:
return 0;
case type_bool:
return value.value_bool ? 1.0 : 0.0;
case type_long:
return static_cast<long double>(value.value_long);
case type_float:
return static_cast<long double>(value.value_float);
case type_double:
return static_cast<long double>(value.value_double);
case type_long_double:
return value.value_long_double;
case type_string:
return pt::to_long_double(value.value_string);
case type_wstring:
return pt::to_long_double(value.value_wstring);
}
return 0;
}
std::string Space::to_str() const
{
if( type == type_string )
@@ -1384,6 +1527,11 @@ double * Space::get_double()
return type == type_double ? &value.value_double : nullptr;
}
long double * Space::get_long_double()
{
return type == type_long_double ? &value.value_long_double : nullptr;
}
std::string * Space::get_str()
{
return type == type_string ? &value.value_string : nullptr;
@@ -1513,7 +1661,7 @@ bool Space::has_value(const std::wstring & val) const
// RENAMEME to get_space(...)
Space * Space::get_object_field(const wchar_t * field)
{
if( is_object() )
@@ -1559,6 +1707,12 @@ double * Space::get_double(const wchar_t * field)
return space ? space->get_double() : nullptr;
}
long double * Space::get_long_double(const wchar_t * field)
{
Space * space = get_object_field(field);
return space ? space->get_long_double() : nullptr;
}
std::string * Space::get_str(const wchar_t * field)
{
Space * space = get_object_field(field);
@@ -1611,6 +1765,11 @@ const double * Space::get_double() const
return type == type_double ? &value.value_double : nullptr;
}
const long double * Space::get_long_double() const
{
return type == type_long_double ? &value.value_long_double : nullptr;
}
const std::string * Space::get_str() const
{
return type == type_string ? &value.value_string : nullptr;
@@ -1703,6 +1862,12 @@ const double * Space::get_double(const wchar_t * field) const
return space ? space->get_double() : nullptr;
}
const long double * Space::get_long_double(const wchar_t * field) const
{
const Space * space = get_object_field(field);
return space ? space->get_long_double() : nullptr;
}
const std::string * Space::get_str(const wchar_t * field) const
{
const Space * space = get_object_field(field);
@@ -2186,6 +2351,11 @@ void Space::copy_value_from(const Space & space)
value.value_double = space.value.value_double;
break;
case type_long_double:
initialize_value_long_double_if_needed();
value.value_long_double = space.value.value_long_double;
break;
case type_string:
initialize_value_string_if_needed();
value.value_string = space.value.value_string;
@@ -2339,6 +2509,17 @@ void Space::initialize_value_double_if_needed()
}
void Space::initialize_value_long_double_if_needed()
{
if( type != type_long_double )
{
remove_value();
new (&value) long double;
type = type_long_double;
}
}
void Space::initialize_value_string_if_needed()
{
if( type != type_string )
@@ -2471,6 +2652,7 @@ void Space::remove_value()
case type_long:
case type_float:
case type_double:
case type_long_double:
type = type_null;
break;

View File

@@ -153,6 +153,7 @@ public:
type_long,
type_float,
type_double,
type_long_double,
type_string,
type_wstring,
type_object,
@@ -165,6 +166,7 @@ public:
long long value_long;
float value_float;
double value_double;
long double value_long_double;
std::string value_string;
std::wstring value_wstring;
ObjectType value_object;
@@ -202,6 +204,7 @@ public:
Space(unsigned long long val);
Space(float val);
Space(double val);
Space(long double val);
Space(const char * str);
Space(const wchar_t * str);
Space(const std::string & str);
@@ -235,6 +238,7 @@ public:
void set(unsigned long long val);
void set(float val);
void set(double val);
void set(long double val);
void set(const char * str);
void set(const wchar_t * str);
void set(const std::string & str);
@@ -256,6 +260,7 @@ public:
Space & add(unsigned long long val);
Space & add(float val);
Space & add(double val);
Space & add(long double val);
Space & add(const char * val);
Space & add(const wchar_t * val);
Space & add(const std::string & val);
@@ -279,6 +284,7 @@ public:
Space & add(const wchar_t * field, unsigned long long val);
Space & add(const wchar_t * field, float val);
Space & add(const wchar_t * field, double val);
Space & add(const wchar_t * field, long double val);
Space & add(const wchar_t * field, const char * val);
Space & add(const wchar_t * field, const wchar_t * val);
Space & add(const wchar_t * field, const std::string & val);
@@ -299,6 +305,7 @@ public:
Space & add(const std::wstring & field, unsigned long long val);
Space & add(const std::wstring & field, float val);
Space & add(const std::wstring & field, double val);
Space & add(const std::wstring & field, long double val);
Space & add(const std::wstring & field, const char * val);
Space & add(const std::wstring & field, const wchar_t * val);
Space & add(const std::wstring & field, const std::string & val);
@@ -336,6 +343,7 @@ public:
bool is_long_long() const;
bool is_float() const;
bool is_double() const;
bool is_long_double() const;
bool is_numeric() const;
bool is_str() const;
bool is_wstr() const;
@@ -356,6 +364,9 @@ public:
unsigned long to_ulong() const;
unsigned long long to_ullong() const;
unsigned long long to_ulong_long() const;
float to_float() const;
double to_double() const;
long double to_long_double() const;
std::string to_str() const;
std::wstring to_wstr() const;
@@ -419,6 +430,7 @@ public:
long long * get_long_long();
float * get_float();
double * get_double();
long double * get_long_double();
std::string * get_str();
std::wstring * get_wstr();
ObjectType * get_object();
@@ -455,6 +467,7 @@ public:
long long * get_long_long(const wchar_t * field);
float * get_float(const wchar_t * field);
double * get_double(const wchar_t * field);
long double * get_long_double(const wchar_t * field);
std::string * get_str(const wchar_t * field);
std::wstring * get_wstr(const wchar_t * field);
ObjectType * get_object(const wchar_t * field);
@@ -467,6 +480,7 @@ public:
const long long * get_long_long() const;
const float * get_float() const;
const double * get_double() const;
const long double * get_long_double() const;
const std::string * get_str() const;
const std::wstring * get_wstr() const;
const ObjectType * get_object() const;
@@ -481,6 +495,7 @@ public:
const long long * get_long_long(const wchar_t * field) const;
const float * get_float(const wchar_t * field) const;
const double * get_double(const wchar_t * field) const;
const long double * get_long_double(const wchar_t * field) const;
const std::string * get_str(const wchar_t * field) const;
const std::wstring * get_wstr(const wchar_t * field) const;
const ObjectType * get_object(const wchar_t * field) const;
@@ -569,6 +584,10 @@ public:
serialize_json_double(str);
break;
case type_long_double:
serialize_json_long_double(str);
break;
case type_string:
serialize_json_string(str);
break;
@@ -970,7 +989,7 @@ protected:
wchar_t buffer[100];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
int chars_written = std::swprintf(buffer, buffer_len, L"%e", static_cast<double>(value.value_float));
int chars_written = std::swprintf(buffer, buffer_len, L"%g", static_cast<double>(value.value_float));
if( errno == EOVERFLOW || chars_written < 0 )
buffer[0] = 0;
@@ -984,7 +1003,21 @@ protected:
wchar_t buffer[100];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
int chars_written = std::swprintf(buffer, buffer_len, L"%e", value.value_double);
int chars_written = std::swprintf(buffer, buffer_len, L"%g", value.value_double);
if( errno == EOVERFLOW || chars_written < 0 )
buffer[0] = 0;
serialize_string_buffer(buffer, str, Escape::escape_space);
}
template<typename StreamType>
void serialize_space_long_double(StreamType & str) const
{
wchar_t buffer[100];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
int chars_written = std::swprintf(buffer, buffer_len, L"%Lg", value.value_long_double);
if( errno == EOVERFLOW || chars_written < 0 )
buffer[0] = 0;
@@ -1154,7 +1187,7 @@ protected:
wchar_t buffer[100];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
int chars_written = std::swprintf(buffer, buffer_len, L"%e", static_cast<double>(value.value_float));
int chars_written = std::swprintf(buffer, buffer_len, L"%g", static_cast<double>(value.value_float));
if( errno == EOVERFLOW || chars_written < 0 )
buffer[0] = 0;
@@ -1168,7 +1201,21 @@ protected:
wchar_t buffer[100];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
int chars_written = std::swprintf(buffer, buffer_len, L"%e", value.value_double);
int chars_written = std::swprintf(buffer, buffer_len, L"%g", value.value_double);
if( errno == EOVERFLOW || chars_written < 0 )
buffer[0] = 0;
serialize_string_buffer(buffer, str, Escape::escape_json);
}
template<typename StreamType>
void serialize_json_long_double(StreamType & str) const
{
wchar_t buffer[100];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
int chars_written = std::swprintf(buffer, buffer_len, L"%Lg", value.value_long_double);
if( errno == EOVERFLOW || chars_written < 0 )
buffer[0] = 0;
@@ -1266,6 +1313,10 @@ protected:
serialize_space_double(str);
break;
case type_long_double:
serialize_space_long_double(str);
break;
case type_string:
serialize_space_string(str);
break;
@@ -1350,6 +1401,7 @@ protected:
void initialize_value_long_if_needed();
void initialize_value_float_if_needed();
void initialize_value_double_if_needed();
void initialize_value_long_double_if_needed();
void initialize_value_string_if_needed();
void initialize_value_string_if_needed(std::string && str);
void initialize_value_wstring_if_needed();