aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/sqlite/sqliteids.h
blob: 2228f7372aa7023850c39f73a61f189186fb7923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include <utils/span.h>
#include <utils/utility.h>

#include <nanotrace/nanotracehr.h>
#include <type_traits>
#include <vector>

namespace Sqlite {

template<auto Type, typename InternalIntegerType = long long>
class BasicId
{
    template<auto, auto>
    friend class CompoundBasicId;

public:
    using IsBasicId = std::true_type;
    using DatabaseType = InternalIntegerType;

    constexpr explicit BasicId() = default;

    static constexpr BasicId create(InternalIntegerType idNumber)
    {
        BasicId id;
        id.id = idNumber;
        return id;
    }

    template<typename Enumeration>
    static constexpr BasicId createSpecialState(Enumeration specialState)
    {
        BasicId id;
        id.id = ::Utils::to_underlying(specialState);
        return id;
    }

    friend constexpr bool compareInvalidAreTrue(BasicId first, BasicId second)
    {
        return first.id == second.id;
    }

    friend constexpr bool operator==(BasicId first, BasicId second)
    {
        return first.id == second.id && first.isValid();
    }

    friend constexpr auto operator<=>(BasicId first, BasicId second) = default;

    constexpr friend InternalIntegerType operator-(BasicId first, BasicId second)
    {
        return first.id - second.id;
    }

    constexpr bool isValid() const { return id > 0; }

    constexpr bool isNull() const { return id == 0; }

    template<typename Enumeration>
    constexpr bool hasSpecialState(Enumeration specialState) const
    {
        return id == ::Utils::to_underlying(specialState);
    }

    explicit operator bool() const { return isValid(); }

    explicit operator std::size_t() const { return static_cast<std::size_t>(id); }

    constexpr InternalIntegerType internalId() const { return id; }

    [[noreturn, deprecated]] InternalIntegerType operator&() const { throw std::exception{}; }

    template<typename String>
    friend void convertToString(String &string, BasicId id)
    {
        NanotraceHR::convertToString(string, id.id);
    }

    friend bool compareId(BasicId first, BasicId second) { return first.id == second.id; }

protected:
    InternalIntegerType id = 0;
};

template<auto Type, auto ContextType>
class CompoundBasicId
{
public:
    using IsBasicId = std::true_type;
    using DatabaseType = long long;

    using MainId = BasicId<Type, int>;
    using ContextId = BasicId<ContextType, int>;

    constexpr explicit CompoundBasicId() = default;

    static constexpr CompoundBasicId create(MainId id, ContextId contextId)
    {
        CompoundBasicId compoundId;
        compoundId.id = (static_cast<long long>(contextId.id) << 32) | static_cast<long long>(id.id);

        return compoundId;
    }

    static constexpr CompoundBasicId create(long long idNumber)
    {
        CompoundBasicId id;
        id.id = idNumber;

        return id;
    }

    constexpr MainId mainId() const { return MainId::create(id); }

    constexpr ContextId contextId() const { return ContextId::create(id >> 32); }

    friend constexpr bool compareInvalidAreTrue(CompoundBasicId first, CompoundBasicId second)
    {
        return first.id == second.id;
    }

    friend constexpr bool operator==(CompoundBasicId first, CompoundBasicId second)
    {
        return first.id == second.id && first.isValid();
    }

    friend constexpr auto operator<=>(CompoundBasicId first, CompoundBasicId second) = default;

    constexpr bool isValid() const { return id != 0; }

    constexpr bool isNull() const { return id == 0; }

    explicit operator bool() const { return isValid(); }

    long long internalId() const { return id; }

    explicit operator std::size_t() const { return static_cast<std::size_t>(id | 0xFFFFFFFFULL); }

    template<typename String>
    friend void convertToString(String &string, CompoundBasicId id)
    {
        int mainId = id.id;
        int contextId = id.id >> 32;
        convertToString(string, mainId);
        convertToString(string, contextId);
    }

    friend bool compareId(CompoundBasicId first, CompoundBasicId second)
    {
        return first.id == second.id;
    }

protected:
    long long id = 0;
};

template<typename Container>
auto toIntegers(const Container &container)
{
    using DataType = typename Container::value_type::DatabaseType;
    const DataType *data = reinterpret_cast<const DataType *>(container.data());

    return Utils::span{data, container.size()};
}

} // namespace Sqlite

namespace std {
template<auto Type, typename InternalIntegerType>
struct hash<Sqlite::BasicId<Type, InternalIntegerType>>
{
    auto operator()(const Sqlite::BasicId<Type, InternalIntegerType> &id) const
    {
        return std::hash<InternalIntegerType>{}(id.internalId());
    }
};

} // namespace std