FE 0.9.0
Header-only C++ frontend library
Loading...
Searching...
No Matches
enum.h
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4
5namespace fe {
6
7/// @name Bit operations for enum classs
8/// Provides all kind of bit and comparison operators for an `enum class` @p E.
9/// Use like this:
10/// ```
11/// enum class MyEnum : unsigned {
12/// A = 1 << 0,
13/// B = 1 << 1,
14/// C = 1 << 2,
15/// };
16///
17/// template<> struct fe::is_bit_enum<MyEnum> : std::true_type {};
18/// ```
19template<typename T>
20struct is_bit_enum : std::false_type {};
21
22template<typename E>
23concept BitEnum = std::is_enum_v<E> && is_bit_enum<E>::value;
24
25template<fe::BitEnum E> constexpr auto to_underlying(E e) noexcept { return static_cast<std::underlying_type_t<E>>(e); }
26
27} // namespace fe
28
29// clang-format off
30template<fe::BitEnum E> constexpr E operator|(E a, E b) noexcept { return static_cast<E>(fe::to_underlying(a) | fe::to_underlying(b)); }
31template<fe::BitEnum E> constexpr E operator&(E a, E b) noexcept { return static_cast<E>(fe::to_underlying(a) & fe::to_underlying(b)); }
32template<fe::BitEnum E> constexpr E operator^(E a, E b) noexcept { return static_cast<E>(fe::to_underlying(a) ^ fe::to_underlying(b)); }
33template<fe::BitEnum E> constexpr E operator~(E a) noexcept { return static_cast<E>(~fe::to_underlying(a)); }
34template<fe::BitEnum E> constexpr E& operator|=(E& a, E b) noexcept { return a = (a | b); }
35template<fe::BitEnum E> constexpr E& operator&=(E& a, E b) noexcept { return a = (a & b); }
36template<fe::BitEnum E> constexpr E& operator^=(E& a, E b) noexcept { return a = (a ^ b); }
37
38namespace fe {
39template<fe::BitEnum E> constexpr bool has_flag(E value, E flag) noexcept { return (value & flag) == flag; }
40} // namespace fe
41
42// clang-format on
constexpr E operator~(E a) noexcept
Definition enum.h:33
constexpr E operator&(E a, E b) noexcept
Definition enum.h:31
constexpr E & operator&=(E &a, E b) noexcept
Definition enum.h:35
constexpr E & operator|=(E &a, E b) noexcept
Definition enum.h:34
constexpr E operator|(E a, E b) noexcept
Definition enum.h:30
constexpr E operator^(E a, E b) noexcept
Definition enum.h:32
constexpr E & operator^=(E &a, E b) noexcept
Definition enum.h:36
Definition arena.h:13
constexpr auto to_underlying(E e) noexcept
Definition enum.h:25
constexpr bool has_flag(E value, E flag) noexcept
Definition enum.h:39