1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 08:41:59 +00:00

avoid misuse

This commit is contained in:
J. Nick Koston
2026-01-29 18:23:25 -06:00
parent 2b1783ce61
commit 5fbd9d5b14
2 changed files with 9 additions and 7 deletions

View File

@@ -114,7 +114,7 @@ SerializationBuffer<> JsonBuilder::serialize() {
}
// Needs heap allocation - reallocate and serialize again with exact size
result.reallocate_heap(size);
result.reallocate_heap_(size);
serializeJson(doc_, result.data_writable(), size + 1);
return result;
}

View File

@@ -89,9 +89,15 @@ template<size_t STACK_SIZE = 768> class SerializationBuffer {
/// Set actual size after serialization (must not exceed allocated size)
void set_size(size_t size) { size_ = size; }
/// Implicit conversion to std::string for backward compatibility
operator std::string() const { return std::string(buffer_, size_); } // NOLINT(google-explicit-constructor)
private:
friend class JsonBuilder; ///< Allows JsonBuilder::serialize() to call reallocate_heap_()
/// Reallocate to heap buffer with new size (for when stack buffer is too small)
/// This invalidates any previous buffer content
void reallocate_heap(size_t size) {
/// This invalidates any previous buffer content. Used by JsonBuilder::serialize().
void reallocate_heap_(size_t size) {
delete[] heap_buffer_;
heap_buffer_ = new char[size + 1];
buffer_ = heap_buffer_;
@@ -99,10 +105,6 @@ template<size_t STACK_SIZE = 768> class SerializationBuffer {
buffer_[0] = '\0';
}
/// Implicit conversion to std::string for backward compatibility
operator std::string() const { return std::string(buffer_, size_); } // NOLINT(google-explicit-constructor)
private:
char stack_buffer_[STACK_SIZE];
char *heap_buffer_{nullptr};
char *buffer_;