Add parameter for endianness to ByteBuffer::wrap

This commit is contained in:
Rapsssito 2024-08-14 13:37:35 +02:00
parent c095b09839
commit 4cec5d8351
2 changed files with 30 additions and 15 deletions

View file

@ -22,32 +22,47 @@ ByteBuffer ByteBuffer::wrap(uint8_t value) {
return buffer;
}
ByteBuffer ByteBuffer::wrap(uint16_t value) {
ByteBuffer ByteBuffer::wrap(uint16_t value, Endian endianness) {
ByteBuffer buffer = ByteBuffer::create(2);
if (endianness == BIG) {
buffer.big_endian();
}
buffer.put_uint16(value);
return buffer;
}
ByteBuffer ByteBuffer::wrap(uint32_t value) {
ByteBuffer ByteBuffer::wrap(uint32_t value, Endian endianness) {
ByteBuffer buffer = ByteBuffer::create(4);
if (endianness == BIG) {
buffer.big_endian();
}
buffer.put_uint32(value);
return buffer;
}
ByteBuffer ByteBuffer::wrap(uint64_t value) {
ByteBuffer ByteBuffer::wrap(uint64_t value, Endian endianness) {
ByteBuffer buffer = ByteBuffer::create(8);
if (endianness == BIG) {
buffer.big_endian();
}
buffer.put_uint64(value);
return buffer;
}
ByteBuffer ByteBuffer::wrap(float value) {
ByteBuffer buffer = ByteBuffer::create(4);
ByteBuffer ByteBuffer::wrap(float value, Endian endianness) {
ByteBuffer buffer = ByteBuffer::create(sizeof(float));
if (endianness == BIG) {
buffer.big_endian();
}
buffer.put_float(value);
return buffer;
}
ByteBuffer ByteBuffer::wrap(double value) {
ByteBuffer buffer = ByteBuffer::create(8);
ByteBuffer ByteBuffer::wrap(double value, Endian endianness) {
ByteBuffer buffer = ByteBuffer::create(sizeof(double));
if (endianness == BIG) {
buffer.big_endian();
}
buffer.put_double(value);
return buffer;
}

View file

@ -46,15 +46,15 @@ class ByteBuffer {
static ByteBuffer wrap(const uint8_t *ptr, size_t len);
// Convenience functions to create a ByteBuffer from a value
static ByteBuffer wrap(const uint8_t value);
static ByteBuffer wrap(const uint16_t value);
static ByteBuffer wrap(const uint32_t value);
static ByteBuffer wrap(const uint64_t value);
static ByteBuffer wrap(const uint16_t value, Endian endianness = LITTLE);
static ByteBuffer wrap(const uint32_t value, Endian endianness = LITTLE);
static ByteBuffer wrap(const uint64_t value, Endian endianness = LITTLE);
static ByteBuffer wrap(const int8_t value) { return wrap((uint8_t) value); }
static ByteBuffer wrap(const int16_t value) { return wrap((uint16_t) value); }
static ByteBuffer wrap(const int32_t value) { return wrap((uint32_t) value); }
static ByteBuffer wrap(const int64_t value) { return wrap((uint64_t) value); }
static ByteBuffer wrap(const float value);
static ByteBuffer wrap(const double value);
static ByteBuffer wrap(const int16_t value, Endian endianness = LITTLE) { return wrap((uint16_t) value, endianness); }
static ByteBuffer wrap(const int32_t value, Endian endianness = LITTLE) { return wrap((uint32_t) value, endianness); }
static ByteBuffer wrap(const int64_t value, Endian endianness = LITTLE) { return wrap((uint64_t) value, endianness); }
static ByteBuffer wrap(const float value, Endian endianness = LITTLE);
static ByteBuffer wrap(const double value, Endian endianness = LITTLE);
static ByteBuffer wrap(const bool value) { return wrap(value ? (uint8_t) 1 : (uint8_t) 0); }
static ByteBuffer wrap(const std::string &value);
static ByteBuffer wrap(const std::initializer_list<uint8_t> values);