Commit d6bf89b6 authored by Ronan's avatar Ronan
Browse files

feat(Variant): impl setValue

parent bef0f90b
......@@ -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;
}
......
......@@ -80,9 +80,9 @@ public:
Variant (float value);
Variant (const std::string &value);
template<typename T, typename = typename std::enable_if<std::is_same<T, void *>::value> >
// void* constructor. Must be explicitly called.
Variant (T value) {}
template<typename T, typename = typename std::enable_if<std::is_same<T, void *>::value> >
Variant (T value) : Variant (Variant::createGeneric(value)) {}
~Variant ();
......@@ -97,7 +97,8 @@ public:
template<typename T>
void setValue (const T &value) {
// TODO.
// Yeah, I'm crazy.
new (this) Variant(value);
}
template<typename T>
......@@ -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<void *>(value);
return variant;
}
VariantPrivate *mPrivate = nullptr;
L_DECLARE_PRIVATE(Variant);
......
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