Utility: enum helpers library
All checks were successful
continuous-integration/drone/push Build is passing
gitea/skullc-peripherals/pipeline/head This commit looks good

This commit is contained in:
Erki 2022-07-15 14:15:04 +03:00
parent 718b6705fd
commit 3aca35788b
4 changed files with 63 additions and 0 deletions

View File

@ -28,6 +28,7 @@ add_executable(tests
function.cpp
assert_ndebug.cpp
assert.cpp
enum_helpers.cpp
)
target_link_libraries(tests

41
Tests/enum_helpers.cpp Normal file
View File

@ -0,0 +1,41 @@
//
// Created by erki on 15.07.22.
//
#include <catch2/catch.hpp>
#include "utility_enum_helpers.hpp"
namespace
{
enum class TestEnum : unsigned
{
FLAG_0 = 1,
FLAG_1 = 2,
FLAG_2 = 4
};
SKULLC_ENUM_DECLARE_BITFLAG_OPERATORS(TestEnum)
}// namespace
TEST_CASE("OR operator works as expected.", "[utility],[enum_helpers]")
{
const TestEnum a = TestEnum::FLAG_0;
const TestEnum b = TestEnum::FLAG_1;
const TestEnum sum = a | b;
CHECK(unsigned(sum) == 3u);
}
TEST_CASE("AND operator works as expected.", "[utility],[enum_helpers]")
{
const TestEnum a = TestEnum::FLAG_0 | TestEnum::FLAG_1;
const TestEnum masked_1 = a & TestEnum::FLAG_0;
CHECK(masked_1 == TestEnum::FLAG_0);
const TestEnum masked_2 = a & TestEnum::FLAG_2;
CHECK(masked_2 != TestEnum::FLAG_2);
}

View File

@ -0,0 +1,20 @@
//
// Created by erki on 15.07.22.
//
#ifndef SKULLC_UTILITY_ENUM_HELPERS_HPP_
#define SKULLC_UTILITY_ENUM_HELPERS_HPP_
#define SKULLC_ENUM_DECLARE_BITFLAG_OPERATORS(E) \
inline E operator|(const E& lhs, const E& rhs) \
{ \
using T = std::underlying_type_t<E>; \
return static_cast<E>(static_cast<T>(lhs) | static_cast<T>(rhs)); \
} \
inline E operator&(const E& lhs, const E& rhs) \
{ \
using T = std::underlying_type_t<E>; \
return static_cast<E>(static_cast<T>(lhs) & static_cast<T>(rhs)); \
}
#endif//SKULLC_UTILITY_ENUM_HELPERS_HPP_

View File

@ -71,6 +71,7 @@ struct StaticPointer
SKULLC_ASSERT_DEBUG(initialized_);
return reinterpret_cast<const value_type*>(storage);
}
private:
bool initialized_ = false;
};