mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 01:34:18 +01:00
Make default constructor safer
This commit is contained in:
parent
d7ea8d6dac
commit
6a7c4b2e59
2 changed files with 11 additions and 24 deletions
|
@ -4,9 +4,11 @@
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
||||||
ByteBuffer ByteBuffer::create(size_t capacity) {
|
ByteBuffer ByteBuffer::create(size_t capacity, Endian endianness) {
|
||||||
std::vector<uint8_t> data(capacity);
|
std::vector<uint8_t> data(capacity);
|
||||||
return {data};
|
ByteBuffer buffer = {data};
|
||||||
|
buffer.endianness_ = endianness;
|
||||||
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer ByteBuffer::wrap(const uint8_t *ptr, size_t len) {
|
ByteBuffer ByteBuffer::wrap(const uint8_t *ptr, size_t len) {
|
||||||
|
@ -23,46 +25,31 @@ ByteBuffer ByteBuffer::wrap(uint8_t value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer ByteBuffer::wrap(uint16_t value, Endian endianness) {
|
ByteBuffer ByteBuffer::wrap(uint16_t value, Endian endianness) {
|
||||||
ByteBuffer buffer = ByteBuffer::create(2);
|
ByteBuffer buffer = ByteBuffer::create(2, endianness);
|
||||||
if (endianness == BIG) {
|
|
||||||
buffer.big_endian();
|
|
||||||
}
|
|
||||||
buffer.put_uint16(value);
|
buffer.put_uint16(value);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer ByteBuffer::wrap(uint32_t value, Endian endianness) {
|
ByteBuffer ByteBuffer::wrap(uint32_t value, Endian endianness) {
|
||||||
ByteBuffer buffer = ByteBuffer::create(4);
|
ByteBuffer buffer = ByteBuffer::create(4, endianness);
|
||||||
if (endianness == BIG) {
|
|
||||||
buffer.big_endian();
|
|
||||||
}
|
|
||||||
buffer.put_uint32(value);
|
buffer.put_uint32(value);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer ByteBuffer::wrap(uint64_t value, Endian endianness) {
|
ByteBuffer ByteBuffer::wrap(uint64_t value, Endian endianness) {
|
||||||
ByteBuffer buffer = ByteBuffer::create(8);
|
ByteBuffer buffer = ByteBuffer::create(8, endianness);
|
||||||
if (endianness == BIG) {
|
|
||||||
buffer.big_endian();
|
|
||||||
}
|
|
||||||
buffer.put_uint64(value);
|
buffer.put_uint64(value);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer ByteBuffer::wrap(float value, Endian endianness) {
|
ByteBuffer ByteBuffer::wrap(float value, Endian endianness) {
|
||||||
ByteBuffer buffer = ByteBuffer::create(sizeof(float));
|
ByteBuffer buffer = ByteBuffer::create(sizeof(float), endianness);
|
||||||
if (endianness == BIG) {
|
|
||||||
buffer.big_endian();
|
|
||||||
}
|
|
||||||
buffer.put_float(value);
|
buffer.put_float(value);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer ByteBuffer::wrap(double value, Endian endianness) {
|
ByteBuffer ByteBuffer::wrap(double value, Endian endianness) {
|
||||||
ByteBuffer buffer = ByteBuffer::create(sizeof(double));
|
ByteBuffer buffer = ByteBuffer::create(sizeof(double), endianness);
|
||||||
if (endianness == BIG) {
|
|
||||||
buffer.big_endian();
|
|
||||||
}
|
|
||||||
buffer.put_double(value);
|
buffer.put_double(value);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,11 +31,11 @@ enum Endian { LITTLE, BIG };
|
||||||
class ByteBuffer {
|
class ByteBuffer {
|
||||||
public:
|
public:
|
||||||
// Default constructor (compatibility with TEMPLATABLE_VALUE)
|
// Default constructor (compatibility with TEMPLATABLE_VALUE)
|
||||||
ByteBuffer() = default;
|
ByteBuffer() : ByteBuffer(std::vector<uint8_t>()) {}
|
||||||
/**
|
/**
|
||||||
* Create a new Bytebuffer with the given capacity
|
* Create a new Bytebuffer with the given capacity
|
||||||
*/
|
*/
|
||||||
static ByteBuffer create(size_t capacity);
|
static ByteBuffer create(size_t capacity, Endian endianness = LITTLE);
|
||||||
/**
|
/**
|
||||||
* Wrap an existing vector in a ByteBufffer
|
* Wrap an existing vector in a ByteBufffer
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue