Go to the documentation of this file.
31 #include <type_traits>
35 #define TOKEN_CONCAT_IMPL(x, y) x ## y
36 #define TOKEN_CONCAT(x, y) TOKEN_CONCAT_IMPL(x, y)
37 #define ANONYMOUS_VARIABLE(prefix) TOKEN_CONCAT(prefix, __COUNTER__)
57 #define SCOPE_GUARD ::bsp::scope_guard::ScopeGuardOnExit() + [&]()
64 #define ON_SCOPE_EXIT auto ANONYMOUS_VARIABLE(SCOPE_EXIT_) = SCOPE_GUARD
66 namespace scope_guard {
77 constexpr
ScopeGuardImpl(F func) : m_func(std::move(func)), m_active(true) { }
83 void release() { this->m_active =
false; }
112 template <
typename To,
typename From>
114 return *
reinterpret_cast<const To*
>(&src);
123 __builtin_unreachable();
133 static_assert(std::is_integral<T>::value,
"Only integral types can be byte swapped");
134 static_assert(
sizeof(T) <=
sizeof(
u64),
"Value cannot be bigger than 64 bit");
136 if constexpr (
sizeof(T) == 1)
return value;
137 else if constexpr (
sizeof(T) == 2)
return __builtin_bswap16(value);
138 else if constexpr (
sizeof(T) == 4)
return __builtin_bswap32(value);
139 else if constexpr (
sizeof(T) == 8)
return __builtin_bswap64(value);
140 else bsp::unreachable();
152 static_assert(std::is_enum<T>::value,
"Cannot get value of non-enum type");
154 return static_cast<typename std::underlying_type<T>::type
>(value);
171 constexpr
operator T() const noexcept {
194 this->m_value = value.m_value;
208 asm volatile(
"" : :
"r,m"(value) :
"memory");
constexpr auto operator=(T value)
Transparently assign value and store it byte swapped.
Definition: utils.hpp:181
ScopeGuardImpl(ScopeGuardImpl &&other) noexcept
Definition: utils.hpp:85
constexpr ByteSwapped(T value)
Definition: utils.hpp:164
void release()
Releases the scope guard and prevents it from being executed when going out of scope.
Definition: utils.hpp:83
constexpr auto operator=(const ByteSwapped &value)
Copy assignment operator.
Definition: utils.hpp:193
uint64_t u64
Definition: types.h:39
#define ALWAYS_INLINE
Definition: attributes.h:34
#define NO_RETURN
Definition: attributes.h:31
#define PACKED
Definition: attributes.h:35
auto enumValue(T value)
Casts a scoped enum type into its underlying value.
Definition: utils.hpp:151
constexpr auto dependent_false_v
Definition: utils.hpp:49
Helper class to store the data of a given type in reverse order.
Definition: utils.hpp:162
To bit_cast(const From &src) noexcept
std::bit_cast implementation for doing reinterpret_cast-style conversion without invoking undefined b...
Definition: utils.hpp:113
ScopeGuardOnExit
Definition: utils.hpp:92
constexpr ScopeGuardImpl< F > operator+(ScopeGuardOnExit, F &&f)
Definition: utils.hpp:98
ALWAYS_INLINE void doNotOptimize(const auto &value)
Confuses the compiler to prevent it from optimizing out a variable. Helpful for debugging.
Definition: utils.hpp:207
~ScopeGuardImpl()
Definition: utils.hpp:78
ScopeGuardImpl & operator=(ScopeGuardImpl &&)=delete
constexpr ByteSwapped()
Definition: utils.hpp:163
Helper type trait for using static_asserts that should always trigger an error when evaluated.
Definition: utils.hpp:46
Commonly used C++ and GNU attributes.
constexpr T byteSwap(T value)
Swaps bytes of input value to convert between big and little endian.
Definition: utils.hpp:132
Definition: cortex.hpp:33
constexpr ScopeGuardImpl(F func)
Definition: utils.hpp:77
Scope Guard implementation.
Definition: utils.hpp:72