From d619eba238e43bf2c5672dd2d2e6e259ef061600 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Fri, 15 Sep 2017 12:04:09 +0200 Subject: [PATCH] feat(Variant): add getValueAsString impl --- include/linphone/utils/utils.h | 1 + src/utils/utils.cpp | 6 +++ src/variant/variant.cpp | 60 +++++++++++++++++++++++----- tester/property-container-tester.cpp | 14 ++++++- 4 files changed, 69 insertions(+), 12 deletions(-) diff --git a/include/linphone/utils/utils.h b/include/linphone/utils/utils.h index ddb9eb40a..4321e37eb 100644 --- a/include/linphone/utils/utils.h +++ b/include/linphone/utils/utils.h @@ -46,6 +46,7 @@ namespace Utils { LINPHONE_PUBLIC std::string toString (float val); LINPHONE_PUBLIC std::string toString (double val); LINPHONE_PUBLIC std::string toString (long double val); + LINPHONE_PUBLIC std::string toString (const void *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); diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 9e4033f4d..e67796e34 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -78,6 +78,12 @@ TO_STRING_IMPL(float) TO_STRING_IMPL(double) TO_STRING_IMPL(long double) +string Utils::toString (const void *val) { + ostringstream ss; + ss << val; + return ss.str(); +} + int Utils::stoi (const string &str, size_t *idx, int base) { char *p; int v = strtol(str.c_str(), &p, base); diff --git a/src/variant/variant.cpp b/src/variant/variant.cpp index 952564293..edaa139f7 100644 --- a/src/variant/variant.cpp +++ b/src/variant/variant.cpp @@ -69,7 +69,7 @@ public: private: inline void deleteString () { if (type == Variant::String) - delete value.str; + delete value.str; } // Integer, because type can be a custom type. @@ -250,7 +250,8 @@ bool Variant::isValid () const { } void Variant::clear () { - // TODO. + L_D(Variant); + d->setType(Invalid); } void Variant::swap (const Variant &variant) { @@ -374,7 +375,6 @@ static inline double getValueAsDouble (const VariantPrivate &p, bool *soFarSoGoo case Variant::Long: case Variant::LongLong: case Variant::Char: - case Variant::Float: return static_cast(getAssumedNumber(p)); case Variant::UnsignedInt: @@ -383,6 +383,9 @@ static inline double getValueAsDouble (const VariantPrivate &p, bool *soFarSoGoo case Variant::UnsignedLongLong: return static_cast(getAssumedUnsignedNumber(p)); + case Variant::Float: + return static_cast(p.value.f); + case Variant::Double: return p.value.d; @@ -408,9 +411,46 @@ static inline float getValueAsFloat (const VariantPrivate &p, bool *soFarSoGood) return 0.f; } -static inline float getValueAsString (const VariantPrivate &p, bool *soFarSoGood) { - // TODO. - return 0.f; +static inline string getValueAsString (const VariantPrivate &p, bool *soFarSoGood) { + 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: + return Utils::toString(getAssumedNumber(p)); + + case Variant::UnsignedInt: + case Variant::UnsignedShort: + case Variant::UnsignedLong: + case Variant::UnsignedLongLong: + return Utils::toString(getAssumedUnsignedNumber(p)); + + case Variant::Char: + return string(1, p.value.c); + + case Variant::Bool: + return string(p.value.b ? "true" : "false"); + + case Variant::Double: + return Utils::toString(p.value.d); + + case Variant::Float: + return Utils::toString(p.value.f); + + case Variant::String: + return *p.value.str; + + case Variant::Generic: + return Utils::toString(p.value.g); + + default: + *soFarSoGood = false; + } + + return string(); } static inline void *getValueAsGeneric (const VariantPrivate &p, bool *soFarSoGood) { @@ -456,16 +496,16 @@ void Variant::getValue (int type, void *value, bool *soFarSoGood) const { // Cast as Unsigned number. case UnsignedInt: - *static_cast(value) = static_cast(getValueAsNumber(*d, soFarSoGood)); + *static_cast(value) = static_cast(getValueAsUnsignedNumber(*d, soFarSoGood)); break; case UnsignedShort: - *static_cast(value) = static_cast(getValueAsNumber(*d, soFarSoGood)); + *static_cast(value) = static_cast(getValueAsUnsignedNumber(*d, soFarSoGood)); break; case UnsignedLong: - *static_cast(value) = static_cast(getValueAsNumber(*d, soFarSoGood)); + *static_cast(value) = static_cast(getValueAsUnsignedNumber(*d, soFarSoGood)); break; case UnsignedLongLong: - *static_cast(value) = getValueAsNumber(*d, soFarSoGood); + *static_cast(value) = getValueAsUnsignedNumber(*d, soFarSoGood); break; // Cast as specific value. diff --git a/tester/property-container-tester.cpp b/tester/property-container-tester.cpp index 6cc7fffb7..2decc9164 100644 --- a/tester/property-container-tester.cpp +++ b/tester/property-container-tester.cpp @@ -26,14 +26,23 @@ using namespace std; using namespace LinphonePrivate; -// ----------------------------------------------------------------------------- - static void set_int_property () { PropertyContainer properties; properties.setProperty("integer", 42); BC_ASSERT_EQUAL(properties.getProperty("integer").getValue(), 42, int, "%d"); } +static void set_string_property () { + PropertyContainer properties; + const string text = "Hey listen!"; + properties.setProperty("string", text); + + { + string textToCheck = properties.getProperty("string").getValue(); + BC_ASSERT_STRING_EQUAL(textToCheck.c_str(), text.c_str()); + } +} + static void set_generic_property () { PropertyContainer properties; properties.setProperty("generic", reinterpret_cast(0x42)); @@ -42,6 +51,7 @@ static void set_generic_property () { test_t property_container_tests[] = { TEST_NO_TAG("Set int property", set_int_property), + TEST_NO_TAG("Set string property", set_string_property), TEST_NO_TAG("Set generic property", set_generic_property) };