Debian Codegraph @

common/main/d_array.h

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7
8 #pragma once
9 #include "dxxsconf.h"
10 #include <array>
11 #include <optional>
12 #include "fwd-d_array.h"
13
14 namespace dcx {
15
16 /* Wrap a std::array and override the normal indexing operations to take
17 * an instance of type `E`. This is intended for use where `E` is an
18 * instance of `enum class`. When `E` is an `enum class`, instances of
19 * `E` cannot implicitly convert to `std::array::size_type`, and so
20 * would require a cast in the caller if this wrapper were not used.
21 * Using this wrapper moves the casts into the wrapper, after the
22 * compiler has checked that the argument is the right type of `enum
23 * class`. This prevents accidentally using an incorrect `enum class`.
24 *
25 * Other types for E are not likely to be useful, but are not blocked.
26 */
27 template <typename T, std::size_t N, typename E>
28 /* `std::size_t` is `unsigned` on i686-w64-mingw32, and `unsigned long` on
29 * x86_64-pc-linux-gnu.
30 *
31 * As a result, if `E` is `unsigned`, then i686-w64-mingw32 breaks due to
32 * `operator[](E)` and `operator[](std::size_t)` being the same signature. If
33 * `E` is `unsigned long`, then x86_64-pc-linux-gnu breaks for the same reason.
34 * Disallow both `unsigned` and `unsigned long`, so that any attempt to use
35 * either for `E` will break everywhere, rather than breaking only some
36 * platforms.
37 */
38 requires(!std::is_same<unsigned, E>::value && !std::is_same<unsigned long, E>::value)
39 struct enumerated_array : std::array<T, N>
40 {
41 using base_type = std::array<T, N>;
42 using index_type = E;
43 using typename base_type::reference;
44 using typename base_type::const_reference;
45 [[nodiscard]]
46 constexpr reference at(E position)
47 {
48 return this->base_type::at(static_cast<std::size_t>(position));
49 }
50 [[nodiscard]]
51 constexpr const_reference at(E position) const
52 {
53 return this->base_type::at(static_cast<std::size_t>(position));
54 }
55 [[nodiscard]]
56 constexpr reference operator[](E position)
57 {
58 return this->base_type::operator[](static_cast<std::size_t>(position));
59 }
60 [[nodiscard]]
61 constexpr const_reference operator[](E position) const
62 {
63 return this->base_type::operator[](static_cast<std::size_t>(position));
64 }
65 const_reference operator[](std::integral auto) const = delete;
66 [[nodiscard]]
67 static constexpr std::optional<E> valid_index(const std::size_t s)
68 {
69 /* static_cast<E> is necessary here, since
70 * `sizeof(E) < sizeof(std::size_t)` is supported and commonly used.
71 * The narrowing rule is stateless, and therefore does not observe that
72 * the cast is on a path where `s < N` ensures no narrowing can occur.
73 */
74 if (s < N)
75 {
76 [[likely]];
77 return std::optional(static_cast<E>(s));
78 }
79 return std::nullopt;
80 }
81 [[nodiscard]]
82 static constexpr std::optional<E> valid_index(const E e)
83 {
84 if (static_cast<std::size_t>(e) < N)
85 {
86 [[likely]];
87 return std::optional{e};
88 }
89 return std::nullopt;
90 }
91 };
92
93 }