CTransponder: setting code as string and refactored validation

refs #81
This commit is contained in:
Klaus Basan
2013-12-20 22:49:23 +00:00
committed by Mathew Sutcliffe
parent c37e160a42
commit bf0a849a7c
2 changed files with 63 additions and 14 deletions

View File

@@ -17,18 +17,7 @@ namespace BlackMisc
bool CTransponder::validValues() const bool CTransponder::validValues() const
{ {
if (this->isDefaultValue()) return true; // special case if (this->isDefaultValue()) return true; // special case
if (this->m_transponderCode < 0 || this->m_transponderCode > 7777) return false; return CTransponder::isValidTransponderCode(this->m_transponderCode);
// check each digit
qint32 tc = this->m_transponderCode;
qint32 d;
while (tc > 7)
{
d = (tc % 10);
if (d > 7) return false;
tc /= 10;
}
return true;
} }
/* /*
@@ -60,7 +49,7 @@ namespace BlackMisc
QString CTransponder::getModeAsString() const QString CTransponder::getModeAsString() const
{ {
QString m; QString m;
switch (this->m_transponderMode) switch (this->getTransponderMode())
{ {
case StateIdent: case StateIdent:
m = "Ident"; m = "Ident";
@@ -115,6 +104,45 @@ namespace BlackMisc
return s; return s;
} }
/*
* Transponder by string
*/
void CTransponder::setTransponderCode(const QString &transponderCode)
{
if (CTransponder::isValidTransponderCode(transponderCode))
{
bool ok;
this->setTransponderCode(transponderCode.toInt(&ok));
}
else
{
Q_ASSERT_X(false, "CTransponder::setTransponderCode", "illegal transponder value");
}
}
/*
* Valid code?
*/
bool CTransponder::isValidTransponderCode(const QString &transponderCode)
{
if (transponderCode.isEmpty() || transponderCode.length() > 4) return false;
bool number;
qint32 tc = transponderCode.toInt(&number);
if (!number) return false;
if (tc < 0 || tc > 7777) return false;
QRegExp rx("[0-7]{1,4}");
return rx.exactMatch(transponderCode);
}
/*
* Valid code?
*/
bool CTransponder::isValidTransponderCode(qint32 transponderCode)
{
if (transponderCode < 0 || transponderCode > 7777) return false;
return CTransponder::isValidTransponderCode(QString::number(transponderCode));
}
/* /*
* Stream to DBus << * Stream to DBus <<
*/ */

View File

@@ -205,6 +205,13 @@ namespace BlackMisc
this->validate(true); this->validate(true);
} }
/*!
* \brief Set transponder code
* \param transponderCode
*/
void setTransponderCode(const QString &transponderCode);
/*! /*!
* \brief Set transponder mode * \brief Set transponder mode
* \param mode * \param mode
@@ -248,7 +255,7 @@ namespace BlackMisc
{ {
return return
this->m_transponderCode == other.m_transponderCode && this->m_transponderCode == other.m_transponderCode &&
this->m_transponderMode == other.m_transponderMode && this->getTransponderMode() == other.getTransponderMode() &&
this->CAvionicsBase::operator ==(other); this->CAvionicsBase::operator ==(other);
} }
@@ -337,6 +344,20 @@ namespace BlackMisc
*/ */
virtual uint getValueHash() const; virtual uint getValueHash() const;
/*!
* \brief Is valid transponder code?
* \param transponderCode
* \return
*/
static bool isValidTransponderCode(const QString &transponderCode);
/*!
* \brief Is valid transponder code?
* \param transponderCode
* \return
*/
static bool isValidTransponderCode(qint32 transponderMode);
/*! /*!
* \brief Register metadata of unit and quantity * \brief Register metadata of unit and quantity
*/ */