aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/types
diff options
context:
space:
mode:
author Joel Machens <ajmach6@gmail.com>2023-11-05 04:09:49 +0100
committer Joel Machens <ajmach6@gmail.com>2023-11-08 03:14:08 +0100
commit4e2e6e541f0dde406f90f668d6ff153f95d2aa45 (patch)
tree1af05925a4a5ac18430a0c2444e6edd2fe098b9b /src/openvic-simulation/types
parentae2742113ec7283a2a5afa62f8bfd98a865c4208 (diff)
Implement Wargoal Loading
Diffstat (limited to 'src/openvic-simulation/types')
-rw-r--r--src/openvic-simulation/types/EnumBitfield.hpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/openvic-simulation/types/EnumBitfield.hpp b/src/openvic-simulation/types/EnumBitfield.hpp
new file mode 100644
index 0000000..2b0a1cf
--- /dev/null
+++ b/src/openvic-simulation/types/EnumBitfield.hpp
@@ -0,0 +1,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;
+}