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;