diff --git a/esphome/core/bytebuffer.cpp b/esphome/core/bytebuffer.cpp index 5d5323d875..75806fad32 100644 --- a/esphome/core/bytebuffer.cpp +++ b/esphome/core/bytebuffer.cpp @@ -81,12 +81,25 @@ void ByteBuffer::flip() { this->position_ = 0; } +bool ByteBuffer::is_changed() { + bool changed = this->is_changed_; + this->is_changed_ = false; + return changed; +} + +void ByteBuffer::update_used_space_() { + this->is_changed_ = true; + if (this->used_space_ < this->position_) { + this->resize(); + } +} + /// Getters uint8_t ByteBuffer::get_uint8() { assert(this->get_remaining() >= 1); this->position_++; - this->update_(); - return this->data_[this->position_]; + this->update_used_space_(); + return this->data_[this->position_ - 1]; } uint64_t ByteBuffer::get_uint(size_t length) { assert(this->get_remaining() >= length); @@ -104,7 +117,7 @@ uint64_t ByteBuffer::get_uint(size_t length) { value |= this->data_[this->position_++]; } } - this->update_(); + this->update_used_space_(); return value; } @@ -128,7 +141,7 @@ std::vector ByteBuffer::get_vector(size_t length) { assert(this->get_remaining() >= length); auto start = this->data_.begin() + this->position_; this->position_ += length; - this->update_(); + this->update_used_space_(); return {start, start + length}; } void ByteBuffer::get_bytes(uint8_t *value, size_t length) { @@ -137,14 +150,14 @@ void ByteBuffer::get_bytes(uint8_t *value, size_t length) { while (length-- != 0) { *(value + index++) = this->data_[this->position_++]; } - this->update_(); + this->update_used_space_(); } /// Putters void ByteBuffer::put_uint8(uint8_t value) { assert(this->get_remaining() >= 1); this->data_[this->position_++] = value; - this->update_(); + this->update_used_space_(); } void ByteBuffer::put_uint(uint64_t value, size_t length) { @@ -162,7 +175,7 @@ void ByteBuffer::put_uint(uint64_t value, size_t length) { value >>= 8; } } - this->update_(); + this->update_used_space_(); } void ByteBuffer::put_float(float value) { static_assert(sizeof(float) == sizeof(uint32_t), "Float sizes other than 32 bit not supported"); @@ -178,7 +191,7 @@ void ByteBuffer::put_vector(const std::vector &value) { assert(this->get_remaining() >= value.size()); std::copy(value.begin(), value.end(), this->data_.begin() + this->position_); this->position_ += value.size(); - this->update_(); + this->update_used_space_(); } void ByteBuffer::put_bytes(const uint8_t *value, size_t length) { assert(this->get_remaining() >= length); @@ -186,7 +199,7 @@ void ByteBuffer::put_bytes(const uint8_t *value, size_t length) { while (length-- != 0) { this->data_[this->position_++] = static_cast(*(value + index++)); } - this->update_(); + this->update_used_space_(); } } // namespace esphome diff --git a/esphome/core/bytebuffer.h b/esphome/core/bytebuffer.h index 4662195fb4..50dd10250d 100644 --- a/esphome/core/bytebuffer.h +++ b/esphome/core/bytebuffer.h @@ -139,11 +139,7 @@ class ByteBuffer { void reset() { this->position_ = this->mark_; } void resize() { this->used_space_ = this->position_; } - bool is_changed() { - bool changed = this->is_changed_; - this->is_changed_ = false; - return changed; - } + bool is_changed(); ByteBuffer &operator[](size_t idx) { this->set_position(idx); @@ -152,12 +148,8 @@ class ByteBuffer { protected: ByteBuffer(std::vector const &data) : data_(data), limit_(data.size()) {} - void update_() { - this->is_changed_ = true; - if (this->used_space_ < this->position_) { - this->resize(); - } - } + void update_used_space_(); + std::vector data_; Endian endianness_{LITTLE}; size_t position_{0};