Skip to content

Instantly share code, notes, and snippets.

@milabs
Created December 2, 2015 11:46
Show Gist options
  • Save milabs/5c063c0fc6a8df814fb2 to your computer and use it in GitHub Desktop.
Save milabs/5c063c0fc6a8df814fb2 to your computer and use it in GitHub Desktop.
TRObject - QVariant-based property tree
#include "TRObject.h"
TRObject& TRObject::operator[](const QString& key)
{
if (type() == QVariant::Map)
return keyValue<QVariantMap>(this, key);
else if (type() == QVariant::Hash)
return keyValue<QVariantHash>(this, key);
setValue(QVariantMap());
return keyValue<QVariantMap>(this, key);
}
TRObject& TRObject::operator[](const QString& key) const
{
return const_cast<TRObject*>(this)->operator[](key);
}
TRObject& TRObject::operator=(const QVariant& value)
{
setValue(value);
return *this;
}
class TRObject : public QVariant
{
public:
TRObject() : QVariant() {}
TRObject(const TRObject& o) : QVariant(o) {}
TRObject(const QVariant& v) : QVariant(v) {}
TRObject& operator[](const QString& key);
TRObject& operator[](const QString& key) const;
TRObject& operator=(const QVariant& value);
private:
template<typename T>
TRObject& keyValue(TRObject *ptr, const QString& key) {
T* p = (T*)ptr->data();
if (!p->contains(key)) p->insert(key, QVariant());
return *reinterpret_cast<TRObject *>(&p->operator[](key));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment