Utility: enum helpers library
This commit is contained in:
parent
718b6705fd
commit
3aca35788b
@ -28,6 +28,7 @@ add_executable(tests
|
|||||||
function.cpp
|
function.cpp
|
||||||
assert_ndebug.cpp
|
assert_ndebug.cpp
|
||||||
assert.cpp
|
assert.cpp
|
||||||
|
enum_helpers.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(tests
|
target_link_libraries(tests
|
||||||
|
|||||||
41
Tests/enum_helpers.cpp
Normal file
41
Tests/enum_helpers.cpp
Normal 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);
|
||||||
|
}
|
||||||
20
Utility/Inc/utility_enum_helpers.hpp
Normal file
20
Utility/Inc/utility_enum_helpers.hpp
Normal 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_
|
||||||
@ -71,6 +71,7 @@ struct StaticPointer
|
|||||||
SKULLC_ASSERT_DEBUG(initialized_);
|
SKULLC_ASSERT_DEBUG(initialized_);
|
||||||
return reinterpret_cast<const value_type*>(storage);
|
return reinterpret_cast<const value_type*>(storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool initialized_ = false;
|
bool initialized_ = false;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user