Compare commits

..

2 Commits

Author SHA1 Message Date
Erki
3aca35788b Utility: enum helpers library
All checks were successful
continuous-integration/drone/push Build is passing
gitea/skullc-peripherals/pipeline/head This commit looks good
2022-07-15 14:15:04 +03:00
Erki
718b6705fd Utility: replace standard library asserts with SKULLC ones. 2022-07-15 13:54:31 +03:00
5 changed files with 67 additions and 2 deletions

View File

@ -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
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

@ -8,6 +8,8 @@
#ifndef SKULLC_UTILITY_FUNCTION_HPP_ #ifndef SKULLC_UTILITY_FUNCTION_HPP_
#define SKULLC_UTILITY_FUNCTION_HPP_ #define SKULLC_UTILITY_FUNCTION_HPP_
#include <utility_assert.hpp>
namespace Utility namespace Utility
{ {
@ -49,7 +51,7 @@ public:
explicit Function(signature callable) explicit Function(signature callable)
: callable_(callable) : callable_(callable)
{ {
assert(callable_); SKULLC_ASSERT_DEBUG(callable_);
} }
~Function() override ~Function() override
@ -81,7 +83,7 @@ public:
explicit FunctionOwned(source& src, signature callable) explicit FunctionOwned(source& src, signature callable)
: src_(&src), callable_(callable) : src_(&src), callable_(callable)
{ {
assert(callable_); SKULLC_ASSERT_DEBUG(callable_);
} }
~FunctionOwned() override ~FunctionOwned() override

View File

@ -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;
}; };