KItinerary

countrydb.cpp
1 /*
2  Copyright (C) 2018 Volker Krause <[email protected]>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This program is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 #include "countrydb.h"
19 #include "countrydb_data.cpp"
20 
21 #include <QDebug>
22 #include <QString>
23 
24 #include <algorithm>
25 
26 using namespace KItinerary;
27 using namespace KItinerary::KnowledgeDb;
28 
29 static_assert(sizeof(CountryId) <= 2, "CountryId too large");
30 
32 {
33  const auto it = std::lower_bound(std::begin(country_table), std::end(country_table), id, [](const Country &lhs, CountryId rhs) {
34  return lhs.id < rhs;
35  });
36  if (it == std::end(country_table) || (*it).id != id) {
37  return {CountryId{}, DrivingSide::Unknown, Unknown};
38  }
39  return (*it);
40 }
41 
43 {
44  return std::begin(country_table);
45 }
46 
48 {
49  return std::end(country_table);
50 }
51 
52 struct PowerPlugCompatMap {
53  PowerPlugType plug;
54  PowerPlugTypes sockets;
55 };
56 
57 static const PowerPlugCompatMap power_plug_compat_map[] = {
58  { TypeA, TypeA | TypeB },
59  { TypeB, TypeB },
60  { TypeC, TypeC | TypeE | TypeF | TypeH | TypeJ | TypeK | TypeL | TypeN },
61  { TypeD, TypeD },
62  { TypeE, TypeE | TypeF | TypeK }, // TypeE <-> TypeF not strictly correct, but in practice almost always compatible
63  { TypeF, TypeE | TypeF | TypeK },
64  { TypeG, TypeG },
65  { TypeH, TypeH },
66  { TypeI, TypeI },
67  { TypeJ, TypeJ },
68  { TypeK, TypeK },
69  { TypeL, TypeL },
70  { TypeM, TypeM },
71  { TypeN, TypeN }
72 };
73 
75 {
76  PowerPlugTypes failPlugs{};
77  for (const auto map : power_plug_compat_map) {
78  if ((plugs & map.plug) == 0) {
79  continue;
80  }
81  if ((map.sockets & sockets) == 0) {
82  failPlugs |= map.plug;
83  }
84  }
85  return failPlugs;
86 }
87 
89 {
90  PowerPlugTypes failSockets{};
91  for (const auto map : power_plug_compat_map) {
92  if ((plugs & map.plug) == 0) {
93  continue;
94  }
95  if ((map.sockets & ~sockets) != 0) {
96  failSockets |= (map.sockets ^ sockets) & sockets;
97  }
98  }
99  return failSockets & ~plugs;
100 }
101 
103 {
104  const auto it = std::lower_bound(std::begin(iso_country_code_table), std::end(iso_country_code_table), iso3Code, [](const auto &lhs, CountryId3 rhs) {
105  return lhs.iso3Code < rhs;
106  });
107  if (it == std::end(iso_country_code_table) || (*it).iso3Code != iso3Code) {
108  return {};
109  }
110 
111  return (*it).iso2Code;
112 }
113 
115 {
116  const auto it = std::lower_bound(std::begin(uic_country_code_table), std::end(uic_country_code_table), uicCountryCode, [](const auto &lhs, uint16_t rhs) {
117  return lhs.uicCode < rhs;
118  });
119  if (it == std::end(uic_country_code_table) || (*it).uicCode != uicCountryCode) {
120  return {};
121  }
122 
123  return (*it).isoCode;
124 }
125 
126 #include "moc_countrydb.cpp"
const Country * countriesEnd()
Iterator access for the country information table.
Definition: countrydb.cpp:47
PowerPlugTypes incompatiblePowerPlugs(PowerPlugTypes plugs, PowerPlugTypes sockets)
Returns the power plugs out of plugs that wont fit into sockets.
Definition: countrydb.cpp:74
Classes for reservation/travel data models, data extraction and data augmentation.
Lookup functions, utilities and data types for the static knowledge database.
Definition: airportdb.cpp:34
CountryId countryIdFromIso3166_1alpha3(CountryId3 iso3Code)
Look up country ISO 3166-1 alpha 2 code from an ISO 3166-1 alpha 3 code.
Definition: countrydb.cpp:102
Type N (Brasilian)
Definition: countrydb.h:65
US three-pin plugs.
Definition: countrydb.h:53
PowerPlugTypes incompatiblePowerSockets(PowerPlugTypes plugs, PowerPlugTypes sockets)
Returns the power sockets out pf sockets that are unable to receive plugs out of plugs, excluding those in plugs.
Definition: countrydb.cpp:88
Country countryForId(CountryId id)
Look up contry infromation by id.
Definition: countrydb.cpp:31
Country information.
Definition: countrydb.h:79
CountryId countryIdForUicCode(uint16_t uicCountryCode)
Look up country ISO code from a UIC country code.
Definition: countrydb.cpp:114
US two-pin plugs.
Definition: countrydb.h:52
const Country * countriesBegin()
Iterator access for the country information table.
Definition: countrydb.cpp:42
PowerPlugType
Power plug types.
Definition: countrydb.h:50
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 24 2020 22:42:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.