refs #247 Using CVariant in samples and tests.

This commit is contained in:
Mathew Sutcliffe
2014-11-15 18:42:41 +00:00
parent 4007f34e22
commit 9b060014af
11 changed files with 68 additions and 53 deletions

View File

@@ -60,7 +60,7 @@ namespace BlackMiscTest
// put Jane in the tower
CPropertyIndexVariantMap newController;
newController.addValue(CAtcStation::IndexController, QVariant::fromValue(CUser("112233", "Jane Doe")));
newController.addValue(CAtcStation::IndexController, CVariant::fromValue(CUser("112233", "Jane Doe")));
atcList.applyIf(
BlackMisc::Predicates::MemberEqual(&CAtcStation::getCallsign, CCallsign("eddm_twr")),
newController);
@@ -69,7 +69,7 @@ namespace BlackMiscTest
// now Jane's time is over
CPropertyIndexVariantMap anotherController;
anotherController.addValue(CAtcStation::IndexController, QVariant::fromValue(CUser("445566", "Fuzzy")));
anotherController.addValue(CAtcStation::IndexController, CVariant::fromValue(CUser("445566", "Fuzzy")));
atcList.applyIf(newController, anotherController);
qDebug() << "-- after update via value map";

View File

@@ -71,7 +71,7 @@ namespace BlackMiscTest
atcList.push_back(station1);
CAtcStation station1Cpy(station1);
CFrequency changedFrequency(118.25, CFrequencyUnit::MHz());
CPropertyIndexVariantMap vm(CAtcStation::IndexFrequency, changedFrequency.toQVariant());
CPropertyIndexVariantMap vm(CAtcStation::IndexFrequency, changedFrequency.toCVariant());
// demonstration apply

View File

@@ -8,12 +8,14 @@
*/
#include "samplesvariant.h"
#include "blackmisc/variant.h"
#include "blackmisc/pqallquantities.h"
#include "blackmisc/avallclasses.h"
#include "blackmisc/blackmiscfreefunctions.h"
#include <QDebug>
#include <QMetaType>
using namespace BlackMisc;
using namespace BlackMisc::PhysicalQuantities;
using namespace BlackMisc::Aviation;
@@ -25,36 +27,38 @@ namespace BlackMiscTest
BlackMisc::registerMetadata();
CAngle a1(30.0, CAngleUnit::deg());
QVariant qva = a1.toQVariant();
qDebug() << a1 << qva.userType();
CVariant cva = a1.toCVariant();
qDebug() << a1 << cva.userType();
CHeading h1(45, CHeading::True, CAngleUnit::deg());
CHeading h2(60, CHeading::True, CAngleUnit::deg());
QVariant qvh = h1.toQVariant();
qDebug() << h1 << qvh.userType();
CVariant cvh = h1.toCVariant();
qDebug() << h1 << cvh.userType();
qDebug() << cva << cvh; // CVariant knows how to stringify the contained value object
CAngle *ap_heading = &h1; // angle actually heading
CAngle *ap_angle = &a1; // angle really heading
qDebug() << (*ap_heading) << ap_heading->toQVariant().userType();
qDebug() << (*ap_angle) << ap_angle->toQVariant().userType();
qDebug() << (*ap_heading) << ap_heading->toCVariant().userType();
qDebug() << (*ap_angle) << ap_angle->toCVariant().userType();
// This works, because ap is actually heading
ap_heading->convertFromQVariant(h2.toQVariant());
qDebug() << (*ap_heading) << ap_heading->toQVariant().userType();
ap_heading->convertFromCVariant(h2.toCVariant());
qDebug() << (*ap_heading) << ap_heading->toCVariant().userType();
// This works, angle from variant angle
ap_angle->convertFromQVariant(a1.toQVariant());
qDebug() << (*ap_angle) << ap_angle->toQVariant().userType();
ap_angle->convertFromCVariant(a1.toCVariant());
qDebug() << (*ap_angle) << ap_angle->toCVariant().userType();
// This gives me an unwanted(!) assert, canConvert is not smart enough to detect upcasting
// because CValueObjects are not QObjects
ap_angle->convertFromQVariant(h2.toQVariant());
qDebug() << (*ap_angle) << ap_angle->toQVariant().userType();
ap_angle->convertFromCVariant(h2.toCVariant());
qDebug() << (*ap_angle) << ap_angle->toCVariant().userType();
// This gives me the intended assert, because I assign angle to heading
ap_heading->convertFromQVariant(a1.toQVariant());
qDebug() << (*ap_heading) << ap_heading->toQVariant().userType();
ap_heading->convertFromCVariant(a1.toCVariant());
qDebug() << (*ap_heading) << ap_heading->toCVariant().userType();
return 0;
}

