KItinerary

alphaid.h
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "kitinerary_export.h"
10
11#include <QDebug>
12#include <QString>
13#include <QStringView>
14
15#include <cstdint>
16#include <functional>
17
18namespace KItinerary {
19namespace KnowledgeDb {
20
21///@cond internal
22namespace Internal {
23 // "private" API for the template code below
24 KITINERARY_EXPORT uint32_t alphaIdFromString(QStringView s, int size);
25 KITINERARY_EXPORT QString alphaIdToString(uint32_t id, int size);
26}
27///@endcond
28
29/**
30 * Compact storage for fixed-size identifiers consisting out of uppercase latin letters,
31 * such as IATA airport codes or ISO 3166 country codes.
32 */
33template <typename T, int N>
34class AlphaId {
35 static_assert((N * 5) < (sizeof(T) * 8), "Not enough space to hold identifier.");
36public:
37 inline constexpr AlphaId() = default;
38 /** Create identifier from a literal. */
39 inline explicit constexpr AlphaId(const char s[N])
40 {
41 for (int i = 0; i < N; ++i) {
42 if (s[i] < 'A' || s[i] > 'Z') {
43 m_id = {};
44 return;
45 }
46 m_id |= (s[i] - '@') << (5 * (N-i-1));
47 }
48 }
49 /** Create identifier from a QString. */
50 inline explicit AlphaId(QStringView s)
51 {
52 m_id = Internal::alphaIdFromString(s, N);
53 }
54
55 /** Returns @c true if this is a valid identifier. */
56 inline constexpr bool isValid() const
57 {
58 return m_id != 0;
59 }
60
61 inline constexpr bool operator<(AlphaId<T, N> rhs) const
62 {
63 return m_id < rhs.m_id;
64 }
65 inline constexpr bool operator==(AlphaId<T, N> other) const
66 {
67 return m_id == other.m_id;
68 }
69 inline constexpr bool operator!=(AlphaId<T, N> other) const
70 {
71 return m_id != other.m_id;
72 }
73
74 /** Returns a string representation of this identifier. */
75 inline QString toString() const
76 {
77 return Internal::alphaIdToString(m_id, N);
78 }
79
80 /** @internal for std::hash */
81 inline constexpr T value() const
82 {
83 return m_id;
84 }
85private:
86 T m_id = {};
87};
88
89}
90
91}
92
93template <typename T, int N>
95{
96 dbg << id.toString();
97 return dbg;
98}
99
100namespace std {
101template <typename T, int N> struct hash<KItinerary::KnowledgeDb::AlphaId<T, N>>
102{
103 typedef KItinerary::KnowledgeDb::AlphaId<T, N> argument_type;
104 typedef std::size_t result_type;
105 result_type operator()(argument_type id) const noexcept
106 {
107 return std::hash<T>()(id.value());
108 }
109};
110}
Compact storage for fixed-size identifiers consisting out of uppercase latin letters,...
Definition alphaid.h:34
constexpr bool isValid() const
Returns true if this is a valid identifier.
Definition alphaid.h:56
constexpr T value() const
Definition alphaid.h:81
QString toString() const
Returns a string representation of this identifier.
Definition alphaid.h:75
constexpr AlphaId(const char s[N])
Create identifier from a literal.
Definition alphaid.h:39
AlphaId(QStringView s)
Create identifier from a QString.
Definition alphaid.h:50
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QDebug operator<<(QDebug dbg, const PerceptualColor::LchaDouble &value)
QString toString(T &&object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:08:32 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.