Compare commits
2 Commits
0ba9416a57
...
3aca35788b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3aca35788b | ||
|
|
718b6705fd |
@ -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
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_
|
||||
@ -8,6 +8,8 @@
|
||||
#ifndef SKULLC_UTILITY_FUNCTION_HPP_
|
||||
#define SKULLC_UTILITY_FUNCTION_HPP_
|
||||
|
||||
#include <utility_assert.hpp>
|
||||
|
||||
namespace Utility
|
||||
{
|
||||
|
||||
@ -49,7 +51,7 @@ public:
|
||||
explicit Function(signature callable)
|
||||
: callable_(callable)
|
||||
{
|
||||
assert(callable_);
|
||||
SKULLC_ASSERT_DEBUG(callable_);
|
||||
}
|
||||
|
||||
~Function() override
|
||||
@ -81,7 +83,7 @@ public:
|
||||
explicit FunctionOwned(source& src, signature callable)
|
||||
: src_(&src), callable_(callable)
|
||||
{
|
||||
assert(callable_);
|
||||
SKULLC_ASSERT_DEBUG(callable_);
|
||||
}
|
||||
|
||||
~FunctionOwned() override
|
||||
|
||||
@ -71,6 +71,7 @@ struct StaticPointer
|
||||
SKULLC_ASSERT_DEBUG(initialized_);
|
||||
return reinterpret_cast<const value_type*>(storage);
|
||||
}
|
||||
|
||||
private:
|
||||
bool initialized_ = false;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user