1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-28 13:43:54 +00:00

backmerge

This commit is contained in:
J. Nick Koston
2025-10-22 09:54:47 -10:00
parent a335aa0713
commit bc7cc066a5

View File

@@ -17,17 +17,20 @@ namespace esphome {
/// ///
/// Requirements: /// Requirements:
/// - ValueType must have a bounded discrete range that maps to bit positions /// - ValueType must have a bounded discrete range that maps to bit positions
/// - Specialization must provide value_to_bit() and bit_to_value() static methods /// - For 1:1 mappings (contiguous enums starting at 0), no specialization needed
/// - For custom mappings (like ColorMode), specialize value_to_bit() and/or bit_to_value()
/// - MaxBits must be sufficient to hold all possible values /// - MaxBits must be sufficient to hold all possible values
/// ///
/// Example usage: /// Example usage (1:1 mapping - climate enums):
/// using ClimateModeMask = FiniteSetMask<ClimateMode, 8>; /// // For enums with contiguous values starting at 0, no specialization needed!
/// using ClimateModeMask = FiniteSetMask<ClimateMode, CLIMATE_MODE_AUTO + 1>;
/// ClimateModeMask modes({CLIMATE_MODE_HEAT, CLIMATE_MODE_COOL}); /// ClimateModeMask modes({CLIMATE_MODE_HEAT, CLIMATE_MODE_COOL});
/// if (modes.count(CLIMATE_MODE_HEAT)) { ... } /// if (modes.count(CLIMATE_MODE_HEAT)) { ... }
/// for (auto mode : modes) { ... } // Iterate over set bits /// for (auto mode : modes) { ... } // Iterate over set bits
/// ///
/// For complete usage examples with template specializations, see: /// Example usage (custom mapping - ColorMode):
/// - esphome/components/light/color_mode.h (ColorMode enum example) /// // For non-contiguous enums or custom mappings, specialize value_to_bit() and/or bit_to_value()
/// // See esphome/components/light/color_mode.h for complete example
/// ///
/// Design notes: /// Design notes:
/// - Uses compile-time type selection for optimal size (uint8_t/uint16_t/uint32_t) /// - Uses compile-time type selection for optimal size (uint8_t/uint16_t/uint32_t)
@@ -150,10 +153,11 @@ template<typename ValueType, int MaxBits = 16> class FiniteSetMask {
} }
protected: protected:
// Must be provided by template specialization // Default implementations for 1:1 mapping (enum value = bit position)
// These convert between values and bit positions (0, 1, 2, ...) // For enums with contiguous values starting at 0, these defaults work as-is.
static constexpr int value_to_bit(ValueType value); // If you need custom mapping (like ColorMode), provide specializations.
static ValueType bit_to_value(int bit); // Not constexpr: array indexing with runtime bounds checking static constexpr int value_to_bit(ValueType value) { return static_cast<int>(value); }
static constexpr ValueType bit_to_value(int bit) { return static_cast<ValueType>(bit); }
bitmask_t mask_{0}; bitmask_t mask_{0};
}; };