1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-04 09:48:16 +00:00

edxtend the functionality of the bytebuffer.

This commit is contained in:
NP v/d Spek 2024-09-19 15:31:10 +02:00
parent 4819071770
commit 252dca528f
2 changed files with 27 additions and 23 deletions

View File

@ -74,9 +74,10 @@ void ByteBuffer::set_position(size_t position) {
void ByteBuffer::clear() { void ByteBuffer::clear() {
this->limit_ = this->get_capacity(); this->limit_ = this->get_capacity();
this->position_ = 0; this->position_ = 0;
this->used_space_ = 0;
} }
void ByteBuffer::flip() { void ByteBuffer::flip() {
this->limit_ = this->position_; this->limit_ = this->used_space_;
this->position_ = 0; this->position_ = 0;
} }
@ -84,7 +85,7 @@ void ByteBuffer::flip() {
uint8_t ByteBuffer::get_uint8() { uint8_t ByteBuffer::get_uint8() {
assert(this->get_remaining() >= 1); assert(this->get_remaining() >= 1);
this->position_++; this->position_++;
this->update_used_(); this->update_used_space_();
return this->data_[this->position_]; return this->data_[this->position_];
} }
uint64_t ByteBuffer::get_uint(size_t length) { uint64_t ByteBuffer::get_uint(size_t length) {
@ -103,7 +104,7 @@ uint64_t ByteBuffer::get_uint(size_t length) {
value |= this->data_[this->position_++]; value |= this->data_[this->position_++];
} }
} }
this->update_used_(); this->update_used_space_();
return value; return value;
} }
@ -127,22 +128,23 @@ std::vector<uint8_t> ByteBuffer::get_vector(size_t length) {
assert(this->get_remaining() >= length); assert(this->get_remaining() >= length);
auto start = this->data_.begin() + this->position_; auto start = this->data_.begin() + this->position_;
this->position_ += length; this->position_ += length;
this->update_used_(); this->update_used_space_();
return {start, start + length}; return {start, start + length};
} }
void ByteBuffer::get_bytes(const uint8_t *data, size_t length) { void ByteBuffer::get_bytes(uint8_t *data, size_t length) {
size_t index = 0;
assert(this->get_remaining() >= length); assert(this->get_remaining() >= length);
auto start = this->data_.begin() + this->position_; while (length-- != 0) {
copy(start, start + length, data); *(data + index++) = this->data_[this->position_++];
this->position_ += length; }
this->update_used_(); this->update_used_space_();
} }
/// Putters /// Putters
void ByteBuffer::put_uint8(uint8_t value) { void ByteBuffer::put_uint8(uint8_t value) {
assert(this->get_remaining() >= 1); assert(this->get_remaining() >= 1);
this->data_[this->position_++] = value; this->data_[this->position_++] = value;
this->update_used_(); this->update_used_space_();
} }
void ByteBuffer::put_uint(uint64_t value, size_t length) { void ByteBuffer::put_uint(uint64_t value, size_t length) {
@ -160,7 +162,7 @@ void ByteBuffer::put_uint(uint64_t value, size_t length) {
value >>= 8; value >>= 8;
} }
} }
this->update_used_(); this->update_used_space_();
} }
void ByteBuffer::put_float(float value) { void ByteBuffer::put_float(float value) {
static_assert(sizeof(float) == sizeof(uint32_t), "Float sizes other than 32 bit not supported"); static_assert(sizeof(float) == sizeof(uint32_t), "Float sizes other than 32 bit not supported");
@ -176,13 +178,15 @@ void ByteBuffer::put_vector(const std::vector<uint8_t> &value) {
assert(this->get_remaining() >= value.size()); assert(this->get_remaining() >= value.size());
std::copy(value.begin(), value.end(), this->data_.begin() + this->position_); std::copy(value.begin(), value.end(), this->data_.begin() + this->position_);
this->position_ += value.size(); this->position_ += value.size();
this->update_used_(); this->update_used_space_();
} }
void ByteBuffer::put_bytes(const uint8_t *data, size_t size) { void ByteBuffer::put_bytes(const uint8_t *data, size_t length) {
assert(this->get_remaining() >= size); assert(this->get_remaining() >= length);
std::copy(data[0], data[size], this->data_.begin() + this->position_); auto index = 0;
this->position_ += size; while (length-- != 0) {
this->update_used_(); this->data_[this->position_++] = static_cast<uint8_t>(*(data + index++));
}
this->update_used_space_();
} }
} // namespace esphome } // namespace esphome

View File

@ -92,7 +92,7 @@ class ByteBuffer {
bool get_bool() { return this->get_uint8(); } bool get_bool() { return this->get_uint8(); }
// Get vector of bytes, increment by length // Get vector of bytes, increment by length
std::vector<uint8_t> get_vector(size_t length); std::vector<uint8_t> get_vector(size_t length);
void get_bytes(const uint8_t *data, size_t length); void get_bytes(uint8_t *data, size_t length);
// Put values into the buffer, increment the position accordingly // Put values into the buffer, increment the position accordingly
// put any integral value, length represents the number of bytes // put any integral value, length represents the number of bytes
@ -119,7 +119,7 @@ class ByteBuffer {
inline size_t get_position() const { return this->position_; } inline size_t get_position() const { return this->position_; }
inline size_t get_limit() const { return this->limit_; } inline size_t get_limit() const { return this->limit_; }
inline size_t get_remaining() const { return this->get_limit() - this->get_position(); } inline size_t get_remaining() const { return this->get_limit() - this->get_position(); }
inline size_t get_used() const { return this->used_; } inline size_t get_used_space() const { return this->used_space_; }
inline Endian get_endianness() const { return this->endianness_; } inline Endian get_endianness() const { return this->endianness_; }
inline void mark() { this->mark_ = this->position_; } inline void mark() { this->mark_ = this->position_; }
inline void big_endian() { this->endianness_ = BIG; } inline void big_endian() { this->endianness_ = BIG; }
@ -134,12 +134,12 @@ class ByteBuffer {
std::vector<uint8_t> get_data() { return this->data_; }; std::vector<uint8_t> get_data() { return this->data_; };
void rewind() { this->position_ = 0; } void rewind() { this->position_ = 0; }
void reset() { this->position_ = this->mark_; } void reset() { this->position_ = this->mark_; }
void resize() { this->used_ = this->position_; } void resize() { this->used_space_ = this->position_; }
protected: protected:
ByteBuffer(std::vector<uint8_t> const &data) : data_(data), limit_(data.size()) {} ByteBuffer(std::vector<uint8_t> const &data) : data_(data), limit_(data.size()) {}
void update_used_() { void update_used_space_() {
if (this->used_ < this->position_) { if (this->used_space_ < this->position_) {
this->resize(); this->resize();
} }
} }
@ -148,7 +148,7 @@ class ByteBuffer {
size_t position_{0}; size_t position_{0};
size_t mark_{0}; size_t mark_{0};
size_t limit_{0}; size_t limit_{0};
size_t used_{0}; size_t used_space_{0};
}; };
} // namespace esphome } // namespace esphome