mirror of
https://github.com/esphome/esphome.git
synced 2026-11-18 02:55:59 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cd8b913aa4 | ||
|
|
da9a6cc6c0 | ||
|
|
95ec16c7ce | ||
|
|
5593d8295a | ||
|
|
0a164623d6 |
No files matched your search
+37
-9
@@ -4,8 +4,10 @@
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <new>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "esphome/core/hal.h" // For PROGMEM definition
|
||||
|
||||
@@ -76,8 +78,8 @@ template<size_t N> struct FixedString {
|
||||
///
|
||||
/// Example:
|
||||
/// PROGMEM_STRING_TABLE(MyStrings, "foo", "bar", "baz");
|
||||
/// ProgmemStr str = MyStrings::get_progmem_str(idx, MyStrings::LAST_INDEX); // For ArduinoJson
|
||||
/// const LogString *log_str = MyStrings::get_log_str(idx, MyStrings::LAST_INDEX); // For logging
|
||||
/// ProgmemStr str = MyStrings::get_progmem_str(idx); // For ArduinoJson
|
||||
/// const LogString *log_str = MyStrings::get_log_str(idx); // For logging
|
||||
///
|
||||
template<FixedString... Strs> struct ProgmemStringTable {
|
||||
static constexpr size_t COUNT = sizeof...(Strs);
|
||||
@@ -122,12 +124,33 @@ inline const char *progmem_skip_strings(const char *blob, uint8_t idx) {
|
||||
return blob;
|
||||
}
|
||||
|
||||
namespace progmem_detail {
|
||||
/// Widen an enum or integer to a table index of type D; negative and out-of-range values become huge ones, which
|
||||
/// fall back. D is a parameter only so the host tests can check the 64-to-32-bit clamp.
|
||||
template<typename D = size_t, typename T> constexpr D table_index(T value) {
|
||||
if constexpr (std::is_enum_v<T>) {
|
||||
return table_index<D>(static_cast<std::underlying_type_t<T>>(value));
|
||||
} else {
|
||||
static_assert(std::is_integral_v<T> && !std::is_same_v<T, bool>, "index must be an integer or an enum");
|
||||
if constexpr (sizeof(T) > sizeof(D)) {
|
||||
const auto v = static_cast<std::make_unsigned_t<T>>(value);
|
||||
return v > std::numeric_limits<D>::max() ? std::numeric_limits<D>::max() : static_cast<D>(v);
|
||||
} else if constexpr (std::is_signed_v<T>) {
|
||||
return static_cast<D>(static_cast<std::make_signed_t<D>>(value)); // sign-extend first
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace progmem_detail
|
||||
|
||||
// Forward declaration for LogString (defined in log.h)
|
||||
struct LogString;
|
||||
|
||||
/// Instantiate a ProgmemStringTable with PROGMEM storage.
|
||||
/// Creates: Name::get_progmem_str(idx, fallback), Name::get_log_str(idx, fallback)
|
||||
/// If idx >= COUNT, returns string at fallback. Use LAST_INDEX for common patterns.
|
||||
/// idx and fallback may be any integer or enum; an idx outside the table returns the string at fallback,
|
||||
/// LAST_INDEX by default.
|
||||
#define PROGMEM_STRING_TABLE(Name, ...) \
|
||||
struct Name { \
|
||||
using Table = ::esphome::ProgmemStringTable<__VA_ARGS__>; \
|
||||
@@ -137,7 +160,7 @@ struct LogString;
|
||||
static constexpr size_t MAX_LENGTH = Table::MAX_LENGTH; \
|
||||
static constexpr auto BLOB ESPHOME_PROGMEM_STRING_TABLE_STORAGE = Table::make_blob(); \
|
||||
static constexpr auto OFFSETS ESPHOME_PROGMEM_STRING_TABLE_STORAGE = Table::make_offsets(); \
|
||||
static const char *get_(uint8_t idx, uint8_t fallback) { \
|
||||
static const char *get_(size_t idx, uint8_t fallback) { \
|
||||
if (idx >= COUNT) \
|
||||
idx = fallback; \
|
||||
/* std::launder is used here to prevent the inter-procedural analysis that */ \
|
||||
@@ -145,13 +168,18 @@ struct LogString;
|
||||
if constexpr (Table::INDEXED) \
|
||||
return std::launder(&BLOB[::esphome::progmem_read_byte(&OFFSETS[idx])]); \
|
||||
else \
|
||||
return std::launder(::esphome::progmem_skip_strings(BLOB.data(), idx)); \
|
||||
return std::launder(::esphome::progmem_skip_strings(BLOB.data(), static_cast<uint8_t>(idx))); \
|
||||
} \
|
||||
static ::ProgmemStr get_progmem_str(uint8_t idx, uint8_t fallback) { \
|
||||
return reinterpret_cast<::ProgmemStr>(get_(idx, fallback)); \
|
||||
template<typename T, typename F = uint8_t> static ::ProgmemStr get_progmem_str(T idx, F fallback = LAST_INDEX) { \
|
||||
return reinterpret_cast<::ProgmemStr>( \
|
||||
get_(::esphome::progmem_detail::table_index(idx), \
|
||||
static_cast<uint8_t>(::esphome::progmem_detail::table_index(fallback)))); \
|
||||
} \
|
||||
static const ::esphome::LogString *get_log_str(uint8_t idx, uint8_t fallback) { \
|
||||
return reinterpret_cast<const ::esphome::LogString *>(get_(idx, fallback)); \
|
||||
template<typename T, typename F = uint8_t> \
|
||||
static const ::esphome::LogString *get_log_str(T idx, F fallback = LAST_INDEX) { \
|
||||
return reinterpret_cast<const ::esphome::LogString *>( \
|
||||
get_(::esphome::progmem_detail::table_index(idx), \
|
||||
static_cast<uint8_t>(::esphome::progmem_detail::table_index(fallback)))); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
@@ -70,4 +70,33 @@ TEST(ProgmemStringTable, StrstrFindsTableEntryInRamString) {
|
||||
EXPECT_EQ(ESPHOME_strstr_P(line, cool), nullptr);
|
||||
}
|
||||
|
||||
enum class SmallMode : uint8_t { SMALL_MODE_OFF, SMALL_MODE_HEAT, SMALL_MODE_COOL };
|
||||
enum class WideMode : uint16_t { WIDE_MODE_OFF, WIDE_MODE_HEAT, WIDE_MODE_COOL, WIDE_MODE_BIG = 0x101 };
|
||||
enum PlainMode { PLAIN_OFF, PLAIN_HEAT };
|
||||
|
||||
TEST(ProgmemStringTable, TakesEnumsAndDefaultsToLastEntry) {
|
||||
EXPECT_STREQ(LOG_STR_ARG(SmallStrings::get_log_str(SmallMode::SMALL_MODE_COOL)), "COOL");
|
||||
EXPECT_STREQ(LOG_STR_ARG(SmallStrings::get_log_str(PLAIN_HEAT)), "HEAT");
|
||||
EXPECT_STREQ(LOG_STR_ARG(SmallStrings::get_log_str(static_cast<SmallMode>(9))), "UNKNOWN");
|
||||
EXPECT_STREQ(LOG_STR_ARG(SmallStrings::get_log_str(static_cast<SmallMode>(9), SmallMode::SMALL_MODE_OFF)), "OFF");
|
||||
}
|
||||
|
||||
TEST(ProgmemStringTable, WideAndNegativeValuesFallBack) {
|
||||
EXPECT_STREQ(LOG_STR_ARG(SmallStrings::get_log_str(WideMode::WIDE_MODE_HEAT)), "HEAT");
|
||||
EXPECT_STREQ(LOG_STR_ARG(SmallStrings::get_log_str(WideMode::WIDE_MODE_BIG)), "UNKNOWN");
|
||||
EXPECT_STREQ(LOG_STR_ARG(SmallStrings::get_log_str(-1)), "UNKNOWN");
|
||||
EXPECT_STREQ(LOG_STR_ARG(SmallStrings::get_log_str(static_cast<int8_t>(-2))), "UNKNOWN");
|
||||
EXPECT_STREQ(LOG_STR_ARG(SmallStrings::get_log_str(size_t{2})), "COOL");
|
||||
EXPECT_STREQ(reinterpret_cast<const char *>(SmallStrings::get_progmem_str(uint32_t{0x10002})), "UNKNOWN");
|
||||
}
|
||||
|
||||
static_assert(progmem_detail::table_index(int8_t{-128}) > UINT8_MAX); // not 128, a valid index in a big table
|
||||
static_assert(progmem_detail::table_index(int16_t{-1}) > UINT8_MAX);
|
||||
static_assert(progmem_detail::table_index(int64_t{-1}) > UINT8_MAX);
|
||||
static_assert(progmem_detail::table_index(uint64_t{1}) == 1);
|
||||
// A 32-bit index type: 0x1'0000'0001 must clamp, not truncate to the valid index 1
|
||||
static_assert(progmem_detail::table_index<uint32_t>(uint64_t{0x1'0000'0001}) == UINT32_MAX);
|
||||
static_assert(progmem_detail::table_index<uint32_t>(int64_t{-1}) == UINT32_MAX);
|
||||
static_assert(progmem_detail::table_index<uint32_t>(int16_t{-2}) > UINT8_MAX);
|
||||
|
||||
} // namespace esphome::core::testing
|
||||
Reference in new issue
Block a user