summaryrefslogtreecommitdiff
path: root/AK/DistinctNumeric.h
blob: 308668451ebf61bad039d4d6c285d2e5a353d96c (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright (c) 2020, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Format.h>
#include <AK/Traits.h>
#include <AK/Types.h>

namespace AK {

/**
 * This implements a "distinct" numeric type that is intentionally incompatible
 * to other incantations. The intention is that each "distinct" type that you
 * want simply gets different values for `fn_length` and `line`. The macros
 * `TYPEDEF_DISTINCT_NUMERIC_*()` at the bottom of `DistinctNumeric.h`.
 *
 * `Incr`, `Cmp`, `Bool`, `Flags`, `Shift`, and `Arith` simply split up the
 * space of operators into 6 simple categories:
 * - No matter the values of these, `DistinctNumeric` always implements `==` and `!=`.
 * - If `Incr` is true, then `++a`, `a++`, `--a`, and `a--` are implemented.
 * - If `Cmp` is true, then `a>b`, `a<b`, `a>=b`, and `a<=b` are implemented.
 * - If `Bool` is true, then `!a`, `a&&b`, and `a||b` are implemented (but not `operator bool()`, because of overzealous integer promotion rules).
 * - If `Flags` is true, then `~a`, `a&b`, `a|b`, `a^b`, `a&=b`, `a|=b`, and `a^=b` are implemented.
 * - If `Shift` is true, then `a<<b`, `a>>b`, `a<<=b`, `a>>=b` are implemented.
 * - If `Arith` is true, then `a+b`, `a-b`, `+a`, `-a`, `a*b`, `a/b`, `a%b`, and the respective `a_=b` versions are implemented.
 * The semantics are always those of the underlying basic type `T`.
 *
 * These can be combined arbitrarily. Want a numeric type that supports `++a`
 * and `a >> b` but not `a > b`? Sure thing, just set
 * `Incr=true, Cmp=false, Shift=true` and you're done!
 * Furthermore, some of these overloads make more sense with specific types, like `a&&b` which should be able to operate
 *
 * I intentionally decided against overloading `&a` because these shall remain
 * numeric types.
 *
 * The C++20 `operator<=>` would require, among other things `std::weak_equality`.
 * Since we do not have that, it cannot be implemented.
 *
 * The are many operators that do not work on `int`, so I left them out:
 * `a[b]`, `*a`, `a->b`, `a.b`, `a->*b`, `a.*b`.
 *
 * There are many more operators that do not make sense for numerical types,
 * or cannot be overloaded in the first place. Naturally, they are not implemented.
 */
template<typename T, typename X, bool Incr, bool Cmp, bool Bool, bool Flags, bool Shift, bool Arith>
class DistinctNumeric {
    using Self = DistinctNumeric<T, X, Incr, Cmp, Bool, Flags, Shift, Arith>;

public:
    constexpr DistinctNumeric() = default;

    constexpr DistinctNumeric(T value)
        : m_value { value }
    {
    }

    constexpr const T& value() const { return m_value; }

    // Always implemented: identity.
    constexpr bool operator==(const Self& other) const
    {
        return this->m_value == other.m_value;
    }
    constexpr bool operator!=(const Self& other) const
    {
        return this->m_value != other.m_value;
    }

    // Only implemented when `Incr` is true:
    constexpr Self& operator++()
    {
        static_assert(Incr, "'++a' is only available for DistinctNumeric types with 'Incr'.");
        this->m_value += 1;
        return *this;
    }
    constexpr Self operator++(int)
    {
        static_assert(Incr, "'a++' is only available for DistinctNumeric types with 'Incr'.");
        Self ret = this->m_value;
        this->m_value += 1;
        return ret;
    }
    constexpr Self& operator--()
    {
        static_assert(Incr, "'--a' is only available for DistinctNumeric types with 'Incr'.");
        this->m_value -= 1;
        return *this;
    }
    constexpr Self operator--(int)
    {
        static_assert(Incr, "'a--' is only available for DistinctNumeric types with 'Incr'.");
        Self ret = this->m_value;
        this->m_value -= 1;
        return ret;
    }

    // Only implemented when `Cmp` is true:
    constexpr bool operator>(const Self& other) const
    {
        static_assert(Cmp, "'a>b' is only available for DistinctNumeric types with 'Cmp'.");
        return this->m_value > other.m_value;
    }
    constexpr bool operator<(const Self& other) const
    {
        static_assert(Cmp, "'a<b' is only available for DistinctNumeric types with 'Cmp'.");
        return this->m_value < other.m_value;
    }
    constexpr bool operator>=(const Self& other) const
    {
        static_assert(Cmp, "'a>=b' is only available for DistinctNumeric types with 'Cmp'.");
        return this->m_value >= other.m_value;
    }
    constexpr bool operator<=(const Self& other) const
    {
        static_assert(Cmp, "'a<=b' is only available for DistinctNumeric types with 'Cmp'.");
        return this->m_value <= other.m_value;
    }
    // 'operator<=>' cannot be implemented. See class comment.

    // Only implemented when `bool` is true:
    constexpr bool operator!() const
    {
        static_assert(Bool, "'!a' is only available for DistinctNumeric types with 'Bool'.");
        return !this->m_value;
    }
    // Intentionally don't define `operator bool() const` here. C++ is a bit
    // overzealous, and whenever there would be a type error, C++ instead tries
    // to convert to a common int-ish type first. `bool` is int-ish, so
    // `operator bool() const` would defy the entire point of this class.

    // Only implemented when `Flags` is true:
    constexpr Self operator~() const
    {
        static_assert(Flags, "'~a' is only available for DistinctNumeric types with 'Flags'.");
        return ~this->m_value;
    }
    constexpr Self operator&(const Self& other) const
    {
        static_assert(Flags, "'a&b' is only available for DistinctNumeric types with 'Flags'.");
        return this->m_value & other.m_value;
    }
    constexpr Self operator|(const Self& other) const
    {
        static_assert(Flags, "'a|b' is only available for DistinctNumeric types with 'Flags'.");
        return this->m_value | other.m_value;
    }
    constexpr Self operator^(const Self& other) const
    {
        static_assert(Flags, "'a^b' is only available for DistinctNumeric types with 'Flags'.");
        return this->m_value ^ other.m_value;
    }
    constexpr Self& operator&=(const Self& other)
    {
        static_assert(Flags, "'a&=b' is only available for DistinctNumeric types with 'Flags'.");
        this->m_value &= other.m_value;
        return *this;
    }
    constexpr Self& operator|=(const Self& other)
    {
        static_assert(Flags, "'a|=b' is only available for DistinctNumeric types with 'Flags'.");
        this->m_value |= other.m_value;
        return *this;
    }
    constexpr Self& operator^=(const Self& other)
    {
        static_assert(Flags, "'a^=b' is only available for DistinctNumeric types with 'Flags'.");
        this->m_value ^= other.m_value;
        return *this;
    }

    // Only implemented when `Shift` is true:
    // TODO: Should this take `int` instead?
    constexpr Self operator<<(const Self& other) const
    {
        static_assert(Shift, "'a<<b' is only available for DistinctNumeric types with 'Shift'.");
        return this->m_value << other.m_value;
    }
    constexpr Self operator>>(const Self& other) const
    {
        static_assert(Shift, "'a>>b' is only available for DistinctNumeric types with 'Shift'.");
        return this->m_value >> other.m_value;
    }
    constexpr Self& operator<<=(const Self& other)
    {
        static_assert(Shift, "'a<<=b' is only available for DistinctNumeric types with 'Shift'.");
        this->m_value <<= other.m_value;
        return *this;
    }
    constexpr Self& operator>>=(const Self& other)
    {
        static_assert(Shift, "'a>>=b' is only available for DistinctNumeric types with 'Shift'.");
        this->m_value >>= other.m_value;
        return *this;
    }

    // Only implemented when `Arith` is true:
    constexpr Self operator+(const Self& other) const
    {
        static_assert(Arith, "'a+b' is only available for DistinctNumeric types with 'Arith'.");
        return this->m_value + other.m_value;
    }
    constexpr Self operator-(const Self& other) const
    {
        static_assert(Arith, "'a-b' is only available for DistinctNumeric types with 'Arith'.");
        return this->m_value - other.m_value;
    }
    constexpr Self operator+() const
    {
        static_assert(Arith, "'+a' is only available for DistinctNumeric types with 'Arith'.");
        return +this->m_value;
    }
    constexpr Self operator-() const
    {
        static_assert(Arith, "'-a' is only available for DistinctNumeric types with 'Arith'.");
        return -this->m_value;
    }
    constexpr Self operator*(const Self& other) const
    {
        static_assert(Arith, "'a*b' is only available for DistinctNumeric types with 'Arith'.");
        return this->m_value * other.m_value;
    }
    constexpr Self operator/(const Self& other) const
    {
        static_assert(Arith, "'a/b' is only available for DistinctNumeric types with 'Arith'.");
        return this->m_value / other.m_value;
    }
    constexpr Self operator%(const Self& other) const
    {
        static_assert(Arith, "'a%b' is only available for DistinctNumeric types with 'Arith'.");
        return this->m_value % other.m_value;
    }
    constexpr Self& operator+=(const Self& other)
    {
        static_assert(Arith, "'a+=b' is only available for DistinctNumeric types with 'Arith'.");
        this->m_value += other.m_value;
        return *this;
    }
    constexpr Self& operator-=(const Self& other)
    {
        static_assert(Arith, "'a+=b' is only available for DistinctNumeric types with 'Arith'.");
        this->m_value += other.m_value;
        return *this;
    }
    constexpr Self& operator*=(const Self& other)
    {
        static_assert(Arith, "'a*=b' is only available for DistinctNumeric types with 'Arith'.");
        this->m_value *= other.m_value;
        return *this;
    }
    constexpr Self& operator/=(const Self& other)
    {
        static_assert(Arith, "'a/=b' is only available for DistinctNumeric types with 'Arith'.");
        this->m_value /= other.m_value;
        return *this;
    }
    constexpr Self& operator%=(const Self& other)
    {
        static_assert(Arith, "'a%=b' is only available for DistinctNumeric types with 'Arith'.");
        this->m_value %= other.m_value;
        return *this;
    }

private:
    T m_value {};
};

template<typename T, typename X, bool Incr, bool Cmp, bool Bool, bool Flags, bool Shift, bool Arith>
struct Formatter<DistinctNumeric<T, X, Incr, Cmp, Bool, Flags, Shift, Arith>> : Formatter<T> {
    ErrorOr<void> format(FormatBuilder& builder, DistinctNumeric<T, X, Incr, Cmp, Bool, Flags, Shift, Arith> value)
    {
        return Formatter<T>::format(builder, value.value());
    }
};

// TODO: When 'consteval' sufficiently-well supported by host compilers, try to
// provide a more usable interface like this one:
// https://gist.github.com/alimpfard/a3b750e8c3a2f44fb3a2d32038968ddf

}

#define TYPEDEF_DISTINCT_NUMERIC_GENERAL(T, Incr, Cmp, Bool, Flags, Shift, Arith, NAME) \
    using NAME = DistinctNumeric<T, struct __##NAME##_tag, Incr, Cmp, Bool, Flags, Shift, Arith>;
#define TYPEDEF_DISTINCT_ORDERED_ID(T, NAME) TYPEDEF_DISTINCT_NUMERIC_GENERAL(T, false, true, true, false, false, false, NAME)
// TODO: Further type aliases?

template<typename T, typename X, auto... Args>
struct Traits<AK::DistinctNumeric<T, X, Args...>> : public GenericTraits<AK::DistinctNumeric<T, X, Args...>> {
    static constexpr bool is_trivial() { return true; }
    static constexpr auto hash(const DistinctNumeric<T, X, Args...>& d) { return Traits<T>::hash(d.value()); }
};

using AK::DistinctNumeric;