diff --git a/esphome/core/application.h b/esphome/core/application.h index c91cba8d19..b7824a254b 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -493,68 +493,68 @@ class Application { std::vector areas_{}; #endif #ifdef USE_BINARY_SENSOR - static_vector binary_sensors_{}; + StaticVector binary_sensors_{}; #endif #ifdef USE_SWITCH - static_vector switches_{}; + StaticVector switches_{}; #endif #ifdef USE_BUTTON - static_vector buttons_{}; + StaticVector buttons_{}; #endif #ifdef USE_EVENT - static_vector events_{}; + StaticVector events_{}; #endif #ifdef USE_SENSOR - static_vector sensors_{}; + StaticVector sensors_{}; #endif #ifdef USE_TEXT_SENSOR - static_vector text_sensors_{}; + StaticVector text_sensors_{}; #endif #ifdef USE_FAN - static_vector fans_{}; + StaticVector fans_{}; #endif #ifdef USE_COVER - static_vector covers_{}; + StaticVector covers_{}; #endif #ifdef USE_CLIMATE - static_vector climates_{}; + StaticVector climates_{}; #endif #ifdef USE_LIGHT - static_vector lights_{}; + StaticVector lights_{}; #endif #ifdef USE_NUMBER - static_vector numbers_{}; + StaticVector numbers_{}; #endif #ifdef USE_DATETIME_DATE - static_vector dates_{}; + StaticVector dates_{}; #endif #ifdef USE_DATETIME_TIME - static_vector times_{}; + StaticVector times_{}; #endif #ifdef USE_DATETIME_DATETIME - static_vector datetimes_{}; + StaticVector datetimes_{}; #endif #ifdef USE_SELECT - static_vector selects_{}; + StaticVector selects_{}; #endif #ifdef USE_TEXT - static_vector texts_{}; + StaticVector texts_{}; #endif #ifdef USE_LOCK - static_vector locks_{}; + StaticVector locks_{}; #endif #ifdef USE_VALVE - static_vector valves_{}; + StaticVector valves_{}; #endif #ifdef USE_MEDIA_PLAYER - static_vector media_players_{}; + StaticVector media_players_{}; #endif #ifdef USE_ALARM_CONTROL_PANEL - static_vector + StaticVector alarm_control_panels_{}; #endif #ifdef USE_UPDATE - static_vector updates_{}; + StaticVector updates_{}; #endif #ifdef USE_SOCKET_SELECT_SUPPORT diff --git a/esphome/core/component_iterator.cpp b/esphome/core/component_iterator.cpp index e012412ab6..668c4a1fda 100644 --- a/esphome/core/component_iterator.cpp +++ b/esphome/core/component_iterator.cpp @@ -17,23 +17,6 @@ void ComponentIterator::begin(bool include_internal) { this->include_internal_ = include_internal; } -template -void ComponentIterator::process_platform_item_(const Container &items, - bool (ComponentIterator::*on_item)(typename Container::value_type)) { - // Since static_vector doesn't have size(), we need to iterate differently - size_t index = 0; - for (auto *item : items) { - if (index++ == this->at_) { - if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) { - this->at_++; - } - return; - } - } - // If we get here, we've reached the end - this->advance_platform_(); -} - void ComponentIterator::advance_platform_() { this->state_ = static_cast(static_cast(this->state_) + 1); this->at_ = 0; diff --git a/esphome/core/component_iterator.h b/esphome/core/component_iterator.h index 6b44990529..fdc30485bc 100644 --- a/esphome/core/component_iterator.h +++ b/esphome/core/component_iterator.h @@ -174,7 +174,17 @@ class ComponentIterator { template void process_platform_item_(const Container &items, - bool (ComponentIterator::*on_item)(typename Container::value_type)); + bool (ComponentIterator::*on_item)(typename Container::value_type)) { + if (this->at_ >= items.size()) { + this->advance_platform_(); + } else { + typename Container::value_type item = items[this->at_]; + if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) { + this->at_++; + } + } + } + void advance_platform_(); }; diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 05e57a267e..b05cc11029 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -95,7 +95,7 @@ template<> constexpr int64_t byteswap(int64_t n) { return __builtin_bswap64(n); ///@{ /// Minimal static vector - saves memory by avoiding std::vector overhead -template class static_vector { +template class StaticVector { public: using value_type = T; using iterator = typename std::array::iterator; @@ -113,6 +113,11 @@ template class static_vector { } } + size_t size() const { return count_; } + + T &operator[](size_t i) { return data_[i]; } + const T &operator[](size_t i) const { return data_[i]; } + // For range-based for loops iterator begin() { return data_.begin(); } iterator end() { return data_.begin() + count_; }