From f0cbe3b332431bfdca6121eb7ae3d28a69f189cb Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 1 Feb 2017 05:10:32 +0100 Subject: [PATCH] refs #873, nullable interface for value objects --- src/blackmisc/aviation/aircraftlights.cpp | 14 ++--- src/blackmisc/aviation/aircraftlights.h | 28 ++++----- src/blackmisc/nullable.cpp | 69 +++++++++++++++++++++++ src/blackmisc/nullable.h | 60 ++++++++++++++++++++ src/blackmisc/propertyindex.h | 7 ++- 5 files changed, 152 insertions(+), 26 deletions(-) create mode 100644 src/blackmisc/nullable.cpp create mode 100644 src/blackmisc/nullable.h diff --git a/src/blackmisc/aviation/aircraftlights.cpp b/src/blackmisc/aviation/aircraftlights.cpp index 09eb03c1d..65397050a 100644 --- a/src/blackmisc/aviation/aircraftlights.cpp +++ b/src/blackmisc/aviation/aircraftlights.cpp @@ -18,12 +18,6 @@ namespace BlackMisc { namespace Aviation { - - CAircraftLights::CAircraftLights(std::nullptr_t null) : m_isNull(true) - { - Q_UNUSED(null); - } - CAircraftLights::CAircraftLights(bool strobeOn, bool landingOn, bool taxiOn, bool beaconOn, bool navOn, bool logoOn) : m_strobeOn(strobeOn), m_landingOn(landingOn), m_taxiOn(taxiOn), m_beaconOn(beaconOn), m_navOn(navOn), m_logoOn(logoOn) { } @@ -52,8 +46,9 @@ namespace BlackMisc CVariant CAircraftLights::propertyByIndex(const BlackMisc::CPropertyIndex &index) const { if (index.isMyself()) { return CVariant::from(*this); } + if (INullable::canHandleIndex(index)) { return INullable::propertyByIndex(index); } - ColumnIndex i = index.frontCasted(); + const ColumnIndex i = index.frontCasted(); switch (i) { case IndexBeacon: @@ -76,7 +71,9 @@ namespace BlackMisc void CAircraftLights::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) { if (index.isMyself()) { (*this) = variant.to(); return; } - ColumnIndex i = index.frontCasted(); + if (INullable::canHandleIndex(index)) { INullable::setPropertyByIndex(index, variant); return; } + + const ColumnIndex i = index.frontCasted(); switch (i) { case IndexBeacon: @@ -122,6 +119,5 @@ namespace BlackMisc m_strobeOn = false; m_taxiOn = false; } - } // namespace } // namespace diff --git a/src/blackmisc/aviation/aircraftlights.h b/src/blackmisc/aviation/aircraftlights.h index a2368b4c4..30fab636e 100644 --- a/src/blackmisc/aviation/aircraftlights.h +++ b/src/blackmisc/aviation/aircraftlights.h @@ -17,6 +17,7 @@ #include "blackmisc/propertyindex.h" #include "blackmisc/valueobject.h" #include "blackmisc/variant.h" +#include "blackmisc/nullable.h" #include #include @@ -26,7 +27,9 @@ namespace BlackMisc namespace Aviation { //! Value object encapsulating information about aircraft's lights - class BLACKMISC_EXPORT CAircraftLights : public CValueObject + class BLACKMISC_EXPORT CAircraftLights : + public CValueObject, + public INullable { public: //! Properties by index @@ -44,17 +47,11 @@ namespace BlackMisc CAircraftLights() = default; //! Constructor, init to null - CAircraftLights(std::nullptr_t null); + using INullable::INullable; //! Constructor CAircraftLights(bool strobeOn, bool landingOn, bool taxiOn, bool beaconOn, bool navOn, bool logoOn); - //! \copydoc BlackMisc::Mixin::Index::propertyByIndex - CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const; - - //! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex - void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant); - //! Strobes lights on? bool isStrobeOn() const { return m_strobeOn; } @@ -97,8 +94,14 @@ namespace BlackMisc //! All off void setAllOff(); - //! Is null; - bool isNull() const { return m_isNull; } + //! \copydoc BlackMisc::Mixin::Index::propertyByIndex + CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const; + + //! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex + void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant); + + //! \copydoc BlackMisc::Mixin::String::toQString + QString convertToQString(bool i18n = false) const; //! Returns object with all lights switched on static CAircraftLights allLightsOn(); @@ -106,9 +109,6 @@ namespace BlackMisc //! Returns object with all lights switched off static CAircraftLights allLightsOff(); - //! \copydoc BlackMisc::Mixin::String::toQString - QString convertToQString(bool i18n = false) const; - private: bool m_strobeOn = false; bool m_landingOn = false; @@ -116,10 +116,10 @@ namespace BlackMisc bool m_beaconOn = false; bool m_navOn = false; bool m_logoOn = false; - bool m_isNull = false; //!< mark as null BLACK_METACLASS( CAircraftLights, + BLACK_METAMEMBER(isNull, 0, DisabledForJson), // disable since JSON is used for network BLACK_METAMEMBER_NAMED(strobeOn, "strobe_on"), BLACK_METAMEMBER_NAMED(landingOn, "landing_on"), BLACK_METAMEMBER_NAMED(taxiOn, "taxi_on"), diff --git a/src/blackmisc/nullable.cpp b/src/blackmisc/nullable.cpp new file mode 100644 index 000000000..2105d1828 --- /dev/null +++ b/src/blackmisc/nullable.cpp @@ -0,0 +1,69 @@ +/* Copyright (C) 2017 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "blackmisc/comparefunctions.h" +#include "blackmisc/nullable.h" +#include "blackmisc/verify.h" + +namespace BlackMisc +{ + INullable::INullable(std::nullptr_t null) : m_isNull(true) + { + Q_UNUSED(null); + } + + bool INullable::canHandleIndex(const CPropertyIndex &index) + { + if (index.isEmpty()) { return false; } + int i = index.frontCasted(); + return (i >= static_cast(IndexIsNull)) && (i <= static_cast(IndexIsNull)); + } + + CVariant INullable::propertyByIndex(const CPropertyIndex &index) const + { + if (!index.isEmpty()) + { + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexIsNull: + return CVariant::fromValue(this->isNull()); + default: + break; + } + } + const QString m = QString("Cannot handle index %1").arg(index.toQString()); + BLACK_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m)); + return CVariant::fromValue(m); + } + + void INullable::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) + { + if (!index.isEmpty()) + { + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexIsNull: + this->setNull(variant.toBool()); + return; + default: + break; + } + } + const QString m = QString("Cannot handle index %1").arg(index.toQString()); + BLACK_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m)); + } + + int INullable::comparePropertyByIndex(const CPropertyIndex &index, const INullable &compareValue) const + { + Q_UNUSED(index); + return Compare::compare(this->m_isNull, compareValue.m_isNull); + } +} // namespace diff --git a/src/blackmisc/nullable.h b/src/blackmisc/nullable.h new file mode 100644 index 000000000..dea3238e3 --- /dev/null +++ b/src/blackmisc/nullable.h @@ -0,0 +1,60 @@ +/* Copyright (C) 2017 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_NULLABLE_H +#define BLACKMISC_NULLABLE_H + +#include "blackmisc/blackmiscexport.h" +#include "blackmisc/propertyindex.h" +#include "blackmisc/variant.h" + +namespace BlackMisc +{ + //! Nullable value object + class BLACKMISC_EXPORT INullable + { + public: + //! Properties by index + enum ColumnIndex + { + IndexIsNull = BlackMisc::CPropertyIndex::GlobalIndexINullable, + }; + + //! Constructor, init to null + INullable(std::nullptr_t null); + + //! Null? + bool isNull() const { return m_isNull; } + + //! Null? + void setNull(bool null) { m_isNull = null; } + + //! Can given index be handled + static bool canHandleIndex(const BlackMisc::CPropertyIndex &index); + + protected: + //! Constructor + INullable() {} + + //! \copydoc BlackMisc::Mixin::Index::propertyByIndex + CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const; + + //! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex + void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant); + + //! Compare for index + int comparePropertyByIndex(const CPropertyIndex &index, const INullable &compareValue) const; + + bool m_isNull = false; //!< null? + }; +} // namespace + +#endif // guard diff --git a/src/blackmisc/propertyindex.h b/src/blackmisc/propertyindex.h index ba54c61b4..8d94a889c 100644 --- a/src/blackmisc/propertyindex.h +++ b/src/blackmisc/propertyindex.h @@ -82,9 +82,10 @@ namespace BlackMisc GlobalIndexCNameVariantPair = 300, GlobalIndexITimestampBased = 400, GlobalIndexIOrderable = 500, - GlobalIndexCIdentifier = 600, - GlobalIndexCRgbColor = 700, - GlobalIndexCCountry = 800, + GlobalIndexINullable = 600, + GlobalIndexCIdentifier = 700, + GlobalIndexCRgbColor = 800, + GlobalIndexCCountry = 900, GlobalIndexCCallsign = 1000, GlobalIndexCAircraftSituation = 1100, GlobalIndexCAtcStation = 1200,