View File

@@ -378,21 +378,21 @@ namespace BlackMiscTest
cv = testserviceInterface.pingCVariant(client);
qDebug() << "Pinged CVariant via interface" << cv.toQString();
// test variant lists with different types wrapped in QVariant
// test variant lists with different types wrapped in CVariant
qDebug() << "----------------- variant tests ----------------";
QVariantList qvList;
qvList << QVariant::fromValue(len);
qvList << QVariant::fromValue(alt);
QList<QVariant> lengthsV;
lengthsV.append(QVariant::fromValue(len));
lengthsV.append(QVariant::fromValue(alt));
testserviceInterface.receiveLengthsQvl(qvList);
qDebug() << "Send lengths via interface as QListVariant:";
CVariantList cvList;
cvList.push_back(CVariant::fromValue(len));
cvList.push_back(CVariant::fromValue(alt));
CVariantList lengthsV;
lengthsV.push_back(CVariant::fromValue(len));
lengthsV.push_back(CVariant::fromValue(alt));
testserviceInterface.receiveLengthsQvl(cvList);
qDebug() << "Send lengths via interface as CVariantList:";
testserviceInterface.receiveLengthsQl(lengthsV);
qDebug() << "Send lengths via interface as QList<QVariant>:";
foreach(QVariant lv, qvList)
qDebug() << "Send lengths via interface as QList<CVariant>:";
foreach(CVariant lv, cvList)
{
qDebug() << " " << "Send length in list:" << BlackMisc::qVariantToString(lv);
qDebug() << " " << "Send length in list:" << lv;
}
QThread::msleep(2500);

View File

