From 5a0ab05012428ed7980c0e1b2fb256ff71aa4d0d Mon Sep 17 00:00:00 2001 From: "Mauricio A. Rovira Galvez" <8482308+marovira@users.noreply.github.com> Date: Wed, 26 Jun 2024 17:16:06 -0700 Subject: [PATCH] [brief] Allow enum bitfield to receive assignment of the base type. [detailed] - Mostly for symmetry with the other function. --- include/zeus/enum_bitfield.hpp | 6 +++++ test/enum_bitfield_test.cpp | 42 ++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/include/zeus/enum_bitfield.hpp b/include/zeus/enum_bitfield.hpp index 78b6457..8ce52c0 100644 --- a/include/zeus/enum_bitfield.hpp +++ b/include/zeus/enum_bitfield.hpp @@ -26,6 +26,12 @@ namespace zeus return *this; } + constexpr EnumBitfield& operator=(BaseType field) + { + m_field = field; + return *this; + } + constexpr BaseType bits() const { return m_field; diff --git a/test/enum_bitfield_test.cpp b/test/enum_bitfield_test.cpp index e5ed38f..88ccfcf 100644 --- a/test/enum_bitfield_test.cpp +++ b/test/enum_bitfield_test.cpp @@ -59,20 +59,44 @@ TEST_CASE("[EnumBitfield] - operator=", "[zeus]") { SECTION("Runtime") { - EnumBitfield a; - a = Bits::b; - REQUIRE(a.bits() == 2); + SECTION("From enum") + { + EnumBitfield a; + a = Bits::b; + REQUIRE(a.bits() == 2); + } + + SECTION("From base type") + { + EnumBitfield a; + a = 2; + REQUIRE(a.bits() == 2); + } } SECTION("Compile-time") { - static constexpr auto val = []() { - EnumBitfield a; - a = Bits::b; - return a.bits(); - }(); + SECTION("From enum") + { + static constexpr auto val = []() { + EnumBitfield a; + a = Bits::b; + return a.bits(); + }(); - STATIC_REQUIRE(val == 2); + STATIC_REQUIRE(val == 2); + } + + SECTION("From base type") + { + static constexpr auto val = []() { + EnumBitfield a; + a = 2; + return a.bits(); + }(); + + STATIC_REQUIRE(val == 2); + } } }