From f4f31dc4227582232985eb6922a90c42598136e0 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 14 Sep 2017 09:28:41 +0200 Subject: [PATCH] feat(variant): add getValueAsDouble impl --- include/linphone/utils/utils.h | 4 ++-- src/utils/utils.cpp | 20 +++++++++++++----- src/variant/variant.cpp | 38 ++++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/include/linphone/utils/utils.h b/include/linphone/utils/utils.h index ccf43a9de..ddb9eb40a 100644 --- a/include/linphone/utils/utils.h +++ b/include/linphone/utils/utils.h @@ -48,11 +48,11 @@ namespace Utils { LINPHONE_PUBLIC std::string toString (long double val); LINPHONE_PUBLIC int stoi (const std::string &str, size_t *idx = 0, int base = 10); + LINPHONE_PUBLIC double stod (const std::string &str, size_t *idx = 0); + LINPHONE_PUBLIC bool stob (const std::string &str); LINPHONE_PUBLIC std::string stringToLower (const std::string &str); - LINPHONE_PUBLIC bool stringToBool (const std::string &str); - // Return a buffer allocated with new. LINPHONE_PUBLIC char *utf8ToChar (uint32_t ic); diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index d3ad01afc..9e4033f4d 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -88,17 +88,27 @@ int Utils::stoi (const string &str, size_t *idx, int base) { return v; } +double Utils::stod (const std::string &str, size_t *idx) { + char *p; + double v = strtod(str.c_str(), &p); + + if (idx) + *idx = p - str.c_str(); + + return v; +} + +bool Utils::stob (const string &str) { + const string lowerStr = stringToLower(str); + return !lowerStr.empty() && (lowerStr == "true" || lowerStr == "1"); +} + string Utils::stringToLower (const string &str) { string result(str.size(), ' '); transform(str.cbegin(), str.cend(), result.begin(), ::tolower); return result; } -bool Utils::stringToBool (const string &str) { - const string lowerStr = stringToLower(str); - return !lowerStr.empty() && (lowerStr == "true" || lowerStr == "1"); -} - char *Utils::utf8ToChar (uint32_t ic) { char *result = new char[5]; int size = 0; diff --git a/src/variant/variant.cpp b/src/variant/variant.cpp index 8edfdcfb9..33c89a4e3 100644 --- a/src/variant/variant.cpp +++ b/src/variant/variant.cpp @@ -279,7 +279,7 @@ static inline bool getValueAsBool (const VariantPrivate &p, bool *soFarSoGood) { return p.value.b; case Variant::String: - return Utils::stringToBool(*p.value.str); + return Utils::stob(*p.value.str); case Variant::Generic: return static_cast(p.value.g); @@ -293,7 +293,41 @@ static inline bool getValueAsBool (const VariantPrivate &p, bool *soFarSoGood) { } static inline double getValueAsDouble (const VariantPrivate &p, bool *soFarSoGood) { - // TODO. + L_ASSERT(p.type > Variant::Invalid && p.type < Variant::MaxDefaultTypes); + + *soFarSoGood = true; + switch (static_cast(p.type)) { + case Variant::Int: + case Variant::Short: + case Variant::Long: + case Variant::LongLong: + case Variant::Char: + case Variant::Float: + return static_cast(getAssumedNumber(p)); + + case Variant::UnsignedInt: + case Variant::UnsignedShort: + case Variant::UnsignedLong: + case Variant::UnsignedLongLong: + return static_cast(getAssumedUnsignedNumber(p)); + + case Variant::Double: + return p.value.d; + + case Variant::Bool: + return static_cast(p.value.b); + + case Variant::String: + return Utils::stod(*p.value.str); + + case Variant::Generic: + return static_cast(!!p.value.g); + + default: + *soFarSoGood = false; + break; + } + return 0.0; }