aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/types/EnumBitfield.hpp
blob: 2b0a1cf922eec56eaaaf0b8bf7848bba7a519989 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma once

#include <type_traits>

namespace OpenVic {
   template<typename E>
      struct is_scoped_enum final : std::bool_constant < requires {
      requires std::is_enum_v<E>;
      requires !std::is_convertible_v<E, std::underlying_type_t<E>>;
   } > {};

   template<class T>
   inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;

   template<typename T>
   concept IsScopedEnum = is_scoped_enum_v<T>;

   template<IsScopedEnum T>
   struct enable_bitfield final : std::false_type {
   };

   template<IsScopedEnum T>
   inline constexpr bool enable_bitfield_v = enable_bitfield<T>::value;

   template<typename T>
   concept EnumSupportBitfield = enable_bitfield_v<T>;
}

template<OpenVic::EnumSupportBitfield T>
[[nodiscard]] constexpr inline auto operator|(const T lhs, const T rhs) noexcept {
   using underlying_type = std::underlying_type_t<T>;
   return static_cast<T>(static_cast<underlying_type>(lhs) | static_cast<underlying_type>(rhs));
}

template<OpenVic::EnumSupportBitfield T>
[[nodiscard]] constexpr inline auto operator&(const T lhs, const T rhs) noexcept {
   using underlying_type = std::underlying_type_t<T>;
   return static_cast<T>(static_cast<underlying_type>(lhs) & static_cast<underlying_type>(rhs));
}

template<OpenVic::EnumSupportBitfield T>
[[nodiscard]] constexpr inline auto operator^(const T lhs, const T rhs) noexcept {
   using underlying_type = std::underlying_type_t<T>;
   return static_cast<T>(static_cast<underlying_type>(lhs) ^ static_cast<underlying_type>(rhs));
}

template<OpenVic::EnumSupportBitfield T>
[[nodiscard]] constexpr inline auto operator~(const T lhs) noexcept {
   using underlying_type = std::underlying_type_t<T>;
   return static_cast<T>(~static_cast<underlying_type>(lhs));
}

template<OpenVic::EnumSupportBitfield T>
constexpr inline decltype(auto) operator|=(T& lhs, const T rhs) noexcept {
   using underlying_type = std::underlying_type_t<T>;
   lhs = static_cast<T>(static_cast<underlying_type>(lhs) | static_cast<underlying_type>(rhs));
   return lhs;
}

template<OpenVic::EnumSupportBitfield T>
constexpr inline decltype(auto) operator&=(T& lhs, const T rhs) noexcept {
   using underlying_type = std::underlying_type_t<T>;
   lhs = static_cast<T>(static_cast<underlying_type>(lhs) & static_cast<underlying_type>(rhs));
   return lhs;
}

template<OpenVic::EnumSupportBitfield T>
constexpr inline decltype(auto) operator^=(T& lhs, const T rhs) noexcept {
   using underlying_type = std::underlying_type_t<T>;
   lhs = static_cast<T>(static_cast<underlying_type>(lhs) ^ static_cast<underlying_type>(rhs));
   return lhs;
}

template<OpenVic::EnumSupportBitfield T>
[[nodiscard]] constexpr inline bool operator<<(const T lhs, const T rhs) noexcept {
   using underlying_type = std::underlying_type_t<T>;
   return (lhs & rhs) == rhs;
}

template<OpenVic::EnumSupportBitfield T>
[[nodiscard]] constexpr inline bool operator>>(const T lhs, const T rhs) noexcept {
   using underlying_type = std::underlying_type_t<T>;
   return (lhs & rhs) == lhs;
}