From ad609e178d9a04367b0f9b0139ade375ed708210 Mon Sep 17 00:00:00 2001 From: Miki Rozloznik Date: Tue, 26 Nov 2024 11:06:20 +0100 Subject: [PATCH] Remove bitmaskToValue --- extension/freemarker/Bitmask.h.ftl | 10 +---- .../cpp17/CppExpressionFormattingPolicy.java | 11 +++++- runtime/ClangTidySuppressions.txt | 2 +- runtime/src/zserio/Bitmasks.h | 25 ------------ runtime/test/zserio/BitmasksTest.cpp | 38 +++++++++++++------ 5 files changed, 39 insertions(+), 47 deletions(-) diff --git a/extension/freemarker/Bitmask.h.ftl b/extension/freemarker/Bitmask.h.ftl index bd873a4..6b90380 100644 --- a/extension/freemarker/Bitmask.h.ftl +++ b/extension/freemarker/Bitmask.h.ftl @@ -59,7 +59,7 @@ public: /** Default copy constructor. */ ${name}(const ${name}&) = default; - + /** Default assignment operator. */ ${name}& operator=(const ${name}&) = default; @@ -285,14 +285,6 @@ inline ${name} operator^=(${name}& lhs, const ${name}& rhs) return lhs; } <@namespace_end package.path/> -<@namespace_begin ["zserio", "detail"]/> - -template <> -struct bitmask_type<${fullName}::Values> -{ - using type = ${fullName}; -}; -<@namespace_end ["zserio", "detail"]/> <@namespace_begin ["std"]/> template <> diff --git a/extension/src/zserio/extension/cpp17/CppExpressionFormattingPolicy.java b/extension/src/zserio/extension/cpp17/CppExpressionFormattingPolicy.java index 760ed9a..34b484f 100644 --- a/extension/src/zserio/extension/cpp17/CppExpressionFormattingPolicy.java +++ b/extension/src/zserio/extension/cpp17/CppExpressionFormattingPolicy.java @@ -195,7 +195,16 @@ public UnaryExpressionFormatting getValueOf(Expression expr) throws ZserioExtens } else if (expr.op1().getExprZserioType() instanceof BitmaskType) { - return new UnaryExpressionFormatting("::zserio::bitmaskToValue(", ")"); + if (expr.op1().requiresOwnerContext()) + { + return new UnaryExpressionFormatting("(", ").getValue()"); + } + else + { + final BitmaskType bitmaskType = (BitmaskType)expr.op1().getExprZserioType(); + final CppNativeType bitmaskNativeType = cppNativeMapper.getCppType(bitmaskType); + return new UnaryExpressionFormatting(bitmaskNativeType.getFullName() + "(", ").getValue()"); + } } else { diff --git a/runtime/ClangTidySuppressions.txt b/runtime/ClangTidySuppressions.txt index d5b5e2b..6c49e51 100644 --- a/runtime/ClangTidySuppressions.txt +++ b/runtime/ClangTidySuppressions.txt @@ -118,6 +118,6 @@ cppcoreguidelines-pro-bounds-pointer-arithmetic:test/zserio/SpanTest.cpp:38 bugprone-exception-escape:test/zserio/OptionalTest.cpp:258 bugprone-exception-escape:test/zserio/VariantTest.cpp:35 fuchsia-multiple-inheritance:test/zserio/VariantTest.cpp:13 -google-explicit-constructor:test/zserio/BitmasksTest.cpp:19 +google-explicit-constructor:test/zserio/BitmasksTest.cpp:22 google-explicit-constructor:test/zserio/BuiltInOperatorsTest.cpp:25 google-explicit-constructor:test/zserio/TrackingAllocator.h:86 diff --git a/runtime/src/zserio/Bitmasks.h b/runtime/src/zserio/Bitmasks.h index 64af01d..fa9b491 100644 --- a/runtime/src/zserio/Bitmasks.h +++ b/runtime/src/zserio/Bitmasks.h @@ -14,31 +14,6 @@ namespace zserio namespace detail { -template -struct bitmask_type; - -template -using bitmask_type_t = typename bitmask_type::type; - -} // namespace detail - -template -constexpr std::enable_if_t, typename T::ZserioType> bitmaskToValue(T value) -{ - return value.getValue(); -} - -template -constexpr std::enable_if_t, typename detail::bitmask_type_t::ZserioType> bitmaskToValue( - T value) -{ - return static_cast::ZserioType>( - static_cast::ZserioType::ValueType>(value)); -} - -namespace detail -{ - template std::enable_if_t> validate(T value, std::string_view fieldName) { diff --git a/runtime/test/zserio/BitmasksTest.cpp b/runtime/test/zserio/BitmasksTest.cpp index e6c0e87..a34953f 100644 --- a/runtime/test/zserio/BitmasksTest.cpp +++ b/runtime/test/zserio/BitmasksTest.cpp @@ -1,4 +1,7 @@ #include "gtest/gtest.h" +#include "zserio/BitBuffer.h" +#include "zserio/BitStreamReader.h" +#include "zserio/BitStreamWriter.h" #include "zserio/Bitmasks.h" namespace zserio @@ -7,7 +10,7 @@ namespace zserio class TestBitmask { public: - using ZserioType = UInt8; + using ZserioType = UInt4; enum class Values : ZserioType::ValueType { @@ -38,25 +41,38 @@ class TestBitmask ZserioType m_value; }; -namespace detail +inline constexpr bool operator==(const TestBitmask& lhs, const TestBitmask& rhs) { + return lhs.getValue() == rhs.getValue(); +} -template <> -struct bitmask_type +TEST(BitmasksTest, validate) { - using type = TestBitmask; -}; + TestBitmask bitmask = TestBitmask::Values::READ; + ASSERT_NO_THROW(detail::validate(bitmask, "")); +} -} // namespace detail +TEST(BitmasksTest, bitSizeOf) +{ + TestBitmask bitmask = TestBitmask::Values::READ; + ASSERT_EQ(4, detail::bitSizeOf(bitmask)); +} -TEST(BitmasksTest, bitmaskToValue) +TEST(BitmasksTest, writeRead) { TestBitmask bitmask = TestBitmask::Values::READ; - ASSERT_EQ(0x1, bitmaskToValue(bitmask)); + const BitSize bitSize = detail::bitSizeOf(bitmask); - ASSERT_EQ(0x1, bitmaskToValue(TestBitmask::Values::READ)); + BitBuffer bitBuffer(bitSize); + BitStreamWriter writer(bitBuffer); + detail::write(writer, bitmask); + ASSERT_EQ(bitSize, writer.getBitPosition()); - ASSERT_EQ(0x1, bitmaskToValue(TestBitmask::Values::READ)); + TestBitmask readBitmask(TestBitmask::Values::WRITE); + BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), BitsTag()); + detail::read(reader, readBitmask); + ASSERT_EQ(bitSize, reader.getBitPosition()); + ASSERT_EQ(readBitmask, bitmask); } } // namespace zserio