10 namespace bytebuffer {
59 T
get(
typename std::enable_if<std::is_integral<T>::value, T>
::type * = 0,
62 return static_cast<T
>(this->
get_uint32_(
sizeof(T)));
66 T
get(
size_t offset,
typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
68 return static_cast<T
>(this->
get_uint32_(offset,
sizeof(T)));
72 void put(
const T &value,
typename std::enable_if<std::is_integral<T>::value, T>::
type * = 0,
73 typename std::enable_if<(
sizeof(T) <=
sizeof(uint32_t)), T>::
type * = 0) {
74 this->
put_uint32_(static_cast<uint32_t>(value),
sizeof(T));
78 void put(
const T &value,
size_t offset,
typename std::enable_if<std::is_integral<T>::value, T>::
type * = 0,
79 typename std::enable_if<(
sizeof(T) <=
sizeof(uint32_t)), T>::
type * = 0) {
80 this->
put_uint32_(static_cast<uint32_t>(value), offset,
sizeof(T));
85 T
get(
typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
87 return static_cast<T
>(this->
get_uint64_(
sizeof(T)));
91 T
get(
size_t offset,
typename std::enable_if<std::is_integral<T>::value, T>::type * = 0,
93 return static_cast<T
>(this->
get_uint64_(offset,
sizeof(T)));
97 void put(
const T &value,
typename std::enable_if<std::is_integral<T>::value, T>::
type * = 0,
98 typename std::enable_if<(
sizeof(T) ==
sizeof(uint64_t)), T>::
type * = 0) {
103 void put(
const T &value,
size_t offset,
typename std::enable_if<std::is_integral<T>::value, T>::
type * = 0,
104 typename std::enable_if<(
sizeof(T) ==
sizeof(uint64_t)), T>::
type * = 0) {
105 this->
put_uint64_(static_cast<uint64_t>(value), offset,
sizeof(T));
110 T
get(
typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
116 T
get(
typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
122 T
get(
size_t offset,
typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
128 T
get(
size_t offset,
typename std::enable_if<std::is_floating_point<T>::value, T>::type * = 0,
133 void put(
const T &value,
typename std::enable_if<std::is_floating_point<T>::value, T>::
type * = 0,
134 typename std::enable_if<(
sizeof(T) <=
sizeof(uint32_t)), T>::
type * = 0) {
135 this->
put_uint32_(bit_cast<uint32_t>(value),
sizeof(T));
139 void put(
const T &value,
typename std::enable_if<std::is_floating_point<T>::value, T>::
type * = 0,
140 typename std::enable_if<(
sizeof(T) ==
sizeof(uint64_t)), T>::
type * = 0) {
141 this->
put_uint64_(bit_cast<uint64_t>(value),
sizeof(T));
145 void put(
const T &value,
size_t offset,
typename std::enable_if<std::is_floating_point<T>::value, T>::
type * = 0,
146 typename std::enable_if<(
sizeof(T) <=
sizeof(uint32_t)), T>::
type * = 0) {
147 this->
put_uint32_(bit_cast<uint32_t>(value), offset,
sizeof(T));
151 void put(
const T &value,
size_t offset,
typename std::enable_if<std::is_floating_point<T>::value, T>::
type * = 0,
152 typename std::enable_if<(
sizeof(T) ==
sizeof(uint64_t)), T>::
type * = 0) {
153 this->
put_uint64_(bit_cast<uint64_t>(value), offset,
sizeof(T));
170 return wrap(std::vector<uint8_t>(ptr, ptr + len), endianness);
201 uint32_t mask = (~static_cast<uint32_t>(0)) << 23;
202 if ((value & mask) != 0)
209 uint32_t mask = (~static_cast<uint32_t>(0)) << 23;
210 if ((value & mask) != 0)
215 auto start = this->
data_.begin() + offset;
216 return {start, start + length};
242 uint64_t
get_uint64(
size_t offset) {
return this->get<uint64_t>(offset); }
244 double get_double(
size_t offset) {
return get<double>(offset); }
249 uint16_t
get_uint16(
size_t offset) {
return get<uint16_t>(offset); }
251 uint32_t
get_uint32(
size_t offset) {
return this->get<uint32_t>(offset); };
253 uint8_t
get_int8(
size_t offset) {
return get<int8_t>(offset); }
254 int16_t
get_int16(
size_t offset) {
return get<int16_t>(offset); }
255 int32_t
get_int32(
size_t offset) {
return get<int32_t>(offset); }
256 int64_t
get_int64(
size_t offset) {
return get<int64_t>(offset); }
258 float get_float(
size_t offset) {
return get<float>(offset); }
265 void put_uint16(uint16_t value,
size_t offset) { this->
put(value, offset); }
266 void put_uint24(uint32_t value,
size_t offset) { this->
put(value, offset); }
267 void put_uint32(uint32_t value,
size_t offset) { this->
put(value, offset); }
268 void put_uint64(uint64_t value,
size_t offset) { this->
put(value, offset); }
270 void put_int8(int8_t value,
size_t offset) { this->
put_uint8(static_cast<uint8_t>(value), offset); }
271 void put_int16(int16_t value,
size_t offset) { this->
put(value, offset); }
273 void put_int32(int32_t value,
size_t offset) { this->
put(value, offset); }
274 void put_int64(int64_t value,
size_t offset) { this->
put(value, offset); }
276 void put_float(
float value,
size_t offset) { this->
put(value, offset); }
279 void put(
const std::vector<uint8_t> &value,
size_t offset) {
280 std::copy(value.begin(), value.end(), this->
data_.begin() + offset);
282 void put_vector(
const std::vector<uint8_t> &value,
size_t offset) { this->
put(value, offset); }
283 void put(
const std::vector<uint8_t> &value) {
308 std::copy(this->
data_.begin() + offset, this->
data_.begin() + offset +
length, (uint8_t *) dest);
330 while (length-- != 0) {
332 value |= this->
data_[--offset];
335 while (length-- != 0) {
337 value |= this->
data_[offset++];
352 while (length-- != 0) {
354 value |= this->
data_[--offset];
357 while (length-- != 0) {
359 value |= this->
data_[offset++];
384 while (length-- != 0) {
385 this->
data_[offset++] =
static_cast<uint8_t
>(value);
390 while (length-- != 0) {
391 this->
data_[--offset] =
static_cast<uint8_t
>(value);
399 while (length-- != 0) {
400 this->
data_[offset++] =
static_cast<uint8_t
>(value);
405 while (length-- != 0) {
406 this->
data_[--offset] =
static_cast<uint8_t
>(value);
Endian get_endianness() const
void put_int8(int8_t value, size_t offset)
void put(const T &value, size_t offset, typename std::enable_if< std::is_floating_point< T >::value, T >::type *=0, typename std::enable_if<(sizeof(T)<=sizeof(uint32_t)), T >::type *=0)
ByteBuffer(size_t capacity, Endian endianness=LITTLE)
Create a new Bytebuffer with the given capacity.
std::vector< uint8_t > get_vector(size_t length, size_t offset)
size_t get_remaining() const
void put_uint32_(uint32_t value, size_t length)
void put(const T &value, typename std::enable_if< std::is_floating_point< T >::value, T >::type *=0, typename std::enable_if<(sizeof(T)==sizeof(uint64_t)), T >::type *=0)
static ByteBuffer wrap(std::vector< uint8_t > const &data, Endian endianness=LITTLE)
uint32_t get_int24(size_t offset)
void put_uint64(uint64_t value)
void put_int64(int64_t value)
void put(const T &value, typename std::enable_if< std::is_integral< T >::value, T >::type *=0, typename std::enable_if<(sizeof(T)==sizeof(uint64_t)), T >::type *=0)
void put_vector(const std::vector< uint8_t > &value, size_t offset)
void put_int8(int8_t value)
uint32_t get_uint32_(size_t offset, size_t length) const
void put_int16(int16_t value, size_t offset)
void put_float(float value)
void set_position(size_t position)
void put_uint16(uint16_t value, size_t offset)
void get_bytes(void *dest, size_t length)
void put_uint8(uint8_t value, size_t offset)
int32_t get_int32(size_t offset)
int16_t get_int16(size_t offset)
std::vector< uint8_t > get_data()
bool get_bool(size_t offset)
uint32_t get_uint32_(size_t length)
ByteBuffer(std::vector< uint8_t > const &data)
uint64_t get_uint64(size_t offset)
int64_t get_int64(size_t offset)
void put(const std::vector< uint8_t > &value)
void put_int24(int32_t value, size_t offset)
void put_vector(const std::vector< uint8_t > &value)
void put(const T &value, typename std::enable_if< std::is_floating_point< T >::value, T >::type *=0, typename std::enable_if<(sizeof(T)<=sizeof(uint32_t)), T >::type *=0)
A class modelled on the Java ByteBuffer class.
void put_int64(int64_t value, size_t offset)
void put_uint32(uint32_t value, size_t offset)
void put_double(double value, size_t offset)
uint32_t get_uint32(size_t offset)
uint32_t get_uint24(size_t offset)
void put_uint16(uint16_t value)
double get_double(size_t offset)
void put_uint32_(uint32_t value, size_t offset, size_t length)
void put_uint24(uint32_t value, size_t offset)
void put_int32(int32_t value)
void put(const T &value, size_t offset, typename std::enable_if< std::is_integral< T >::value, T >::type *=0, typename std::enable_if<(sizeof(T)==sizeof(uint64_t)), T >::type *=0)
size_t get_position() const
void put(const std::vector< uint8_t > &value, size_t offset)
uint8_t get_int8(size_t offset)
void put_uint8(uint8_t value)
void put_uint64(uint64_t value, size_t offset)
void put_uint24(uint32_t value)
void get_bytes(void *dest, size_t length, size_t offset)
void put_double(double value)
void put_int24(int32_t value)
float get_float(size_t offset)
uint16_t get_uint16(size_t offset)
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
void put_uint64_(uint64_t value, size_t length)
Putters.
void put_bool(bool value, size_t offset)
void put_int16(int16_t value)
Implementation of SPI Controller mode.
void put_uint32(uint32_t value)
uint64_t get_uint64_(size_t length)
void put_int32(int32_t value, size_t offset)
static ByteBuffer wrap(const uint8_t *ptr, size_t len, Endian endianness=LITTLE)
void put_float(float value, size_t offset)
uint8_t get_uint8(size_t offset)
void put(const T &value, size_t offset, typename std::enable_if< std::is_integral< T >::value, T >::type *=0, typename std::enable_if<(sizeof(T)<=sizeof(uint32_t)), T >::type *=0)
void put_uint64_(uint64_t value, size_t offset, size_t length)
size_t get_capacity() const
static ByteBuffer wrap(T value, Endian endianness=LITTLE)
std::vector< uint8_t > data_
uint64_t get_uint64_(size_t offset, size_t length) const
void put(const T &value, typename std::enable_if< std::is_integral< T >::value, T >::type *=0, typename std::enable_if<(sizeof(T)<=sizeof(uint32_t)), T >::type *=0)
void set_limit(size_t limit)
void put(const T &value, size_t offset, typename std::enable_if< std::is_floating_point< T >::value, T >::type *=0, typename std::enable_if<(sizeof(T)==sizeof(uint64_t)), T >::type *=0)
void put_bool(bool value)
std::vector< uint8_t > get_vector(size_t length)