Skip to content

Commit

Permalink
Improve bitmaskToValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed Nov 26, 2024
1 parent fe7ccd9 commit fe6e707
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
8 changes: 8 additions & 0 deletions extension/freemarker/Bitmask.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ 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 @@ -198,7 +198,7 @@ else if (expr.op1().getExprZserioType() instanceof BitmaskType)
final BitmaskType bitmaskType = (BitmaskType)expr.op1().getExprZserioType();
final CppNativeType bitmaskNativeType = cppNativeMapper.getCppType(bitmaskType);
return new UnaryExpressionFormatting(
"::zserio::bitmaskToValue<" + bitmaskNativeType.getFullName() + ">(", ")");
"::zserio::bitmaskToValue(", ")");
}
else
{
Expand Down
1 change: 1 addition & 0 deletions runtime/ClangTidySuppressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +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/BuiltInOperatorsTest.cpp:25
google-explicit-constructor:test/zserio/TrackingAllocator.h:86
18 changes: 15 additions & 3 deletions runtime/src/zserio/Bitmasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,29 @@
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<is_bitmask_v<T> && std::is_enum_v<typename T::Values>, typename T::ZserioType>
bitmaskToValue(typename T::Values rawValue)
constexpr std::enable_if_t<std::is_enum_v<T>, typename detail::bitmask_type_t<T>::ZserioType> bitmaskToValue(
T value)
{
return static_cast<typename T::ZserioType>(static_cast<typename T::ZserioType::ValueType>(rawValue));
return static_cast<typename detail::bitmask_type_t<T>::ZserioType>(
static_cast<typename detail::bitmask_type_t<T>::ZserioType::ValueType>(value));
}

namespace detail
Expand Down
1 change: 1 addition & 0 deletions runtime/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(ZSERIO_CPP17_RUNTIME_TEST_SRCS
zserio/BitBufferTest.cpp
zserio/BitFieldUtilTest.cpp
zserio/BitPositionUtilTest.cpp
zserio/BitmasksTest.cpp
zserio/BitStreamReaderTest.cpp
zserio/BitStreamTest.cpp
zserio/BitStreamWriterTest.cpp
Expand Down
62 changes: 62 additions & 0 deletions runtime/test/zserio/BitmasksTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "gtest/gtest.h"
#include "zserio/Bitmasks.h"

namespace zserio
{

class TestBitmask
{
public:
using ZserioType = UInt8;

enum class Values : ZserioType::ValueType
{
READ = 0x1,
WRITE = 0x2,
READ_WRITE = 0x3
};

constexpr TestBitmask(Values value) noexcept :
m_value(static_cast<ZserioType::ValueType>(value))
{}

constexpr explicit TestBitmask(ZserioType value) noexcept :
m_value(value)
{}

constexpr explicit operator ZserioType() const
{
return m_value;
}

constexpr ZserioType getValue() const
{
return m_value;
}

private:
ZserioType m_value;
};

namespace detail
{

template <>
struct bitmask_type<TestBitmask::Values>
{
using type = TestBitmask;
};

} // namespace detail

TEST(BitmasksTest, bitmaskToValue)
{
TestBitmask bitmask = TestBitmask::Values::READ;
ASSERT_EQ(0x1, bitmaskToValue(bitmask));

ASSERT_EQ(0x1, bitmaskToValue<TestBitmask>(TestBitmask::Values::READ));

ASSERT_EQ(0x1, bitmaskToValue(TestBitmask::Values::READ));
}

} // namespace zserio

0 comments on commit fe6e707

Please sign in to comment.