Commit 38479978 authored by Volker Krause's avatar Volker Krause

Move IataCode class to its own file

parent 2a1b3357
......@@ -26,6 +26,7 @@ set(kitinerary_lib_srcs
knowledgedb/airportdb.cpp
knowledgedb/countrydb.cpp
knowledgedb/iatacode.cpp
knowledgedb/knowledgedb.cpp
knowledgedb/timezonedb.cpp
knowledgedb/trainstationdb.cpp
......
......@@ -32,64 +32,12 @@
namespace KItinerary {
namespace KnowledgeDb {
static_assert(sizeof(IataCode) == sizeof(uint16_t), "IATA code changed size!");
static_assert(alignof(Airport) <= sizeof(Airport), "Airport struct alignment too big!");
static constexpr auto airport_table_size = sizeof(airport_table) / sizeof(Airport);
static_assert(airport_table_size == sizeof(coordinate_table) / sizeof(KnowledgeDb::Coordinate), "Airport coordinate table size mismatch!");
IataCode::IataCode(const QString &iataStr) : IataCode()
{
if (iataStr.size() != 3) {
return;
}
if (!iataStr.at(0).isUpper() || !iataStr.at(1).isUpper() || !iataStr.at(2).isUpper()) {
return;
}
m_letter0 = iataStr.at(0).toLatin1() - 'A';
m_letter1 = iataStr.at(1).toLatin1() - 'A';
m_letter2 = iataStr.at(2).toLatin1() - 'A';
m_valid = 1;
}
bool IataCode::isValid() const
{
return m_valid;
}
bool IataCode::operator<(IataCode rhs) const
{
return toUInt16() < rhs.toUInt16();
}
bool IataCode::operator==(IataCode other) const
{
return memcmp(this, &other, sizeof(IataCode)) == 0;
}
bool IataCode::operator!=(IataCode other) const
{
return memcmp(this, &other, sizeof(IataCode)) != 0;
}
QString IataCode::toString() const
{
if (!isValid()) {
return QString();
}
QString s;
s.reserve(3);
s.push_back(QLatin1Char(m_letter0 + 'A'));
s.push_back(QLatin1Char(m_letter1 + 'A'));
s.push_back(QLatin1Char(m_letter2 + 'A'));
return s;
}
uint16_t IataCode::toUInt16() const
{
return m_letter0 << 11 | m_letter1 << 6 | m_letter2 << 1 | m_valid;
}
static bool operator<(const Airport &lhs, IataCode rhs)
{
return lhs.iataCode < rhs;
......
......@@ -21,54 +21,17 @@
#define AIRPORTDB_H
#include "kitinerary_export.h"
#include "knowledgedb.h"
#include "countrydb.h"
#include "iatacode.h"
#include "knowledgedb.h"
#include "timezonedb.h"
#include <cmath>
#include <cstdint>
class QString;
class QTimeZone;
namespace KItinerary {
namespace KnowledgeDb {
/** IATA airport code. */
class KITINERARY_EXPORT IataCode
{
public:
inline constexpr IataCode()
: m_letter0(0)
, m_letter1(0)
, m_letter2(0)
, m_valid(0)
{
}
inline explicit constexpr IataCode(const char iataStr[4])
: m_letter0(iataStr[0] - 'A')
, m_letter1(iataStr[1] - 'A')
, m_letter2(iataStr[2] - 'A')
, m_valid(1)
{
}
explicit IataCode(const QString &iataStr);
bool isValid() const;
bool operator<(IataCode rhs) const;
bool operator==(IataCode other) const;
bool operator!=(IataCode other) const;
QString toString() const;
private:
uint16_t toUInt16() const;
uint16_t m_letter0 : 5;
uint16_t m_letter1 : 5;
uint16_t m_letter2 : 5;
uint16_t m_valid : 1;
};
/** Airport information structure as used in the database.
* @internal
*/
......
/*
Copyright (c) 2017-2019 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "iatacode.h"
#include <QDebug>
QDebug operator<<(QDebug dbg, KItinerary::KnowledgeDb::IataCode code)
{
dbg << code.toString();
return dbg;
}
namespace KItinerary {
namespace KnowledgeDb {
static_assert(sizeof(IataCode) == sizeof(uint16_t), "IATA code changed size!");
IataCode::IataCode(const QString &iataStr) : IataCode()
{
if (iataStr.size() != 3) {
return;
}
if (!iataStr.at(0).isUpper() || !iataStr.at(1).isUpper() || !iataStr.at(2).isUpper()) {
return;
}
m_letter0 = iataStr.at(0).toLatin1() - 'A';
m_letter1 = iataStr.at(1).toLatin1() - 'A';
m_letter2 = iataStr.at(2).toLatin1() - 'A';
m_valid = 1;
}
bool IataCode::isValid() const
{
return m_valid;
}
bool IataCode::operator<(IataCode rhs) const
{
return toUInt16() < rhs.toUInt16();
}
bool IataCode::operator==(IataCode other) const
{
return memcmp(this, &other, sizeof(IataCode)) == 0;
}
bool IataCode::operator!=(IataCode other) const
{
return memcmp(this, &other, sizeof(IataCode)) != 0;
}
QString IataCode::toString() const
{
if (!isValid()) {
return QString();
}
QString s;
s.reserve(3);
s.push_back(QLatin1Char(m_letter0 + 'A'));
s.push_back(QLatin1Char(m_letter1 + 'A'));
s.push_back(QLatin1Char(m_letter2 + 'A'));
return s;
}
uint16_t IataCode::toUInt16() const
{
return m_letter0 << 11 | m_letter1 << 6 | m_letter2 << 1 | m_valid;
}
}}
/*
Copyright (c) 2017-2019 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef KITINERARY_KNOWLEDGEDB_IATACODE_H
#define KITINERARY_KNOWLEDGEDB_IATACODE_H
#include "kitinerary_export.h"
#include <cstdint>
class QDebug;
class QString;
namespace KItinerary {
namespace KnowledgeDb {
/** IATA airport code. */
class KITINERARY_EXPORT IataCode
{
public:
inline constexpr IataCode()
: m_letter0(0)
, m_letter1(0)
, m_letter2(0)
, m_valid(0)
{
}
inline explicit constexpr IataCode(const char iataStr[4])
: m_letter0(iataStr[0] - 'A')
, m_letter1(iataStr[1] - 'A')
, m_letter2(iataStr[2] - 'A')
, m_valid(1)
{
}
explicit IataCode(const QString &iataStr);
bool isValid() const;
bool operator<(IataCode rhs) const;
bool operator==(IataCode other) const;
bool operator!=(IataCode other) const;
QString toString() const;
private:
uint16_t toUInt16() const;
uint16_t m_letter0 : 5;
uint16_t m_letter1 : 5;
uint16_t m_letter2 : 5;
uint16_t m_valid : 1;
};
}}
KITINERARY_EXPORT QDebug operator<<(QDebug dbg, KItinerary::KnowledgeDb::IataCode code);
#endif // KITINERARY_KNOWLEDGEDB_IATACODE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment