From d6bf89b6e4359360a571abfd75dd3363ffab8ea7 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 14 Sep 2017 14:32:21 +0200 Subject: [PATCH] feat(Variant): impl setValue --- src/variant/variant.cpp | 34 ++++++++++++++++++++++++---------- src/variant/variant.h | 13 ++++++++++--- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/variant/variant.cpp b/src/variant/variant.cpp index 33c89a4e3..a37c6f3bf 100644 --- a/src/variant/variant.cpp +++ b/src/variant/variant.cpp @@ -51,11 +51,18 @@ public: }; Variant::Variant () { - // Nothing. Construct an invalid invariant. + if (!mPrivate) + mPrivate = new VariantPrivate(); } -Variant::Variant (Type type) { - // TODO. +Variant::Variant (Type type) : Variant() { + if (type != String) + return; + + L_D(Variant); + + if (!d->value.str) + d->value.str = new string(); } Variant::Variant (const Variant &src) { @@ -126,12 +133,17 @@ Variant::Variant (float value) : Variant(Float) { d->value.f = value; } -Variant::Variant (const std::string &value) { - // TODO. +Variant::Variant (const std::string &value) : Variant(String) { + L_D(Variant); + *d->value.str = value; } Variant::~Variant () { - // TODO. + L_D(Variant); + if (d->type == String) + delete d->value.str; + + delete mPrivate; } bool Variant::operator!= (const Variant &variant) const { @@ -175,8 +187,8 @@ bool Variant::operator>= (const Variant &variant) const { } bool Variant::isValid () const { - // TODO. - return false; + L_D(const Variant); + return d->type != Invalid; } void Variant::clear () { @@ -349,11 +361,13 @@ static inline void *getValueAsGeneric (const VariantPrivate &p, bool *soFarSoGoo // ----------------------------------------------------------------------------- void Variant::getValue (int type, void *value, bool *soFarSoGood) const { + L_ASSERT(type > Invalid && type != MaxDefaultTypes); + L_D(const Variant); - if (type <= Invalid || type >= MaxDefaultTypes) { + if (type > MaxDefaultTypes) { *soFarSoGood = false; - // Unable to get value. It will be great to support custom types. + // TODO: Unable to get value. It will be great to support custom types. return; } diff --git a/src/variant/variant.h b/src/variant/variant.h index 4b1ffe529..3eeb9090b 100644 --- a/src/variant/variant.h +++ b/src/variant/variant.h @@ -80,9 +80,9 @@ public: Variant (float value); Variant (const std::string &value); - template::value> > // void* constructor. Must be explicitly called. - Variant (T value) {} + template::value> > + Variant (T value) : Variant (Variant::createGeneric(value)) {} ~Variant (); @@ -97,7 +97,8 @@ public: template void setValue (const T &value) { - // TODO. + // Yeah, I'm crazy. + new (this) Variant(value); } template @@ -128,6 +129,12 @@ private: void getValue (int type, void *value, bool *soFarSoGood) const; + static inline Variant createGeneric (void *value) { + Variant variant(Generic); + variant.setValue(value); + return variant; + } + VariantPrivate *mPrivate = nullptr; L_DECLARE_PRIVATE(Variant);