@@ -41,12 +41,9 @@ namespace BlackMiscTest
/*
* Receive variant
*/
void Testservice::receiveVariant(const QDBusVariant &variant, int localMetaType)
void Testservice::receiveVariant(const CVariant &variant)
{
QVariant qv = variant.variant();
QVariant qvF = BlackMisc::fixQVariantFromDbusArgument(qv, localMetaType);
QString s = BlackMisc::qVariantToString(qvF, true);
qDebug() << "Pid:" << ServiceTool::getPid() << "Received variant:" << s;
qDebug() << "Pid:" << ServiceTool::getPid() << "Received variant:" << variant;
}
/*

View File

@@ -54,7 +54,7 @@ namespace BlackMiscTest
void receiveStringMessage(const QString &message);
//! Receive a QVariant
void receiveVariant(const QDBusVariant &variant, int localMetaType);
void receiveVariant(const BlackMisc::CVariant &variant);
//! Receive speed
void receiveSpeed(const BlackMisc::PhysicalQuantities::CSpeed &speed);

View File

@@ -206,14 +206,14 @@ namespace BlackMiscTest
return asyncCallWithArgumentList(QLatin1String("receiveLength"), argumentList);
}
inline QDBusPendingReply<> receiveLengthsQl(const QVariantList &lengthsList)
inline QDBusPendingReply<> receiveLengthsQl(const BlackMisc::CVariantList &lengthsList)
{
QList<QVariant> argumentList;
argumentList << QVariant::fromValue(lengthsList);
return asyncCallWithArgumentList(QLatin1String("receiveLengthsQl"), argumentList);
}
inline QDBusPendingReply<> receiveLengthsQvl(const QVariantList &lengthsVariantList)
inline QDBusPendingReply<> receiveLengthsQvl(const BlackMisc::CVariantList &lengthsVariantList)
{
QList<QVariant> argumentList;
argumentList << QVariant::fromValue(lengthsVariantList);

View File

@@ -4,7 +4,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "samplesphysicalquantities.h"
#include "blackmisc/variant.h"
using namespace BlackMisc;
using namespace BlackMisc::PhysicalQuantities;
namespace BlackMiscTest
@@ -18,7 +20,7 @@ namespace BlackMiscTest
// parsing
CSpeed parsedPq1 = CPqString::parseToVariant("100.123 km/h").value<CSpeed>();
CLength parsedPq2 = CPqString::parseToVariant("-33.123ft").value<CLength>();
QVariant parsedPq3 = CPqString::parseToVariant("666");
CVariant parsedPq3 = CPqString::parseToVariant("666");
qDebug() << "parsed" << parsedPq1 << parsedPq2 << parsedPq3;
CSpeed speedParsed = CPqString::parse<CSpeed>("111.33ft/s");

View File

@@ -84,6 +84,15 @@ namespace BlackMisc
return !((*this) == other);
}
/*
* Less than?
*/
bool CTestValueObject::operator <(const CTestValueObject &other) const
{
if (this == &other) return false;
return TupleConverter<CTestValueObject>::toTuple(*this) < TupleConverter<CTestValueObject>::toTuple(other);
}
/*
* Hash
*/
@@ -95,16 +104,16 @@ namespace BlackMisc
/*
* Property by index
*/
QVariant CTestValueObject::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
CVariant CTestValueObject::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
{
if (index.isMyself()) { return this->toQVariant(); }
if (index.isMyself()) { return this->toCVariant(); }
ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexDescription:
return QVariant::fromValue(this->m_description);
return CVariant::fromValue(this->m_description);
case IndexName:
return QVariant::fromValue(this->m_name);
return CVariant::fromValue(this->m_name);
default:
return CValueObject::propertyByIndex(index);
}
@@ -113,11 +122,11 @@ namespace BlackMisc
/*
* Property by index (setter)
*/
void CTestValueObject::setPropertyByIndex(const QVariant &variant, const BlackMisc::CPropertyIndex &index)
void CTestValueObject::setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index)
{
if (index.isMyself())
{
this->convertFromQVariant(variant);
this->convertFromCVariant(variant);
return;
}
ColumnIndex i = index.frontCasted<ColumnIndex>();

View File

@@ -57,6 +57,9 @@ namespace BlackMisc
//! Unequal operator !=
bool operator !=(const CTestValueObject &other) const;
//! Less than operator <
bool operator <(const CTestValueObject &other) const;
//! \copydoc CValueObject::getValueHash()
virtual uint getValueHash() const override;
@@ -72,11 +75,11 @@ namespace BlackMisc
//! \copydoc TupleConverter<>::jsonMembers()
static const QStringList &jsonMembers();
//! \copydoc CValueObject::propertyByIndex(int)
virtual QVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const override;
//! \copydoc CValueObject::propertyByIndex
virtual CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const override;
//! \copydoc CValueObject::setPropertyByIndex(const QVariant &, int index)
void setPropertyByIndex(const QVariant &variant, const BlackMisc::CPropertyIndex &index) override;
//! \copydoc CValueObject::setPropertyByIndex
void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index) override;
//! \copydoc CValueObject::toQVariant
virtual QVariant toQVariant() const override

View File

@@ -52,7 +52,7 @@ namespace BlackMiscTest
QVERIFY2(!(l1 > l4), "Null length and non-null length should not be comparable");
QVERIFY2(compare(l1, l4) < 0, "Null length and non-null length should be sortable");
QVariant station1qv = QVariant::fromValue(station1);
CVariant station1qv = CVariant::fromValue(station1);
QVERIFY2(station1 == station1, "Station should be equal");
QVERIFY(station1.getController() == station2.getController());
@@ -60,10 +60,10 @@ namespace BlackMiscTest
QVERIFY2(station1 == station2, "Station should be equal");
QVERIFY2(station1 != station3, "Station should not be equal");
QVERIFY2(station1qv == station1, "Station should be equal (QVariant)");
QVERIFY2(station1 == station1qv, "Station should be equal (QVariant)");
QVERIFY2(station2 == station1qv, "Station should be equal (QVariant)");
QVERIFY2(station3 != station1qv, "Station should be equal (QVariant)");
QVERIFY2(station1qv == station1, "Station should be equal (CVariant)");
QVERIFY2(station1 == station1qv, "Station should be equal (CVariant)");
QVERIFY2(station2 == station1qv, "Station should be equal (CVariant)");
QVERIFY2(station3 != station1qv, "Station should be equal (CVariant)");
QVERIFY2(compare(station1, station1) == 0, "Station should be equal");
QVERIFY2(compare(station1, station2) == 0, "Station should be equal");