header_utils
Loading...
Searching...
No Matches
enums.h
1
4
5#pragma once
6
7#include "min-cpp-version/cpp17.h"
8
10#include <magic_enum/magic_enum.hpp>
11#include <magic_enum/magic_enum_utility.hpp>
12#include <array>
13
14namespace ghassanpl
15{
16 namespace enums
17 {
18 using namespace magic_enum;
19 }
20
22 template <typename ENUM_TYPE>
23 GHPL_REQUIRES(std::is_enum_v<ENUM_TYPE>)
24 struct circular_enum
25 {
26 using enum_type = ENUM_TYPE;
27 using underlying_type = std::underlying_type_t<enum_type>;
28 enum_type value;
29
30 //auto& operator=(enum_type val) noexcept { value = val; return *this; }
31 circular_enum& operator++() noexcept {
32 value = magic_enum::enum_next_value_circular(value);
33 return *this;
34 }
35 operator enum_type() const noexcept { return value; }
36 };
37
38 template <typename ENUM_TYPE>
39 GHPL_REQUIRES(std::is_enum_v<ENUM_TYPE>)
40 constexpr auto IncWrap(ENUM_TYPE& val) noexcept
41 {
42 val = magic_enum::enum_next_value_circular(val);
43 return val;
44 }
45
46 template <typename ENUM_TYPE>
47 GHPL_REQUIRES(std::is_enum_v<ENUM_TYPE>)
48 constexpr auto DecWrap(ENUM_TYPE& val) noexcept
49 {
50 val = magic_enum::enum_prev_value_circular(val);
51 return val;
52 }
53
57 template <typename VALUE_TYPE, typename ENUM_TYPE>
58 GHPL_REQUIRES(std::is_enum_v<ENUM_TYPE>)
59 struct enum_array : public std::array<VALUE_TYPE, magic_enum::enum_count<ENUM_TYPE>()>
60 {
61 using array_type = std::array<VALUE_TYPE, magic_enum::enum_count<ENUM_TYPE>()>;
62
63 using value_type = VALUE_TYPE;
64 using size_type = typename array_type::size_type;
65 using difference_type = typename array_type::difference_type;
66 using pointer = typename array_type::pointer;
67 using const_pointer = typename array_type::const_pointer;
68 using reference = typename array_type::reference;
69 using const_reference = typename array_type::const_reference;
70 using iterator = typename array_type::iterator;
71 using const_iterator = typename array_type::const_iterator;
72 using reverse_iterator = typename array_type::reverse_iterator;
73 using const_reverse_iterator = typename array_type::const_reverse_iterator;
74
76
77 using enum_type = ENUM_TYPE;
78
79 using array_type::at;
80
81 [[nodiscard]] constexpr reference at(ENUM_TYPE pos) {
82 return this->array_type::at(magic_enum::enum_index(pos).value());
83 }
84
85 [[nodiscard]] constexpr const_reference at(ENUM_TYPE pos) const {
86 return this->array_type::at(magic_enum::enum_index(pos).value());
87 }
88
89 using array_type::operator[];
90
91 [[nodiscard]] constexpr reference operator[](ENUM_TYPE pos) noexcept {
92 return this->array_type::operator[](magic_enum::enum_index(pos).value());
93 }
94
95 [[nodiscard]] constexpr const_reference operator[](ENUM_TYPE pos) const noexcept {
96 return this->array_type::operator[](magic_enum::enum_index(pos).value());
97 }
98 };
99}
auto iterator(T &cont, RANDOM &rng=::ghassanpl::random::default_random_engine)
Returns an iterator to a random element in cont.
Definition random.h:300
constexpr decltype(auto) at(random_access_range auto &range, std::integral auto index)
Returns a reference to the value at index of range.
Definition ranges.h:61
Primary namespace for everything in this library.
Definition align+rec2.h:10
GHPL_REQUIRES(std::is_enum_v< ENUM_TYPE >) struct circular_enum
TODO: Finish this.
Definition enums.h:23