Created
December 2, 2015 11:46
-
-
Save milabs/5c063c0fc6a8df814fb2 to your computer and use it in GitHub Desktop.
TRObject - QVariant-based property tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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