forked from mirrors/linphone-iphone
feat(Variant): add getValueAsString impl
This commit is contained in:
parent
b3d8680150
commit
d619eba238
4 changed files with 69 additions and 12 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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<double>(getAssumedNumber(p));
|
||||
|
||||
case Variant::UnsignedInt:
|
||||
|
|
@ -383,6 +383,9 @@ static inline double getValueAsDouble (const VariantPrivate &p, bool *soFarSoGoo
|
|||
case Variant::UnsignedLongLong:
|
||||
return static_cast<double>(getAssumedUnsignedNumber(p));
|
||||
|
||||
case Variant::Float:
|
||||
return static_cast<double>(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<Variant::Type>(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<unsigned int *>(value) = static_cast<unsigned int>(getValueAsNumber(*d, soFarSoGood));
|
||||
*static_cast<unsigned int *>(value) = static_cast<unsigned int>(getValueAsUnsignedNumber(*d, soFarSoGood));
|
||||
break;
|
||||
case UnsignedShort:
|
||||
*static_cast<unsigned short *>(value) = static_cast<unsigned short>(getValueAsNumber(*d, soFarSoGood));
|
||||
*static_cast<unsigned short *>(value) = static_cast<unsigned short>(getValueAsUnsignedNumber(*d, soFarSoGood));
|
||||
break;
|
||||
case UnsignedLong:
|
||||
*static_cast<unsigned long *>(value) = static_cast<unsigned long>(getValueAsNumber(*d, soFarSoGood));
|
||||
*static_cast<unsigned long *>(value) = static_cast<unsigned long>(getValueAsUnsignedNumber(*d, soFarSoGood));
|
||||
break;
|
||||
case UnsignedLongLong:
|
||||
*static_cast<unsigned long long *>(value) = getValueAsNumber(*d, soFarSoGood);
|
||||
*static_cast<unsigned long long *>(value) = getValueAsUnsignedNumber(*d, soFarSoGood);
|
||||
break;
|
||||
|
||||
// Cast as specific value.
|
||||
|
|
|
|||
|
|
@ -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<int>(), 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<string>();
|
||||
BC_ASSERT_STRING_EQUAL(textToCheck.c_str(), text.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
static void set_generic_property () {
|
||||
PropertyContainer properties;
|
||||
properties.setProperty("generic", reinterpret_cast<void *>(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)
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue