mirror of
https://github.com/esphome/esphome.git
synced 2025-10-26 20:53:50 +00:00
Merge branch 'enum_mask_helper' into integration
This commit is contained in:
@@ -127,12 +127,12 @@ constexpr ColorMode COLOR_MODE_LOOKUP[] = {
|
||||
/// Uses lookup table for non-contiguous enum values
|
||||
struct ColorModeBitPolicy {
|
||||
using mask_t = uint16_t; // 10 bits requires uint16_t
|
||||
static constexpr int max_bits = sizeof(COLOR_MODE_LOOKUP) / sizeof(COLOR_MODE_LOOKUP[0]);
|
||||
static constexpr int MAX_BITS = sizeof(COLOR_MODE_LOOKUP) / sizeof(COLOR_MODE_LOOKUP[0]);
|
||||
|
||||
static constexpr unsigned to_bit(ColorMode mode) {
|
||||
// Linear search through lookup table
|
||||
// Compiler optimizes this to efficient code since array is constexpr
|
||||
for (int i = 0; i < max_bits; ++i) {
|
||||
for (int i = 0; i < MAX_BITS; ++i) {
|
||||
if (COLOR_MODE_LOOKUP[i] == mode)
|
||||
return i;
|
||||
}
|
||||
@@ -140,7 +140,7 @@ struct ColorModeBitPolicy {
|
||||
}
|
||||
|
||||
static constexpr ColorMode from_bit(unsigned bit) {
|
||||
return (bit < max_bits) ? COLOR_MODE_LOOKUP[bit] : ColorMode::UNKNOWN;
|
||||
return (bit < MAX_BITS) ? COLOR_MODE_LOOKUP[bit] : ColorMode::UNKNOWN;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ template<typename ValueType, int MaxBits> struct DefaultBitPolicy {
|
||||
using mask_t = typename std::conditional<(MaxBits <= 8), uint8_t,
|
||||
typename std::conditional<(MaxBits <= 16), uint16_t, uint32_t>::type>::type;
|
||||
|
||||
static constexpr int max_bits = MaxBits;
|
||||
static constexpr int MAX_BITS = MaxBits;
|
||||
|
||||
static constexpr unsigned to_bit(ValueType value) { return static_cast<unsigned>(value); }
|
||||
|
||||
@@ -32,7 +32,7 @@ template<typename ValueType, int MaxBits> struct DefaultBitPolicy {
|
||||
///
|
||||
/// BitPolicy requirements:
|
||||
/// - using mask_t = <uint8_t|uint16_t|uint32_t> // Bitmask storage type
|
||||
/// - static constexpr int max_bits // Maximum number of bits
|
||||
/// - static constexpr int MAX_BITS // Maximum number of bits
|
||||
/// - static constexpr unsigned to_bit(ValueType) // Convert value to bit position
|
||||
/// - static constexpr ValueType from_bit(unsigned) // Convert bit position to value
|
||||
///
|
||||
@@ -155,10 +155,10 @@ template<typename ValueType, typename BitPolicy = DefaultBitPolicy<ValueType, 16
|
||||
}
|
||||
|
||||
/// Find the next set bit in a bitmask starting from a given position
|
||||
/// Returns the bit position, or max_bits if no more bits are set
|
||||
/// Returns the bit position, or MAX_BITS if no more bits are set
|
||||
static constexpr int find_next_set_bit(bitmask_t mask, int start_bit) {
|
||||
int bit = start_bit;
|
||||
while (bit < BitPolicy::max_bits && !(mask & (static_cast<bitmask_t>(1) << bit))) {
|
||||
while (bit < BitPolicy::MAX_BITS && !(mask & (static_cast<bitmask_t>(1) << bit))) {
|
||||
++bit;
|
||||
}
|
||||
return bit;
|
||||
|
||||
Reference in New Issue
Block a user