refs #116 added CVariant, a wrapper for QVariant with more natural/transparent DBus integration

This commit is contained in:
Mathew Sutcliffe
2014-05-16 19:43:47 +01:00
parent b03a9b11a4
commit 2726a7a09a
3 changed files with 218 additions and 1 deletions

54
src/blackmisc/variant.cpp Normal file
View File

@@ -0,0 +1,54 @@
/* Copyright (C) 2014 VATSIM Community / authors
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "variant.h"
#include "blackmiscfreefunctions.h"
#include <QDBusArgument>
#include <QDBusMetaType>
#include <QDBusVariant>
namespace BlackMisc
{
void CVariant::registerMetadata()
{
qRegisterMetaType<CVariant>();
qDBusRegisterMetaType<CVariant>();
}
QString CVariant::toString(bool i18n) const
{
if (type() == QVariant::UserType)
{
const CValueObject *s = CValueObject::fromQVariant(m_v); // FIXME this will return garbage if value is not a CValueObject
if (s)
{
return s->toQString(i18n);
}
}
return m_v.toString();
}
QDBusArgument &operator <<(QDBusArgument &arg, const CVariant &var)
{
arg.beginStructure();
arg << QString(var.typeName()) << QDBusVariant(var.toQVariant());
arg.endStructure();
return arg;
}
const QDBusArgument &operator >>(const QDBusArgument &arg, CVariant &var)
{
QString typeName;
QDBusVariant dbusVar;
arg.beginStructure();
arg >> typeName >> dbusVar;
arg.endStructure();
var = fixQVariantFromDbusArgument(dbusVar.variant(), QMetaType::type(qPrintable(typeName)));
return arg;
}
} // namespace