mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 01:34:18 +01:00
remove the bytebuffer
This commit is contained in:
parent
55a23691d7
commit
eaa5abc260
2 changed files with 5 additions and 61 deletions
|
@ -7,9 +7,9 @@
|
|||
|
||||
namespace esphome {
|
||||
|
||||
ByteBuffer ByteBuffer::wrap(const uint8_t *value, size_t length, Endian endianness) {
|
||||
ByteBuffer ByteBuffer::wrap(const uint8_t *ptr, size_t len, Endian endianness) {
|
||||
// there is a double copy happening here, could be optimized but at cost of clarity.
|
||||
std::vector<uint8_t> data(value, value + length);
|
||||
std::vector<uint8_t> data(ptr, ptr + len);
|
||||
ByteBuffer buffer = {data};
|
||||
buffer.endianness_ = endianness;
|
||||
return buffer;
|
||||
|
@ -74,32 +74,16 @@ void ByteBuffer::set_position(size_t position) {
|
|||
void ByteBuffer::clear() {
|
||||
this->limit_ = this->get_capacity();
|
||||
this->position_ = 0;
|
||||
this->used_space_ = 0;
|
||||
}
|
||||
void ByteBuffer::flip() {
|
||||
this->limit_ = this->used_space_;
|
||||
this->limit_ = this->position_;
|
||||
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_used_space_();
|
||||
return this->data_[this->position_ - 1];
|
||||
return this->data_[this->position_++];
|
||||
}
|
||||
uint64_t ByteBuffer::get_uint(size_t length) {
|
||||
assert(this->get_remaining() >= length);
|
||||
|
@ -117,7 +101,6 @@ uint64_t ByteBuffer::get_uint(size_t length) {
|
|||
value |= this->data_[this->position_++];
|
||||
}
|
||||
}
|
||||
this->update_used_space_();
|
||||
return value;
|
||||
}
|
||||
|
||||
|
@ -141,23 +124,13 @@ std::vector<uint8_t> ByteBuffer::get_vector(size_t length) {
|
|||
assert(this->get_remaining() >= length);
|
||||
auto start = this->data_.begin() + this->position_;
|
||||
this->position_ += length;
|
||||
this->update_used_space_();
|
||||
return {start, start + length};
|
||||
}
|
||||
void ByteBuffer::get_bytes(uint8_t *value, size_t length) {
|
||||
size_t index = 0;
|
||||
assert(this->get_remaining() >= length);
|
||||
while (length-- != 0) {
|
||||
*(value + index++) = this->data_[this->position_++];
|
||||
}
|
||||
this->update_used_space_();
|
||||
}
|
||||
|
||||
/// Putters
|
||||
void ByteBuffer::put_uint8(uint8_t value) {
|
||||
assert(this->get_remaining() >= 1);
|
||||
this->data_[this->position_++] = value;
|
||||
this->update_used_space_();
|
||||
}
|
||||
|
||||
void ByteBuffer::put_uint(uint64_t value, size_t length) {
|
||||
|
@ -175,7 +148,6 @@ void ByteBuffer::put_uint(uint64_t value, size_t length) {
|
|||
value >>= 8;
|
||||
}
|
||||
}
|
||||
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");
|
||||
|
@ -191,15 +163,5 @@ void ByteBuffer::put_vector(const std::vector<uint8_t> &value) {
|
|||
assert(this->get_remaining() >= value.size());
|
||||
std::copy(value.begin(), value.end(), this->data_.begin() + this->position_);
|
||||
this->position_ += value.size();
|
||||
this->update_used_space_();
|
||||
}
|
||||
void ByteBuffer::put_bytes(const uint8_t *value, size_t length) {
|
||||
assert(this->get_remaining() >= length);
|
||||
auto index = 0;
|
||||
while (length-- != 0) {
|
||||
this->data_[this->position_++] = static_cast<uint8_t>(*(value + index++));
|
||||
}
|
||||
this->update_used_space_();
|
||||
}
|
||||
|
||||
} // namespace esphome
|
||||
|
|
|
@ -30,13 +30,10 @@ enum Endian { LITTLE, BIG };
|
|||
* data from a buffer after it has been written.
|
||||
*
|
||||
*/
|
||||
|
||||
class ByteBuffer;
|
||||
class ByteBuffer {
|
||||
public:
|
||||
// Default constructor (compatibility with TEMPLATABLE_VALUE)
|
||||
ByteBuffer() : ByteBuffer(std::vector<uint8_t>()) {}
|
||||
~ByteBuffer() = default;
|
||||
/**
|
||||
* Create a new Bytebuffer with the given capacity
|
||||
*/
|
||||
|
@ -49,7 +46,7 @@ class ByteBuffer {
|
|||
/**
|
||||
* Wrap an existing array in a ByteBuffer. Note that this will create a copy of the data.
|
||||
*/
|
||||
static ByteBuffer wrap(const uint8_t *value, size_t length, Endian endianness = LITTLE);
|
||||
static ByteBuffer wrap(const uint8_t *ptr, size_t len, Endian endianness = LITTLE);
|
||||
// Convenience functions to create a ByteBuffer from a value
|
||||
static ByteBuffer wrap(uint8_t value);
|
||||
static ByteBuffer wrap(uint16_t value, Endian endianness = LITTLE);
|
||||
|
@ -95,7 +92,6 @@ class ByteBuffer {
|
|||
bool get_bool() { return this->get_uint8(); }
|
||||
// Get vector of bytes, increment by length
|
||||
std::vector<uint8_t> get_vector(size_t length);
|
||||
void get_bytes(uint8_t *value, size_t length);
|
||||
|
||||
// Put values into the buffer, increment the position accordingly
|
||||
// put any integral value, length represents the number of bytes
|
||||
|
@ -116,13 +112,11 @@ class ByteBuffer {
|
|||
void put_double(double value);
|
||||
void put_bool(bool value) { this->put_uint8(value); }
|
||||
void put_vector(const std::vector<uint8_t> &value);
|
||||
void put_bytes(const uint8_t *value, size_t length);
|
||||
|
||||
inline size_t get_capacity() const { return this->data_.size(); }
|
||||
inline size_t get_position() const { return this->position_; }
|
||||
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_used_space() const { return this->used_space_; }
|
||||
inline Endian get_endianness() const { return this->endianness_; }
|
||||
inline void mark() { this->mark_ = this->position_; }
|
||||
inline void big_endian() { this->endianness_ = BIG; }
|
||||
|
@ -137,26 +131,14 @@ class ByteBuffer {
|
|||
std::vector<uint8_t> get_data() { return this->data_; };
|
||||
void rewind() { this->position_ = 0; }
|
||||
void reset() { this->position_ = this->mark_; }
|
||||
void resize() { this->used_space_ = this->position_; }
|
||||
|
||||
bool is_changed();
|
||||
|
||||
ByteBuffer &operator[](size_t idx) {
|
||||
this->set_position(idx);
|
||||
return *this;
|
||||
}
|
||||
|
||||
protected:
|
||||
ByteBuffer(std::vector<uint8_t> const &data) : data_(data), limit_(data.size()) {}
|
||||
void update_used_space_();
|
||||
|
||||
std::vector<uint8_t> data_;
|
||||
Endian endianness_{LITTLE};
|
||||
size_t position_{0};
|
||||
size_t mark_{0};
|
||||
size_t limit_{0};
|
||||
size_t used_space_{0};
|
||||
bool is_changed_{false};
|
||||
};
|
||||
|
||||
} // namespace esphome
|
||||
|
|
Loading…
Reference in a new issue