From de10d781259fb73b605fed83d5c14812997234b8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 13 Oct 2025 14:10:41 -1000 Subject: [PATCH] dry --- esphome/core/helpers.h | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index a3c0447a8d..e838c82f3e 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -180,6 +180,13 @@ template class FixedVector { } } + // Helper to reset pointers after cleanup + void reset_() { + data_ = nullptr; + capacity_ = 0; + size_ = 0; + } + public: FixedVector() = default; @@ -187,25 +194,19 @@ template class FixedVector { // Enable move semantics for use in containers FixedVector(FixedVector &&other) noexcept : data_(other.data_), size_(other.size_), capacity_(other.capacity_) { - other.data_ = nullptr; - other.size_ = 0; - other.capacity_ = 0; + other.reset_(); } FixedVector &operator=(FixedVector &&other) noexcept { if (this != &other) { // Delete our current data - if (data_ != nullptr) { - delete[] data_; - } + cleanup_(); // Take ownership of other's data data_ = other.data_; size_ = other.size_; capacity_ = other.capacity_; // Leave other in valid empty state - other.data_ = nullptr; - other.size_ = 0; - other.capacity_ = 0; + other.reset_(); } return *this; } @@ -213,9 +214,7 @@ template class FixedVector { // Allocate capacity - can be called multiple times to reinit void init(size_t n) { cleanup_(); - data_ = nullptr; - capacity_ = 0; - size_ = 0; + reset_(); if (n > 0) { // Allocate raw memory without calling constructors data_ = static_cast(::operator new(n * sizeof(T))); @@ -229,9 +228,7 @@ template class FixedVector { // Shrink capacity to fit current size (frees all memory) void shrink_to_fit() { cleanup_(); - data_ = nullptr; - capacity_ = 0; - size_ = 0; + reset_(); } /// Add element without bounds checking