Debian Codegraph @

common/main/d_bitset.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 <bitset>
10 #include <optional>
11
12 namespace dcx {
13
14 /* Wrap std::bitset similar to how enumerated_array wraps std::array.
15 * See d_array.h for a full description.
16 */
17 template <std::size_t N, typename E>
18 class enumerated_bitset : std::bitset<N>
19 {
20 using base_type = std::bitset<N>;
21 public:
22 using base_type::base_type;
23 using base_type::size;
24 constexpr typename base_type::reference operator[](E position)
25 {
26 return this->base_type::operator[](static_cast<std::size_t>(position));
27 }
28 constexpr bool operator[](E position) const
29 {
30 return this->base_type::operator[](static_cast<std::size_t>(position));
31 }
32 enumerated_bitset &reset()
33 {
34 return static_cast<enumerated_bitset &>(this->base_type::reset());
35 }
36 enumerated_bitset &reset(E position)
37 {
38 return static_cast<enumerated_bitset &>(this->base_type::reset(static_cast<std::size_t>(position)));
39 }
40 enumerated_bitset &set(E position)
41 {
42 return static_cast<enumerated_bitset &>(this->base_type::set(static_cast<std::size_t>(position)));
43 }
44 [[nodiscard]]
45 static constexpr std::optional<E> valid_index(const std::size_t s)
46 {
47 return s < N ? std::optional(static_cast<E>(s)) : std::nullopt;
48 }
49 [[nodiscard]]
50 static constexpr bool valid_index(E e)
51 {
52 return static_cast<std::size_t>(e) < N;
53 }
54 };
55
56 }