From 9b060014aff80f51e46e726fc622af508296c3d2 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Sat, 15 Nov 2014 18:42:41 +0000 Subject: [PATCH] refs #247 Using CVariant in samples and tests. --- samples/blackmisc/sampleschangeobject.cpp | 4 +-- samples/blackmisc/samplescontainer.cpp | 2 +- samples/blackmisc/samplesvariant.cpp | 32 +++++++++++-------- samples/blackmiscdbus/servicetool.cpp | 24 +++++++------- samples/blackmiscdbus/testservice.cpp | 7 ++-- samples/blackmiscdbus/testservice.h | 2 +- samples/blackmiscdbus/testservice_interface.h | 4 +-- .../samplesphysicalquantities.cpp | 4 ++- tests/blackmisc/testvalueobject.cpp | 21 ++++++++---- tests/blackmisc/testvalueobject.h | 11 ++++--- tests/blackmisc/testvariantandmap.cpp | 10 +++--- 11 files changed, 68 insertions(+), 53 deletions(-) diff --git a/samples/blackmisc/sampleschangeobject.cpp b/samples/blackmisc/sampleschangeobject.cpp index 98564f84e..6be73d2ad 100644 --- a/samples/blackmisc/sampleschangeobject.cpp +++ b/samples/blackmisc/sampleschangeobject.cpp @@ -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"; diff --git a/samples/blackmisc/samplescontainer.cpp b/samples/blackmisc/samplescontainer.cpp index d98bdece6..721af90be 100644 --- a/samples/blackmisc/samplescontainer.cpp +++ b/samples/blackmisc/samplescontainer.cpp @@ -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 diff --git a/samples/blackmisc/samplesvariant.cpp b/samples/blackmisc/samplesvariant.cpp index 3d3ca8736..086bbe4c0 100644 --- a/samples/blackmisc/samplesvariant.cpp +++ b/samples/blackmisc/samplesvariant.cpp @@ -8,12 +8,14 @@ */ #include "samplesvariant.h" +#include "blackmisc/variant.h" #include "blackmisc/pqallquantities.h" #include "blackmisc/avallclasses.h" #include "blackmisc/blackmiscfreefunctions.h" #include #include +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; } diff --git a/samples/blackmiscdbus/servicetool.cpp b/samples/blackmiscdbus/servicetool.cpp index 89207c08d..7a6a87e7a 100644 --- a/samples/blackmiscdbus/servicetool.cpp +++ b/samples/blackmiscdbus/servicetool.cpp @@ -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 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:"; - foreach(QVariant lv, qvList) + qDebug() << "Send lengths via interface as QList:"; + foreach(CVariant lv, cvList) { - qDebug() << " " << "Send length in list:" << BlackMisc::qVariantToString(lv); + qDebug() << " " << "Send length in list:" << lv; } QThread::msleep(2500); diff --git a/samples/blackmiscdbus/testservice.cpp b/samples/blackmiscdbus/testservice.cpp index 6037398d3..b3b99c6fb 100644 --- a/samples/blackmiscdbus/testservice.cpp +++ b/samples/blackmiscdbus/testservice.cpp @@ -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; } /* diff --git a/samples/blackmiscdbus/testservice.h b/samples/blackmiscdbus/testservice.h index ef1d40807..5c7125458 100644 --- a/samples/blackmiscdbus/testservice.h +++ b/samples/blackmiscdbus/testservice.h @@ -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); diff --git a/samples/blackmiscdbus/testservice_interface.h b/samples/blackmiscdbus/testservice_interface.h index 64b44e4ba..f55e864a6 100644 --- a/samples/blackmiscdbus/testservice_interface.h +++ b/samples/blackmiscdbus/testservice_interface.h @@ -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 argumentList; argumentList << QVariant::fromValue(lengthsList); return asyncCallWithArgumentList(QLatin1String("receiveLengthsQl"), argumentList); } - inline QDBusPendingReply<> receiveLengthsQvl(const QVariantList &lengthsVariantList) + inline QDBusPendingReply<> receiveLengthsQvl(const BlackMisc::CVariantList &lengthsVariantList) { QList argumentList; argumentList << QVariant::fromValue(lengthsVariantList); diff --git a/samples/blackmiscquantities/samplesphysicalquantities.cpp b/samples/blackmiscquantities/samplesphysicalquantities.cpp index 90062ba56..cb19577cc 100644 --- a/samples/blackmiscquantities/samplesphysicalquantities.cpp +++ b/samples/blackmiscquantities/samplesphysicalquantities.cpp @@ -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(); CLength parsedPq2 = CPqString::parseToVariant("-33.123ft").value(); - QVariant parsedPq3 = CPqString::parseToVariant("666"); + CVariant parsedPq3 = CPqString::parseToVariant("666"); qDebug() << "parsed" << parsedPq1 << parsedPq2 << parsedPq3; CSpeed speedParsed = CPqString::parse("111.33ft/s"); diff --git a/tests/blackmisc/testvalueobject.cpp b/tests/blackmisc/testvalueobject.cpp index 91dd1dfac..8820c594a 100644 --- a/tests/blackmisc/testvalueobject.cpp +++ b/tests/blackmisc/testvalueobject.cpp @@ -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::toTuple(*this) < TupleConverter::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(); 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(); diff --git a/tests/blackmisc/testvalueobject.h b/tests/blackmisc/testvalueobject.h index 8b8431213..5447f742f 100644 --- a/tests/blackmisc/testvalueobject.h +++ b/tests/blackmisc/testvalueobject.h @@ -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 diff --git a/tests/blackmisc/testvariantandmap.cpp b/tests/blackmisc/testvariantandmap.cpp index 9742a3be8..692a64120 100644 --- a/tests/blackmisc/testvariantandmap.cpp +++ b/tests/blackmisc/testvariantandmap.cpp @@ -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");