diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 117a2ad..53eedd7 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(tests function.cpp assert_ndebug.cpp assert.cpp + enum_helpers.cpp ) target_link_libraries(tests diff --git a/Tests/enum_helpers.cpp b/Tests/enum_helpers.cpp new file mode 100644 index 0000000..1d8df14 --- /dev/null +++ b/Tests/enum_helpers.cpp @@ -0,0 +1,41 @@ +// +// Created by erki on 15.07.22. +// + +#include + +#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); +} diff --git a/Utility/Inc/utility_enum_helpers.hpp b/Utility/Inc/utility_enum_helpers.hpp new file mode 100644 index 0000000..7ee6d8f --- /dev/null +++ b/Utility/Inc/utility_enum_helpers.hpp @@ -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; \ + return static_cast(static_cast(lhs) | static_cast(rhs)); \ + } \ + inline E operator&(const E& lhs, const E& rhs) \ + { \ + using T = std::underlying_type_t; \ + return static_cast(static_cast(lhs) & static_cast(rhs)); \ + } + +#endif//SKULLC_UTILITY_ENUM_HELPERS_HPP_ diff --git a/Utility/Inc/utility_staticpointer.hpp b/Utility/Inc/utility_staticpointer.hpp index 0715be6..4556faf 100644 --- a/Utility/Inc/utility_staticpointer.hpp +++ b/Utility/Inc/utility_staticpointer.hpp @@ -71,6 +71,7 @@ struct StaticPointer SKULLC_ASSERT_DEBUG(initialized_); return reinterpret_cast(storage); } + private: bool initialized_ = false; };