Debian Codegraph @

Source/Core/Common/EnumMap.h

raw symbols SCIP
1 // Copyright 2021 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7
8 #include "Common/TypeUtils.h"
9
10 template <std::size_t position, std::size_t bits, typename T, typename StorageType>
11 struct BitField;
12
13 namespace Common
14 {
15 // A type that allows lookup of values associated with an enum as the key.
16 // Designed for enums whose numeric values start at 0 and increment continuously with few gaps.
17 template <typename V, Common::Enum auto last_member>
18 class EnumMap final
19 {
20 using T = decltype(last_member);
21 static constexpr size_t s_size = static_cast<size_t>(last_member) + 1;
22
23 using array_type = std::array<V, s_size>;
24 using iterator = typename array_type::iterator;
25 using const_iterator = typename array_type::const_iterator;
26
27 public:
28 constexpr EnumMap() = default;
29 constexpr EnumMap(const EnumMap& other) = default;
30 constexpr EnumMap& operator=(const EnumMap& other) = default;
31 constexpr EnumMap(EnumMap&& other) = default;
32 constexpr EnumMap& operator=(EnumMap&& other) = default;
33
34 // Constructor that accepts exactly size Vs (enforcing that all must be specified).
35 template <typename... T>
36 constexpr EnumMap(T... values) requires(Common::IsNOf<V, s_size, T...>::value)
37 : m_array{static_cast<V>(values)...}
38 {
39 }
40
41 constexpr const V& operator[](T key) const { return m_array[static_cast<std::size_t>(key)]; }
42 constexpr V& operator[](T key) { return m_array[static_cast<std::size_t>(key)]; }
43
44 // These only exist to perform the safety check; without them, BitField's implicit conversion
45 // would work (but since BitField is used for game-generated data, we need to be careful about
46 // bounds-checking)
47 template <std::size_t position, std::size_t bits, typename StorageType>
48 constexpr const V& operator[](BitField<position, bits, T, StorageType> key) const
49 {
50 static_assert(1 << bits == s_size, "Unsafe indexing into EnumMap (may go out of bounds)");
51 return m_array[static_cast<std::size_t>(key.Value())];
52 }
53 template <std::size_t position, std::size_t bits, typename StorageType>
54 constexpr V& operator[](BitField<position, bits, T, StorageType> key)
55 {
56 static_assert(1 << bits == s_size, "Unsafe indexing into EnumMap (may go out of bounds)");
57 return m_array[static_cast<std::size_t>(key.Value())];
58 }
59
60 constexpr bool InBounds(T key) const { return static_cast<std::size_t>(key) < s_size; }
61
62 constexpr size_t size() const noexcept { return s_size; }
63
64 constexpr V* data() { return m_array.data(); }
65 constexpr const V* data() const { return m_array.data(); }
66
67 constexpr iterator begin() { return m_array.begin(); }
68 constexpr iterator end() { return m_array.end(); }
69 constexpr const_iterator begin() const { return m_array.begin(); }
70 constexpr const_iterator end() const { return m_array.end(); }
71 constexpr const_iterator cbegin() const { return m_array.cbegin(); }
72 constexpr const_iterator cend() const { return m_array.cend(); }
73
74 constexpr void fill(const V& v) { m_array.fill(v); }
75
76 private:
77 array_type m_array{};
78 };
79 } // namespace Common