summaryrefslogtreecommitdiff
path: root/lib/utils
diff options
context:
space:
mode:
authorDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2026-01-27 23:05:35 +0200
committerDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2026-01-27 23:05:35 +0200
commitf21dbeab19488b61da11ecd22dccbf0f73662df3 (patch)
tree11e20a351d5ce1a8772b430b56295453aaebeb0a /lib/utils
parente6f08ae266f7c075b2047bea2d7c7ea2a108bfa2 (diff)
utils: rework MU_ENABLE_BITOPS using C++20 code
Instead of macros, we using C++20 concepts to define the helpers to deal with bitops on enum-class conveniently. x# Please enter the commit message for your changes. Lines starting
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/mu-utils.hh62
-rw-r--r--lib/utils/tests/test-utils.cc4
2 files changed, 48 insertions, 18 deletions
diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh
index c465689..a1c8979 100644
--- a/lib/utils/mu-utils.hh
+++ b/lib/utils/mu-utils.hh
@@ -1,5 +1,5 @@
/*
-** Copyright (C) 2020-2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
+** Copyright (C) 2020-2026 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
@@ -31,6 +31,7 @@
#include <ostream>
#include <iostream>
#include <type_traits>
+#include <concepts>
#include <algorithm>
#include <numeric>
@@ -673,23 +674,48 @@ private:
#define MU_COLOR_DEFAULT "\x1b[0m"
-/// Allow using enum structs as bitflags
-#define MU_TO_NUM(ET, ELM) std::underlying_type_t<ET>(ELM)
-#define MU_TO_ENUM(ET, NUM) static_cast<ET>(NUM)
-#define MU_ENABLE_BITOPS(ET) \
- constexpr ET operator&(ET e1, ET e2) { \
- return MU_TO_ENUM(ET, MU_TO_NUM(ET, e1) & MU_TO_NUM(ET, e2)); \
- } \
- constexpr ET operator|(ET e1, ET e2) { \
- return MU_TO_ENUM(ET, MU_TO_NUM(ET, e1) | MU_TO_NUM(ET, e2)); \
- } \
- constexpr ET operator~(ET e) { return MU_TO_ENUM(ET, ~(MU_TO_NUM(ET, e))); } \
- constexpr bool any_of(ET e) { return MU_TO_NUM(ET, e) != 0; } \
- constexpr bool none_of(ET e) { return MU_TO_NUM(ET, e) == 0; } \
- constexpr bool one_of(ET e1, ET e2) { return (e1 & e2) == e2; } \
- constexpr ET& operator&=(ET& e1, ET e2) { return e1 = e1 & e2; } \
- constexpr ET& operator|=(ET& e1, ET e2) { return e1 = e1 | e2; } \
- static_assert(1==1) // require a semicolon
+template<class Enum> // like C++23 std::to_underlying
+constexpr std::underlying_type_t<Enum> to_ut( Enum e ) noexcept {
+ return static_cast<std::underlying_type_t<Enum>>(e);
+}
+
+// C++20 way to make enum-class bitmaps usable
+
+template<typename Enum>
+requires std::is_enum_v<Enum>
+struct enable_bitops {
+ static constexpr bool enable = false;
+};
+// add a concept EnumBitmap which work for enum-classes that specialize enable_bitmups
+template<typename Enum> concept EnumBitmap = enable_bitops<Enum>::enable;
+
+template<EnumBitmap Enum> constexpr Enum operator|(Enum e1, Enum e2) {
+ return static_cast<Enum>(to_ut(e1) | to_ut(e2));
+}
+template<EnumBitmap Enum> constexpr Enum operator|=(Enum& e1, Enum e2) {
+ return e1 = static_cast<Enum>(e1 | e2);
+}
+template<EnumBitmap Enum> constexpr Enum operator&=(Enum& e1, Enum e2) {
+ return e1 = static_cast<Enum>(e1 & e2);
+}
+template<EnumBitmap Enum> constexpr Enum operator&(Enum e1, Enum e2) {
+ return static_cast<Enum>(to_ut(e1) & to_ut(e2));
+}
+template<EnumBitmap Enum> constexpr Enum operator~(Enum e) {
+ return static_cast<Enum>(~to_ut(e));
+}
+template<EnumBitmap Enum> constexpr bool any_of(Enum e) {
+ return to_ut(e) != 0;
+}
+template<EnumBitmap Enum> constexpr bool none_of(Enum e) {
+ return to_ut(e) == 0;
+}
+
+#define MU_ENABLE_BITOPS(Enum) \
+ template<> \c
+ struct enable_bitops<Enum> { \
+ static constexpr bool enable = true; \
+ }
} // namespace Mu
diff --git a/lib/utils/tests/test-utils.cc b/lib/utils/tests/test-utils.cc
index 195f996..2e5aa47 100644
--- a/lib/utils/tests/test-utils.cc
+++ b/lib/utils/tests/test-utils.cc
@@ -260,12 +260,16 @@ test_join()
}
+namespace Mu {
enum struct Bits { None = 0, Bit1 = 1 << 0, Bit2 = 1 << 1 };
MU_ENABLE_BITOPS(Bits);
+}
static void
test_define_bitmap()
{
+ using namespace Mu;
+
g_assert_cmpuint((guint)Bits::None, ==, (guint)0);
g_assert_cmpuint((guint)Bits::Bit1, ==, (guint)1);
g_assert_cmpuint((guint)Bits::Bit2, ==, (guint)2);