This commit is contained in:
J. Nick Koston 2025-07-05 22:58:27 -05:00
parent a45743c2b7
commit e2e35bf965
No known key found for this signature in database
2 changed files with 18 additions and 24 deletions

View File

@ -31,19 +31,13 @@ static const char *const TAG = "component";
// This is safe because ESPHome is single-threaded during initialization // This is safe because ESPHome is single-threaded during initialization
namespace { namespace {
// Error messages for failed components // Error messages for failed components
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::unique_ptr<std::vector<std::pair<const Component *, const char *>>> component_error_messages; std::unique_ptr<std::vector<std::pair<const Component *, const char *>>> component_error_messages;
// Setup priority overrides - freed after setup completes // Setup priority overrides - freed after setup completes
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::unique_ptr<std::vector<std::pair<const Component *, float>>> setup_priority_overrides; std::unique_ptr<std::vector<std::pair<const Component *, float>>> setup_priority_overrides;
} // namespace } // namespace
static std::unique_ptr<std::vector<std::pair<const Component *, const char *>>> &get_component_error_messages() {
return component_error_messages;
}
static std::unique_ptr<std::vector<std::pair<const Component *, float>>> &get_setup_priority_overrides() {
return setup_priority_overrides;
}
namespace setup_priority { namespace setup_priority {
const float BUS = 1000.0f; const float BUS = 1000.0f;
@ -136,8 +130,8 @@ void Component::call_dump_config() {
if (this->is_failed()) { if (this->is_failed()) {
// Look up error message from global vector // Look up error message from global vector
const char *error_msg = "unspecified"; const char *error_msg = "unspecified";
if (get_component_error_messages()) { if (component_error_messages) {
for (const auto &pair : *get_component_error_messages()) { for (const auto &pair : *component_error_messages) {
if (pair.first == this) { if (pair.first == this) {
error_msg = pair.second; error_msg = pair.second;
break; break;
@ -291,18 +285,18 @@ void Component::status_set_error(const char *message) {
ESP_LOGE(TAG, "Component %s set Error flag: %s", this->get_component_source(), message); ESP_LOGE(TAG, "Component %s set Error flag: %s", this->get_component_source(), message);
if (strcmp(message, "unspecified") != 0) { if (strcmp(message, "unspecified") != 0) {
// Lazy allocate the error messages vector if needed // Lazy allocate the error messages vector if needed
if (!get_component_error_messages()) { if (!component_error_messages) {
get_component_error_messages() = std::make_unique<std::vector<std::pair<const Component *, const char *>>>(); component_error_messages = std::make_unique<std::vector<std::pair<const Component *, const char *>>>();
} }
// Check if this component already has an error message // Check if this component already has an error message
for (auto &pair : *get_component_error_messages()) { for (auto &pair : *component_error_messages) {
if (pair.first == this) { if (pair.first == this) {
pair.second = message; pair.second = message;
return; return;
} }
} }
// Add new error message // Add new error message
get_component_error_messages()->emplace_back(this, message); component_error_messages->emplace_back(this, message);
} }
} }
void Component::status_clear_warning() { void Component::status_clear_warning() {
@ -328,9 +322,9 @@ void Component::status_momentary_error(const std::string &name, uint32_t length)
void Component::dump_config() {} void Component::dump_config() {}
float Component::get_actual_setup_priority() const { float Component::get_actual_setup_priority() const {
// Check if there's an override in the global vector // Check if there's an override in the global vector
if (get_setup_priority_overrides()) { if (setup_priority_overrides) {
// Linear search is fine for small n (typically < 5 overrides) // Linear search is fine for small n (typically < 5 overrides)
for (const auto &pair : *get_setup_priority_overrides()) { for (const auto &pair : *setup_priority_overrides) {
if (pair.first == this) { if (pair.first == this) {
return pair.second; return pair.second;
} }
@ -340,14 +334,14 @@ float Component::get_actual_setup_priority() const {
} }
void Component::set_setup_priority(float priority) { void Component::set_setup_priority(float priority) {
// Lazy allocate the vector if needed // Lazy allocate the vector if needed
if (!get_setup_priority_overrides()) { if (!setup_priority_overrides) {
get_setup_priority_overrides() = std::make_unique<std::vector<std::pair<const Component *, float>>>(); setup_priority_overrides = std::make_unique<std::vector<std::pair<const Component *, float>>>();
// Reserve some space to avoid reallocations (most configs have < 10 overrides) // Reserve some space to avoid reallocations (most configs have < 10 overrides)
get_setup_priority_overrides()->reserve(10); setup_priority_overrides->reserve(10);
} }
// Check if this component already has an override // Check if this component already has an override
for (auto &pair : *get_setup_priority_overrides()) { for (auto &pair : *setup_priority_overrides) {
if (pair.first == this) { if (pair.first == this) {
pair.second = priority; pair.second = priority;
return; return;
@ -355,7 +349,7 @@ void Component::set_setup_priority(float priority) {
} }
// Add new override // Add new override
get_setup_priority_overrides()->emplace_back(this, priority); setup_priority_overrides->emplace_back(this, priority);
} }
bool Component::has_overridden_loop() const { bool Component::has_overridden_loop() const {
@ -420,7 +414,7 @@ WarnIfComponentBlockingGuard::~WarnIfComponentBlockingGuard() {}
void clear_setup_priority_overrides() { void clear_setup_priority_overrides() {
// Free the setup priority map completely // Free the setup priority map completely
get_setup_priority_overrides().reset(); setup_priority_overrides.reset();
} }
} // namespace esphome } // namespace esphome

View File

@ -490,7 +490,7 @@ std::string base64_encode(const uint8_t *buf, size_t buf_len) {
char_array_4[3] = char_array_3[2] & 0x3f; char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; (i < 4); i++) for (i = 0; (i < 4); i++)
ret += BASE64_CHARS[char_array_4[i]]; ret += BASE64_CHARS[static_cast<uint8_t>(char_array_4[i])];
i = 0; i = 0;
} }
} }
@ -505,7 +505,7 @@ std::string base64_encode(const uint8_t *buf, size_t buf_len) {
char_array_4[3] = char_array_3[2] & 0x3f; char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++) for (j = 0; (j < i + 1); j++)
ret += BASE64_CHARS[char_array_4[j]]; ret += BASE64_CHARS[static_cast<uint8_t>(char_array_4[j])];
while ((i++ < 3)) while ((i++ < 3))
ret += '='; ret += '=';