KItinerary

knowledgedb.h
1/*
2 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <cmath>
10#include <cstdint>
11
12namespace KItinerary {
13
14/** Lookup functions, utilities and data types for the static knowledge database.
15 * The content accessible by this functions is extracted from Wikidata and compiled
16 * into this library.
17 * @note The types in this namespace match the binary storage structure and thus
18 * are not intended for use in binary compatible APIs.
19 */
20namespace KnowledgeDb
21{
22
23/** Geographical coordinate.
24 * This matches the binary data layout on disk, it's not intended
25 * for use in API.
26 */
27struct Coordinate {
28 inline constexpr Coordinate()
29 : longitude(NAN)
30 , latitude(NAN)
31 {
32 }
33
34 inline explicit constexpr Coordinate(float lng, float lat)
35 : longitude(lng)
36 , latitude(lat)
37 {
38 }
39
40 inline bool isValid() const
41 {
42 return !std::isnan(latitude) && !std::isnan(longitude);
43 }
44
45 inline constexpr bool operator==(Coordinate other) const
46 {
47 return latitude == other.latitude && longitude == other.longitude;
48 }
49
50 float longitude;
51 float latitude;
52};
53
54/** Unalinged storage of a numerical value.
55 * This is optimized for a compact memory layout, at the expense of slightly more
56 * expensive comparison operations.
57 * @tparam N the size in byte, at this point limited to at most 4
58 */
59template <int N> class UnalignedNumber : private UnalignedNumber<N-1> {
60public:
61 inline constexpr UnalignedNumber() = default;
62 inline explicit constexpr UnalignedNumber(uint32_t num)
63 : UnalignedNumber<N-1>(num)
64 , m_value((num & (0xFF << (N-1)*8)) >> (N-1)*8)
65 {}
66
67 inline constexpr bool operator==(UnalignedNumber<N> other) const
68 {
69 if (m_value == other.m_value) {
71 }
72 return false;
73 }
74 inline constexpr bool operator!=(UnalignedNumber<N> other) const
75 {
76 if (m_value == other.m_value) {
78 }
79 return true;
80 }
81 inline constexpr bool operator<(UnalignedNumber<N> other) const
82 {
83 if (m_value == other.m_value) {
85 }
86 return m_value < other.m_value;
87 }
88
89 inline constexpr UnalignedNumber<N>& operator=(uint32_t num)
90 {
91 setValue(num);
92 return *this;
93 }
94 inline constexpr UnalignedNumber<N>& operator|=(uint32_t num)
95 {
96 setValue(value() | num);
97 return *this;
98 }
99
100 inline constexpr operator uint32_t() const
101 {
102 return value();
103 }
104
105 inline constexpr uint32_t value() const
106 {
107 return UnalignedNumber<N-1>::value() | (m_value << (N-1)*8);
108 }
109
110protected:
111 inline constexpr void setValue(uint32_t num)
112 {
113 m_value = (num & (0xFF << (N-1)*8)) >> (N-1)*8;
115 }
116
117private:
118 uint8_t m_value = 0;
119};
120
121template <> class UnalignedNumber<1> {
122public:
123 inline constexpr UnalignedNumber() = default;
124 inline explicit constexpr UnalignedNumber(uint32_t num)
125 : m_value(num & 0xFF)
126 {}
127
128 inline constexpr bool operator==(UnalignedNumber<1> other) const
129 {
130 return m_value == other.m_value;
131 }
132 inline constexpr bool operator!=(UnalignedNumber<1> other) const
133 {
134 return m_value != other.m_value;
135 }
136 inline constexpr bool operator<(UnalignedNumber<1> other) const
137 {
138 return m_value < other.m_value;
139 }
140
141 inline constexpr uint32_t value() const
142 {
143 return m_value;
144 }
145
146protected:
147 inline constexpr void setValue(uint32_t num)
148 {
149 m_value = num & 0xFF;
150 }
151
152private:
153 uint8_t m_value = 0;
154};
155
156}
157
158}
159
Unalinged storage of a numerical value.
Definition knowledgedb.h:59
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
Geographical coordinate.
Definition knowledgedb.h:27
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.