refs #873, nullable interface for value objects

This commit is contained in:
Klaus Basan
2017-02-01 05:10:32 +01:00
committed by Mathew Sutcliffe
parent 273427d3d9
commit f0cbe3b332
5 changed files with 152 additions and 26 deletions

View File

@@ -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<ColumnIndex>();
const ColumnIndex i = index.frontCasted<ColumnIndex>();
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<CAircraftLights>(); return; }
ColumnIndex i = index.frontCasted<ColumnIndex>();
if (INullable::canHandleIndex(index)) { INullable::setPropertyByIndex(index, variant); return; }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexBeacon:
@@ -122,6 +119,5 @@ namespace BlackMisc
m_strobeOn = false;
m_taxiOn = false;
}
} // namespace
} // namespace

View File

@@ -17,6 +17,7 @@
#include "blackmisc/propertyindex.h"
#include "blackmisc/valueobject.h"
#include "blackmisc/variant.h"
#include "blackmisc/nullable.h"
#include <QMetaType>
#include <QString>
@@ -26,7 +27,9 @@ namespace BlackMisc
namespace Aviation
{
//! Value object encapsulating information about aircraft's lights
class BLACKMISC_EXPORT CAircraftLights : public CValueObject<CAircraftLights>
class BLACKMISC_EXPORT CAircraftLights :
public CValueObject<CAircraftLights>,
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"),

View File

@@ -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<int>();
return (i >= static_cast<int>(IndexIsNull)) && (i <= static_cast<int>(IndexIsNull));
}
CVariant INullable::propertyByIndex(const CPropertyIndex &index) const
{
if (!index.isEmpty())
{
ColumnIndex i = index.frontCasted<ColumnIndex>();
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<ColumnIndex>();
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

60
src/blackmisc/nullable.h Normal file
View File

@@ -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

View File

@@ -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,