From 91d53805b51a129132cd699e33cb0df97e9f4b8e Mon Sep 17 00:00:00 2001
From: Patrick Wuttke
Date: Thu, 27 Mar 2025 15:07:45 +0100
Subject: [PATCH] Semi-disabled bitFlagsToInt() again, since it is quite
error-prone.
---
source/mijin/util/bitflags.hpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/source/mijin/util/bitflags.hpp b/source/mijin/util/bitflags.hpp
index e34446a..e16a886 100644
--- a/source/mijin/util/bitflags.hpp
+++ b/source/mijin/util/bitflags.hpp
@@ -29,6 +29,7 @@ template
struct BitFlags
{
using bits_t = TBits;
+ static constexpr bool ENABLE_TO_INT = false;
constexpr TBits& operator |=(const BitFlags& other) {
for (std::size_t idx = 0; idx < sizeof(TBits); ++idx) {
@@ -103,6 +104,11 @@ concept BitFlagsType = is_bitflags_v;
template
TInt bitFlagsToInt(const BitFlags& flags) noexcept
{
+ // If a BitFlags object contains padding (number of defined bits < number of bits in type), the bits in the padding might not be 0.
+ // In that case bit_casting will produce an incorrect value.
+ // Filling the gaps with padding bits fixes this, but is unfortunately quite error prone :/.
+ static_assert(T::ENABLE_TO_INT, "bitFlagsToInt not enabled for this type. Make sure the number of bits defined is the same as the number of bits in the type and define ENABLE_TO_INT to true.");
+
static constexpr std::size_t BYTES = std::min(sizeof(T), sizeof(TInt));
TInt result = 0;