Loading...
Searching...
No Matches
bit_masking.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <concepts>
5
7
8namespace terra::util {
9
10/// @concept FlagLike
11/// @brief Concept for types that behave like bitmask flags.
12///
13/// This concept checks if a type `E` is an enum with an unsigned integral underlying type,
14/// has a `NO_FLAG` value equal to 0, and supports bitwise OR (`|`) and AND (`&`) operations.
15/// @tparam E The enum type to check.
16template < typename E >
17concept FlagLike = std::is_enum_v< E > && std::unsigned_integral< std::underlying_type_t< E > > &&
18 ( static_cast< std::underlying_type_t< E > >( E::NO_FLAG ) == 0 );
19
20template < FlagLike E >
21KOKKOS_INLINE_FUNCTION constexpr E operator|( E a, E b )
22{
23 using T = std::underlying_type_t< E >;
24 return static_cast< E >( static_cast< T >( a ) | static_cast< T >( b ) );
25}
26
27template < FlagLike E >
28KOKKOS_INLINE_FUNCTION constexpr E operator&( E a, E b )
29{
30 using T = std::underlying_type_t< E >;
31 return static_cast< E >( static_cast< T >( a ) & static_cast< T >( b ) );
32}
33
34/// @brief Checks if a bitmask value contains a specific flag.
35///
36/// This function checks if the bitmask value `mask_value` has the flag `flag` set.
37/// If `flag` is `E::NO_FLAG`, it checks if `mask_value` is exactly `E::NO_FLAG`.
38/// @tparam E The enum type representing the bitmask.
39/// @param mask_value The bitmask value to check.
40/// @param flag The flag to check for in `mask_value`.
41/// @return `true` if `mask_value` contains `flag` or whether `mask_value == flag` if `flag == E::NO_FLAG`, otherwise `false`.
42template < FlagLike E >
43KOKKOS_INLINE_FUNCTION constexpr bool has_flag( E mask_value, E flag ) noexcept
44{
45 using U = std::underlying_type_t< E >;
46
47 if ( flag == E::NO_FLAG )
48 {
49 return mask_value == E::NO_FLAG;
50 }
51
52 return static_cast< U >( mask_value & flag ) == static_cast< U >( flag );
53}
54
55} // namespace terra::util
Concept for types that behave like bitmask flags.
Definition bit_masking.hpp:17
Definition solver.hpp:9
constexpr E operator&(E a, E b)
Definition bit_masking.hpp:28
constexpr bool has_flag(E mask_value, E flag) noexcept
Checks if a bitmask value contains a specific flag.
Definition bit_masking.hpp:43
constexpr E operator|(E a, E b)
Definition bit_masking.hpp:21