alphaid.h 2.67 KB
Newer Older
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
10
11
12
13
14

#include "kitinerary_export.h"

#include <QDebug>
#include <QString>

#include <cstdint>
15
#include <functional>
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

namespace KItinerary {
namespace KnowledgeDb {

///@cond internal
namespace Internal {
    // "private" API for the template code below
    KITINERARY_EXPORT uint16_t alphaIdFromString(const QString &s, int size);
    KITINERARY_EXPORT QString alphaIdToString(uint16_t id, int size);
}
///@endcond

/**
 *  Compact storage for fixed-size identifiers consisting out of uppercase latin letters,
 *  such as IATA airport codes or ISO 3166 country codes.
 */
template <typename T, int N>
class AlphaId {
    static_assert((N * 5) < (sizeof(T) * 8), "Not enough space to hold identifier.");
public:
    inline constexpr AlphaId() = default;
    /** Create identifier from a literal. */
    inline explicit constexpr AlphaId(const char s[N])
    {
        for (int i = 0; i < N; ++i) {
            if (s[i] < 'A' || s[i] > 'Z') {
                m_id = {};
                return;
            }
            m_id |= (s[i] - '@') << (5 * (N-i-1));
        }
    }
    /** Create identifier from a QString. */
    inline explicit AlphaId(const QString &s)
    {
        static_assert(sizeof(decltype(Internal::alphaIdFromString(s, N))) <= sizeof(T), "alphaIdFromString truncates result");
        m_id = Internal::alphaIdFromString(s, N);
    }

    /** Returns @c true if this is a valid idenfier. */
    inline constexpr bool isValid() const
    {
        return m_id != 0;
    }

    inline constexpr bool operator<(AlphaId<T, N> rhs) const
    {
        return m_id < rhs.m_id;
    }
    inline constexpr bool operator==(AlphaId<T, N> other) const
    {
        return m_id == other.m_id;
    }
    inline constexpr bool operator!=(AlphaId<T, N> other) const
    {
        return m_id != other.m_id;
    }

    /** Returns a string representation of this identifier. */
    inline QString toString() const
    {
        return Internal::alphaIdToString(m_id, N);
    }

80
81
82
83
84
    /** @internal for std::hash */
    inline constexpr T value() const
    {
        return m_id;
    }
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
private:
    T m_id = {};
};

}

}

template <typename T, int N>
inline QDebug operator<<(QDebug dbg, KItinerary::KnowledgeDb::AlphaId<T, N> id)
{
    dbg << id.toString();
    return dbg;
}

100
101
102
103
104
105
106
107
108
109
110
namespace std {
template <typename T, int N> struct hash<KItinerary::KnowledgeDb::AlphaId<T, N>>
{
    typedef KItinerary::KnowledgeDb::AlphaId<T, N> argument_type;
    typedef std::size_t result_type;
    result_type operator()(argument_type id) const noexcept
    {
        return std::hash<T>()(id.value());
    }
};
}