Skip to content

Commit

Permalink
Remove bitmaskToValue
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Nov 26, 2024
1 parent adf6e2b commit ad609e1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 47 deletions.
10 changes: 1 addition & 9 deletions extension/freemarker/Bitmask.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public:

/** Default copy constructor. */
${name}(const ${name}&) = default;

/** Default assignment operator. */
${name}& operator=(const ${name}&) = default;

Expand Down Expand Up @@ -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 <>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion runtime/ClangTidySuppressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 0 additions & 25 deletions runtime/src/zserio/Bitmasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,6 @@ namespace zserio
namespace detail
{

template <typename T>
struct bitmask_type;

template <typename T>
using bitmask_type_t = typename bitmask_type<T>::type;

} // namespace detail

template <typename T>
constexpr std::enable_if_t<is_bitmask_v<T>, typename T::ZserioType> bitmaskToValue(T value)
{
return value.getValue();
}

template <typename T>
constexpr std::enable_if_t<std::is_enum_v<T>, typename detail::bitmask_type_t<T>::ZserioType> bitmaskToValue(
T value)
{
return static_cast<typename detail::bitmask_type_t<T>::ZserioType>(
static_cast<typename detail::bitmask_type_t<T>::ZserioType::ValueType>(value));
}

namespace detail
{

template <typename T>
std::enable_if_t<is_bitmask_v<T>> validate(T value, std::string_view fieldName)
{
Expand Down
38 changes: 27 additions & 11 deletions runtime/test/zserio/BitmasksTest.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,7 +10,7 @@ namespace zserio
class TestBitmask
{
public:
using ZserioType = UInt8;
using ZserioType = UInt4;

enum class Values : ZserioType::ValueType
{
Expand Down Expand Up @@ -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<TestBitmask::Values>
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>(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

0 comments on commit ad609e1

Please sign in to comment.