Commit acce40ef authored by Ronan's avatar Ronan
Browse files

feat(Variant): add getValueAsFloat impl

parent d619eba2
......@@ -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);
......@@ -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;
......@@ -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<Variant::Type>(type)) {
case Variant::Int:
case Variant::Short:
case Variant::Long:
case Variant::LongLong:
case Variant::Char:
return static_cast<double>(getAssumedNumber(p));
case Variant::UnsignedInt:
case Variant::UnsignedShort:
case Variant::UnsignedLong:
case Variant::UnsignedLongLong:
return static_cast<double>(getAssumedUnsignedNumber(p));
case Variant::Float:
return p.value.f;
case Variant::Double:
return static_cast<float>(p.value.d);
case Variant::Bool:
return static_cast<double>(p.value.b);
case Variant::String:
return Utils::stof(*p.value.str);
case Variant::Generic:
return static_cast<double>(!!p.value.g);
*soFarSoGood = false;
return 0.f;
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