From acce40efc3bf423a6bfb0aab611499bd4866bb4f Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Fri, 15 Sep 2017 14:36:08 +0200 Subject: [PATCH] feat(Variant): add getValueAsFloat impl --- include/linphone/utils/utils.h | 1 + src/utils/utils.cpp | 14 +++++++++++-- src/variant/variant.cpp | 38 +++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/include/linphone/utils/utils.h b/include/linphone/utils/utils.h index 4321e37eb..bbdeccc68 100644 --- a/include/linphone/utils/utils.h +++ b/include/linphone/utils/utils.h @@ -50,6 +50,7 @@ namespace Utils { 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 float stof (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); diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index e67796e34..f2369576e 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -94,12 +94,22 @@ int Utils::stoi (const string &str, size_t *idx, int base) { return v; } -double Utils::stod (const std::string &str, size_t *idx) { +double Utils::stod (const string &str, size_t *idx) { char *p; double v = strtod(str.c_str(), &p); if (idx) - *idx = p - str.c_str(); + *idx = p - str.c_str(); + + return v; +} + +float Utils::stof (const string &str, size_t *idx) { + char *p; + float v = strtof(str.c_str(), &p); + + if (idx) + *idx = p - str.c_str(); return v; } diff --git a/src/variant/variant.cpp b/src/variant/variant.cpp index edaa139f7..eb538cedf 100644 --- a/src/variant/variant.cpp +++ b/src/variant/variant.cpp @@ -407,7 +407,43 @@ static inline double getValueAsDouble (const VariantPrivate &p, bool *soFarSoGoo } static inline float getValueAsFloat (const VariantPrivate &p, bool *soFarSoGood) { - // TODO. + const int type = p.getType(); + L_ASSERT(type > Variant::Invalid && type < Variant::MaxDefaultTypes); + + switch (static_cast(type)) { + case Variant::Int: + case Variant::Short: + case Variant::Long: + case Variant::LongLong: + case Variant::Char: + return static_cast(getAssumedNumber(p)); + + case Variant::UnsignedInt: + case Variant::UnsignedShort: + case Variant::UnsignedLong: + case Variant::UnsignedLongLong: + return static_cast(getAssumedUnsignedNumber(p)); + + case Variant::Float: + return p.value.f; + + case Variant::Double: + return static_cast(p.value.d); + + case Variant::Bool: + return static_cast(p.value.b); + + case Variant::String: + return Utils::stof(*p.value.str); + + case Variant::Generic: + return static_cast(!!p.value.g); + + default: + *soFarSoGood = false; + break; + } + return 0.f; }