mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 23:21:54 +00:00 
			
		
		
		
	dry
This commit is contained in:
		| @@ -180,6 +180,13 @@ template<typename T> class FixedVector { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   // Helper to reset pointers after cleanup | ||||||
|  |   void reset_() { | ||||||
|  |     data_ = nullptr; | ||||||
|  |     capacity_ = 0; | ||||||
|  |     size_ = 0; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  public: |  public: | ||||||
|   FixedVector() = default; |   FixedVector() = default; | ||||||
|  |  | ||||||
| @@ -187,25 +194,19 @@ template<typename T> class FixedVector { | |||||||
|  |  | ||||||
|   // Enable move semantics for use in containers |   // Enable move semantics for use in containers | ||||||
|   FixedVector(FixedVector &&other) noexcept : data_(other.data_), size_(other.size_), capacity_(other.capacity_) { |   FixedVector(FixedVector &&other) noexcept : data_(other.data_), size_(other.size_), capacity_(other.capacity_) { | ||||||
|     other.data_ = nullptr; |     other.reset_(); | ||||||
|     other.size_ = 0; |  | ||||||
|     other.capacity_ = 0; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   FixedVector &operator=(FixedVector &&other) noexcept { |   FixedVector &operator=(FixedVector &&other) noexcept { | ||||||
|     if (this != &other) { |     if (this != &other) { | ||||||
|       // Delete our current data |       // Delete our current data | ||||||
|       if (data_ != nullptr) { |       cleanup_(); | ||||||
|         delete[] data_; |  | ||||||
|       } |  | ||||||
|       // Take ownership of other's data |       // Take ownership of other's data | ||||||
|       data_ = other.data_; |       data_ = other.data_; | ||||||
|       size_ = other.size_; |       size_ = other.size_; | ||||||
|       capacity_ = other.capacity_; |       capacity_ = other.capacity_; | ||||||
|       // Leave other in valid empty state |       // Leave other in valid empty state | ||||||
|       other.data_ = nullptr; |       other.reset_(); | ||||||
|       other.size_ = 0; |  | ||||||
|       other.capacity_ = 0; |  | ||||||
|     } |     } | ||||||
|     return *this; |     return *this; | ||||||
|   } |   } | ||||||
| @@ -213,9 +214,7 @@ template<typename T> class FixedVector { | |||||||
|   // Allocate capacity - can be called multiple times to reinit |   // Allocate capacity - can be called multiple times to reinit | ||||||
|   void init(size_t n) { |   void init(size_t n) { | ||||||
|     cleanup_(); |     cleanup_(); | ||||||
|     data_ = nullptr; |     reset_(); | ||||||
|     capacity_ = 0; |  | ||||||
|     size_ = 0; |  | ||||||
|     if (n > 0) { |     if (n > 0) { | ||||||
|       // Allocate raw memory without calling constructors |       // Allocate raw memory without calling constructors | ||||||
|       data_ = static_cast<T *>(::operator new(n * sizeof(T))); |       data_ = static_cast<T *>(::operator new(n * sizeof(T))); | ||||||
| @@ -229,9 +228,7 @@ template<typename T> class FixedVector { | |||||||
|   // Shrink capacity to fit current size (frees all memory) |   // Shrink capacity to fit current size (frees all memory) | ||||||
|   void shrink_to_fit() { |   void shrink_to_fit() { | ||||||
|     cleanup_(); |     cleanup_(); | ||||||
|     data_ = nullptr; |     reset_(); | ||||||
|     capacity_ = 0; |  | ||||||
|     size_ = 0; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /// Add element without bounds checking |   /// Add element without bounds checking | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user