Arduino KVComm  master
Key-Value pair communication library for Arduino
KV_Builder.ipp
Go to the documentation of this file.
1 #ifdef ARDUINO
2 
5 
6 #else
7 
8 #include <KVComm/KV_Builder.hpp>
9 #include <KVComm/KV_Types.hpp>
10 
11 #endif
12 
13 template <class T>
14 void KV_Builder::overwrite(uint8_t *buffer, const T *data, size_t count) {
15  for (size_t i = 0; i < count; ++i) {
18  }
19 }
20 
21 template <class T>
22 bool KV_Builder::append(const char *key, const T *data, size_t count) {
23  uint8_t *dataDestination = writeHeader(key, KV_Type<T>::getTypeID(),
24  KV_Type<T>::getLength() * count);
25  if (dataDestination == nullptr)
26  return false;
27  overwrite(dataDestination, data, count);
28  return true;
29 }
30 
31 template <class T>
32 bool KV_Builder::overwrite(KV_Iterator::iterator existing, const T *data,
33  size_t count) {
34  if (existing->getTypeID() != KV_Type<T>::getTypeID() ||
35  existing->getDataLength() != KV_Type<T>::getLength() * count)
36  return false;
37  auto offset = existing->getData() - buffer;
38  uint8_t *destination = buffer + offset;
39  overwrite(destination, data, count);
40  return true;
41 }
KV_Type
Template struct for making types serializable.
Definition: KV_Types.hpp:43
KV_Types.hpp
KV_Type type definitions for fundamental types (int::_t, uint::_t, float, double, bool,...
KV_Builder::append
bool append(const char *key, const T *data, size_t count)
Append the new element to the buffer.
Definition: KV_Builder.ipp:22
KV_Type::getTypeID
constexpr static uint8_t getTypeID()
KV_Builder::writeHeader
uint8_t * writeHeader(const char *key, uint8_t typeID, size_t length)
Write the header of a new element into the buffer, advance the write pointer, and return a pointer to...
Definition: KV_Builder.cpp:29
KV_Builder::buffer
uint8_t * buffer
Definition: KV_Builder.hpp:387
KV_Iterator::KV::getTypeID
uint8_t getTypeID() const
Get the type ID of the current element.
Definition: KV_Iterator.hpp:52
KV_Iterator::KV::getData
const uint8_t * getData() const
Get a pointer to the data of the current element.
Definition: KV_Iterator.hpp:64
KV_Iterator::KV::getDataLength
uint16_t getDataLength() const
Get the length of the data of the current element.
Definition: KV_Iterator.hpp:56
KV_Iterator::iterator
Definition: KV_Iterator.hpp:268
KV_Builder::overwrite
bool overwrite(KV_Iterator::iterator existing, const T *data, size_t count)
Overwrite the existing element referenced by existing with the new data, if the type and size match.
Definition: KV_Builder.ipp:32
KV_Type::writeToBuffer
static void writeToBuffer(const T &, uint8_t *buffer)
KV_Builder.hpp
This file contains the KV_Builder class, a key-value pair, dictionary-like container that supports ma...
KV_Type::getLength
constexpr static size_t getLength()