IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
enum_flags.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/types.hxx>
6#include <type_traits>
7
8namespace ice
9{
10
11 template<typename T>
12 struct FlagLogic
13 {
14 static constexpr bool IsEnabled = std::is_enum_v<T>;
15 };
16
17 template<typename T>
18 concept FlagType = FlagLogic<T>::IsEnabled && requires(T) {
19 { T::None } -> std::convertible_to<T>;
20 };
21
22 template<typename T>
23 concept FlagAllValue = FlagType<T> && requires(T) {
24 { T::All } -> std::convertible_to<T>;
25 };
26
27 template<ice::FlagType T>
28 constexpr auto operator|(T left, T right) noexcept -> T
29 {
30 auto const left_value = static_cast<std::underlying_type_t<T>>(left);
31 auto const right_value = static_cast<std::underlying_type_t<T>>(right);
32 return static_cast<T>(left_value | right_value);
33 }
34
35 template<ice::FlagType T>
36 constexpr auto operator|=(T& left, T right) noexcept -> T&
37 {
38 auto const left_value = static_cast<std::underlying_type_t<T>>(left);
39 auto const right_value = static_cast<std::underlying_type_t<T>>(right);
40 left = static_cast<T>(left_value | right_value);
41 return left;
42 }
43
44 template<ice::FlagType T>
45 constexpr auto operator&(T left, T right) noexcept -> T
46 {
47 auto const left_value = static_cast<std::underlying_type_t<T>>(left);
48 auto const right_value = static_cast<std::underlying_type_t<T>>(right);
49 return static_cast<T>(left_value & right_value);
50 }
51
52 template<ice::FlagType T>
53 constexpr auto operator&=(T& left, T right) noexcept -> T&
54 {
55 auto const left_value = static_cast<std::underlying_type_t<T>>(left);
56 auto const right_value = static_cast<std::underlying_type_t<T>>(right);
57 left = static_cast<T>(left_value & right_value);
58 return left;
59 }
60
61 template<ice::FlagType T>
62 constexpr auto operator~(T left) noexcept -> T
63 {
64 auto const left_value = static_cast<std::underlying_type_t<T>>(left);
65 if constexpr (FlagAllValue<T>)
66 {
67 auto const all_value = static_cast<std::underlying_type_t<T>>(T::All);
68 return static_cast<T>(left_value ^ all_value);
69 }
70 else
71 {
72 return static_cast<T>(~left_value);
73 }
74 }
75
76 template<ice::FlagType T>
77 constexpr bool has_all(T value, T expected_flags) noexcept
78 {
79 return (value & expected_flags) == expected_flags;
80 }
81
82 template<ice::FlagType T>
83 constexpr bool has_any(T value, T expected_flags) noexcept
84 {
85 return (value & expected_flags) != T::None;
86 }
87
88 template<ice::FlagType T>
89 constexpr bool has_none(T value, T expected_flags) noexcept
90 {
91 return (value & expected_flags) == T::None;
92 }
93
94} // namespace ice
Definition enum_flags.hxx:23
Definition enum_flags.hxx:18
SPDX-License-Identifier: MIT.
Definition array.hxx:12
constexpr bool has_any(T value, T expected_flags) noexcept
Definition enum_flags.hxx:83
constexpr auto operator&(T left, T right) noexcept -> T
Definition enum_flags.hxx:45
constexpr auto operator|(T left, T right) noexcept -> T
Definition enum_flags.hxx:28
constexpr auto operator&=(T &left, T right) noexcept -> T &
Definition enum_flags.hxx:53
constexpr bool has_none(T value, T expected_flags) noexcept
Definition enum_flags.hxx:89
constexpr auto operator|=(T &left, T right) noexcept -> T &
Definition enum_flags.hxx:36
constexpr auto operator~(T left) noexcept -> T
Definition enum_flags.hxx:62
constexpr bool has_all(T value, T expected_flags) noexcept
Definition enum_flags.hxx:77
Definition enum_flags.hxx:13
static constexpr bool IsEnabled
Definition enum_flags.hxx:14