From a7b9d99f6276150270b3538ba1602cee287f5ac2 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 13 Sep 2017 16:57:31 +0200 Subject: [PATCH] feat(variant): add impl (in progress) on getValue --- src/chat/chat-message-p.h | 2 - src/chat/chat-message.cpp | 6 +- src/chat/chat-message.h | 2 - src/chat/cpim/parser/cpim-parser.cpp | 2 - src/db/provider/db-session-provider.cpp | 2 - src/variant/variant.cpp | 145 ++++++++++++++++++++++-- src/variant/variant.h | 33 ++++-- 7 files changed, 157 insertions(+), 35 deletions(-) diff --git a/src/chat/chat-message-p.h b/src/chat/chat-message-p.h index 991a23aff..87198de32 100644 --- a/src/chat/chat-message-p.h +++ b/src/chat/chat-message-p.h @@ -19,8 +19,6 @@ #ifndef _CHAT_MESSAGE_P_H_ #define _CHAT_MESSAGE_P_H_ -#include - #include "chat-message.h" #include "db/events-db.h" #include "object/object-p.h" diff --git a/src/chat/chat-message.cpp b/src/chat/chat-message.cpp index 10e30db06..ee56033da 100644 --- a/src/chat/chat-message.cpp +++ b/src/chat/chat-message.cpp @@ -16,8 +16,6 @@ * along with this program. If not, see . */ -#include - #include "db/events-db.h" #include "object/object-p.h" @@ -88,7 +86,7 @@ shared_ptr ChatMessage::getErrorInfo () const { void ChatMessage::send () { L_D(ChatMessage); - + if (d->contents.size() > 1) { MultipartChatMessageModifier mcmm; mcmm.encode(d); @@ -159,7 +157,7 @@ void ChatMessage::addContent (const shared_ptr &content) { void ChatMessage::removeContent (const shared_ptr &content) { L_D(ChatMessage); if (d->isReadOnly) return; - + d->contents.remove(const_pointer_cast(content)); } diff --git a/src/chat/chat-message.h b/src/chat/chat-message.h index 9769c2ef7..b8b026c8e 100644 --- a/src/chat/chat-message.h +++ b/src/chat/chat-message.h @@ -67,8 +67,6 @@ public: std::shared_ptr getErrorInfo () const; - std::string getContentType () const; - void send (); bool containsReadableText () const; diff --git a/src/chat/cpim/parser/cpim-parser.cpp b/src/chat/cpim/parser/cpim-parser.cpp index 0a6ee995e..15f6a1c6e 100644 --- a/src/chat/cpim/parser/cpim-parser.cpp +++ b/src/chat/cpim/parser/cpim-parser.cpp @@ -16,8 +16,6 @@ * along with this program. If not, see . */ -#include - #include #include diff --git a/src/db/provider/db-session-provider.cpp b/src/db/provider/db-session-provider.cpp index 35dc9c82c..34edc9a5f 100644 --- a/src/db/provider/db-session-provider.cpp +++ b/src/db/provider/db-session-provider.cpp @@ -16,8 +16,6 @@ * along with this program. If not, see . */ -#include - #ifdef SOCI_ENABLED #include #endif // ifdef SOCI_ENABLED diff --git a/src/variant/variant.cpp b/src/variant/variant.cpp index e36d6cd1d..fa18701ed 100644 --- a/src/variant/variant.cpp +++ b/src/variant/variant.cpp @@ -20,6 +20,8 @@ // ============================================================================= +using namespace std; + LINPHONE_BEGIN_NAMESPACE class VariantPrivate { @@ -27,10 +29,13 @@ public: union Value { int i; unsigned int ui; + short s; + unsigned short us; long l; unsigned long ul; long long ll; unsigned long long ull; + char c; bool b; double d; float f; @@ -61,42 +66,57 @@ Variant::Variant (int value) : Variant(Int) { d->value.i = value; } -Variant::Variant (unsigned int value) : Variant(Int) { +Variant::Variant (unsigned int value) : Variant(UnsignedInt) { L_D(Variant); d->value.ui = value; } -Variant::Variant (long value) : Variant(Int) { +Variant::Variant (short value) : Variant(Short) { + L_D(Variant); + d->value.s = value; +} + +Variant::Variant (unsigned short value) : Variant(UnsignedShort) { + L_D(Variant); + d->value.us = value; +} + +Variant::Variant (long value) : Variant(Long) { L_D(Variant); d->value.l = value; } -Variant::Variant (unsigned long value) : Variant(Int) { +Variant::Variant (unsigned long value) : Variant(UnsignedLong) { L_D(Variant); d->value.ul = value; } -Variant::Variant (long long value) : Variant(Int) { +Variant::Variant (long long value) : Variant(LongLong) { L_D(Variant); d->value.ll = value; } -Variant::Variant (unsigned long long value) : Variant(Int) { +Variant::Variant (unsigned long long value) : Variant(UnsignedLongLong) { L_D(Variant); d->value.ull = value; } -Variant::Variant (bool value) : Variant(Int) { +Variant::Variant (char value) : Variant(Char) { + L_D(Variant); + d->value.c = value; +} + +Variant::Variant (bool value) : Variant(Bool) { L_D(Variant); d->value.b = value; } -Variant::Variant (double value) : Variant(Int) { +Variant::Variant (double value) : Variant(Double) { L_D(Variant); d->value.d = value; } -Variant::Variant (float value) : Variant(Int) { +Variant::Variant (float value) : Variant(Float) { L_D(Variant); d->value.f = value; } @@ -162,13 +182,114 @@ void Variant::swap (const Variant &variant) { // TODO. } +// ----------------------------------------------------------------------------- +// Number conversions. // ----------------------------------------------------------------------------- -void Variant::getValue (int type, void *value, bool *soFarSoGood) { - if (type <= 0 || type >= MaxDefaultTypes) - return; // Unable to get value. - +static inline long long getValueAsNumber (const VariantPrivate &p, bool *soFarSoGood) { // TODO. + return 0; +} + +static inline unsigned long long getValueAsUnsignedNumber (const VariantPrivate &p, bool *soFarSoGood) { + // TODO. + return 0; +} + +// ----------------------------------------------------------------------------- +// Specific conversions. +// ----------------------------------------------------------------------------- + +static inline bool getValueAsBool (const VariantPrivate &p, bool *soFarSoGood) { + // TODO. + return false; +} + +static inline double getValueAsDouble (const VariantPrivate &p, bool *soFarSoGood) { + // TODO. + return 0.0; +} + +static inline float getValueAsFloat (const VariantPrivate &p, bool *soFarSoGood) { + // TODO. + return 0.f; +} + +static inline float getValueAsString (const VariantPrivate &p, bool *soFarSoGood) { + // TODO. + return 0.f; +} + +static inline void *getValueAsGeneric (const VariantPrivate &p, bool *soFarSoGood) { + // TODO. + return nullptr; +} + +// ----------------------------------------------------------------------------- + +void Variant::getValue (int type, void *value, bool *soFarSoGood) const { + L_D(const Variant); + + if (type <= 0 || type >= MaxDefaultTypes) { + *soFarSoGood = false; + // Unable to get value. It will be great to support custom types. + return; + } + + switch (static_cast(type)) { + // Cast as Number. + case Int: + *static_cast(value) = static_cast(getValueAsNumber(*d, soFarSoGood)); + break; + case Short: + *static_cast(value) = static_cast(getValueAsNumber(*d, soFarSoGood)); + break; + case Long: + *static_cast(value) = static_cast(getValueAsNumber(*d, soFarSoGood)); + break; + case LongLong: + *static_cast(value) = getValueAsNumber(*d, soFarSoGood); + break; + case Char: + *static_cast(value) = static_cast(getValueAsNumber(*d, soFarSoGood)); + break; + + // Cast as Unsigned number. + case UnsignedInt: + *static_cast(value) = static_cast(getValueAsNumber(*d, soFarSoGood)); + break; + case UnsignedShort: + *static_cast(value) = static_cast(getValueAsNumber(*d, soFarSoGood)); + break; + case UnsignedLong: + *static_cast(value) = static_cast(getValueAsNumber(*d, soFarSoGood)); + break; + case UnsignedLongLong: + *static_cast(value) = getValueAsNumber(*d, soFarSoGood); + break; + + // Cast as specific value. + case Bool: + *static_cast(value) = getValueAsBool(*d, soFarSoGood); + break; + case Double: + *static_cast(value) = getValueAsDouble(*d, soFarSoGood); + break; + case Float: + *static_cast(value) = getValueAsFloat(*d, soFarSoGood); + break; + case String: + *static_cast(value) = getValueAsString(*d, soFarSoGood); + break; + case Generic: + *static_cast(value) = getValueAsGeneric(*d, soFarSoGood); + break; + + case Invalid: + case MaxDefaultTypes: + *soFarSoGood = false; + break; + } } LINPHONE_END_NAMESPACE diff --git a/src/variant/variant.h b/src/variant/variant.h index fccf35d95..2966f79d1 100644 --- a/src/variant/variant.h +++ b/src/variant/variant.h @@ -30,15 +30,18 @@ LINPHONE_BEGIN_NAMESPACE #define L_DECLARE_VARIANT_TYPES(FUNC) \ FUNC(int, Int, 1) \ FUNC(unsigned int, UnsignedInt, 2) \ - FUNC(long, Long, 3) \ - FUNC(unsigned long, UnsignedLong, 4) \ - FUNC(long long, LongLong, 5) \ - FUNC(unsigned long long, UnsignedLongLong, 6) \ - FUNC(bool, Bool, 7) \ - FUNC(double, Double, 8) \ - FUNC(float, Float, 9) \ - FUNC(std::string, String, 10) \ - FUNC(void *, Generic, 11) + FUNC(short, Short, 3) \ + FUNC(unsigned short, UnsignedShort, 4) \ + FUNC(long, Long, 5) \ + FUNC(unsigned long, UnsignedLong, 6) \ + FUNC(long long, LongLong, 7) \ + FUNC(unsigned long long, UnsignedLongLong, 8) \ + FUNC(char, Char, 9) \ + FUNC(bool, Bool, 10) \ + FUNC(double, Double, 11) \ + FUNC(float, Float, 12) \ + FUNC(std::string, String, 13) \ + FUNC(void *, Generic, 14) #define L_DECLARE_VARIANT_ENUM_TYPE(TYPE, NAME, ID) NAME = ID, #define L_DECLARE_VARIANT_TRAIT_TYPE(TYPE, NAME, ID) \ @@ -65,10 +68,13 @@ public: Variant (int value); Variant (unsigned int value); + Variant (short value); + Variant (unsigned short value); Variant (long value); Variant (unsigned long value); Variant (long long value); Variant (unsigned long long value); + Variant (char value); Variant (bool value); Variant (double value); Variant (float value); @@ -100,7 +106,12 @@ public: static_assert(id != Invalid, "Unable to get value of unsupported type."); T value; - getValue(id, &value, &soFarSoGood); + + bool ok; + getValue(id, static_cast(&value), &ok); + if (soFarSoGood) + *soFarSoGood = ok; + return value; } @@ -115,7 +126,7 @@ private: static const int id = Invalid; }; - void getValue (int type, void *value, bool *soFarSoGood); + void getValue (int type, void *value, bool *soFarSoGood) const; VariantPrivate *mPrivate = nullptr;