Arduino KVComm  master
Key-Value pair communication library for Arduino
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Static_KV_Builder< N > Class Template Reference

KV_Builder with a static buffer. More...

#include <KVComm/KV_Builder.hpp>

Inheritance diagram for Static_KV_Builder< N >:
Collaboration diagram for Static_KV_Builder< N >:

Public Member Functions

 Static_KV_Builder ()
 
template<class T >
bool add (const char *key, const T *data, size_t count)
 Add a key-value pair to the dictionary, or update the existing value with the same key. More...
 
template<class T >
bool add (const char *key, const T &value)
 Add a key-value pair to the dictionary, or update the existing value with the same key. More...
 
bool add (const char *key, const char *data)
 Add a key-value pair to the dictionary, or update the existing value with the same key. More...
 
template<class T , size_t N>
bool add (const char *key, const T(&array)[N])
 Add a key-value pair to the dictionary, or update the existing value with the same key. More...
 
template<class T >
bool add (const char *key, std::initializer_list< T > list)
 Add a key-value pair to the dictionary, or update the existing value with the same key. More...
 
template<class T , size_t N>
bool add (const char *key, const std::array< T, N > &array)
 Add a key-value pair to the dictionary, or update the existing value with the same key. More...
 
template<class T >
bool add (const char *key, const std::vector< T > &vector)
 Add a key-value pair to the dictionary, or update the existing value with the same key. More...
 
void clear ()
 Clear all elements of the dictionary. More...
 
void print (std::ostream &os) const
 
void print (Print &os) const
 Dump the dictionary buffer to the given output stream in a human-readable format (offset + hexadecimal + ASCII). More...
 
void printPython (std::ostream &os) const
 
void printPython (Print &os) const
 Dump the dictionary buffer to the given output stream as a Python bytes object. More...
 
const uint8_t * getBuffer () const
 Get the buffer containing the dictionary. More...
 
size_t getBufferSize () const
 Get the total size of the buffer. More...
 
size_t getLength () const
 Get the length of the used part of the buffer. More...
 
void changeBuffer (uint8_t *buffer, size_t size)
 Replace the buffer. More...
 
KV_Iterator::iterator find (const char *key) const
 Get the element with the given key. More...
 

Private Member Functions

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 where the data should be written. More...
 
template<class T >
bool append (const char *key, const T *data, size_t count)
 Append the new element to the buffer. More...
 
template<class T >
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. More...
 
template<class T >
void overwrite (uint8_t *buffer, const T *data, size_t count)
 (Over)write the data of an element to the buffer. More...
 

Private Attributes

std::array< uint8_t, N > buffer = {{}}
 
size_t bufferSize
 
size_t maxLen = bufferSize
 The maximum remaining length that is still free. More...
 
uint8_t * bufferwritelocation = buffer
 A pointer to the first free/unused byte in the buffer. More...
 

Detailed Description

template<size_t N>
class Static_KV_Builder< N >

KV_Builder with a static buffer.

Examples
KVComm-Send.ino.

Definition at line 452 of file KV_Builder.hpp.

Constructor & Destructor Documentation

◆ Static_KV_Builder()

Static_KV_Builder ( )
inline

Definition at line 454 of file KV_Builder.hpp.

Member Function Documentation

◆ add() [1/7]

bool add ( const char *  key,
const T *  data,
size_t  count 
)
inlineinherited

Add a key-value pair to the dictionary, or update the existing value with the same key.

If the key can't be found in the dictionary, the new element is appended. If a value with the same key is found, and if the type and size are the same, the existing value is updated with the new value. If the type or size don't match, the dictionary is not altered.

Template Parameters
TThe type of the value to add.
Parameters
keyThe key for this element. The maximum length is 255 characters.
dataA pointer to the (array of) value(s) to add.
countThe number of elements in the array. The maximum size is 65535 bytes, so the maximum count is 65535 divided by the size of one element.
Return values
trueThe element was added to the dictionary successfully, or the existing element with the same key was updated with the new value.
falseThe buffer is full, the key or data length is too large, or the type and size don't match the ones of the existing element with the same key.

Definition at line 142 of file KV_Builder.hpp.

◆ add() [2/7]

bool add ( const char *  key,
const T &  value 
)
inlineinherited

Add a key-value pair to the dictionary, or update the existing value with the same key.

The data of the element is a single value.

If the key can't be found in the dictionary, the new element is appended. If a value with the same key is found, and if the type and size are the same, the existing value is updated with the new value. If the type or size don't match, the dictionary is not altered.

Template Parameters
TThe type of the value to add.
Parameters
keyThe key for this element. The maximum length is 255 characters.
valueThe value to add. The maximum size is 65535 bytes.
Return values
trueThe element was added to the dictionary successfully, or the existing element with the same key was updated with the new value.
falseThe buffer is full, the key or data length is too large, or the type and size don't match the ones of the existing element with the same key.

Definition at line 187 of file KV_Builder.hpp.

◆ add() [3/7]

bool add ( const char *  key,
const char *  data 
)
inlineinherited

Add a key-value pair to the dictionary, or update the existing value with the same key.

The data of the element is a null-terminated string.

If the key can't be found in the dictionary, the new element is appended. If a value with the same key is found, and if the type and size are the same, the existing value is updated with the new value. If the type or size don't match, the dictionary is not altered.

Parameters
keyThe key for this element. The maximum length is 255 characters.
dataThe string to add. The maximum length is 65534 characters.
Return values
trueThe element was added to the dictionary successfully, or the existing element with the same key was updated with the new value.
falseThe buffer is full, the key or data length is too large, or the type and size don't match the ones of the existing element with the same key.

Definition at line 216 of file KV_Builder.hpp.

◆ add() [4/7]

bool add ( const char *  key,
const T(&)  array[N] 
)
inlineinherited

Add a key-value pair to the dictionary, or update the existing value with the same key.

The data of the element is an array of values.

If the key can't be found in the dictionary, the new element is appended. If a value with the same key is found, and if the type and size are the same, the existing value is updated with the new value. If the type or size don't match, the dictionary is not altered.

Template Parameters
TThe type of the values to add.
NThe number of values to add.
Parameters
keyThe key for this element. The maximum length is 255 characters.
arrayThe array of values to add. The maximum size is 65535 bytes.
Return values
trueThe element was added to the dictionary successfully, or the existing element with the same key was updated with the new value.
falseThe buffer is full, the key or data length is too large, or the type and size don't match the ones of the existing element with the same key.

Definition at line 250 of file KV_Builder.hpp.

◆ add() [5/7]

bool add ( const char *  key,
std::initializer_list< T >  list 
)
inlineinherited

Add a key-value pair to the dictionary, or update the existing value with the same key.

The data of the element is a list of values.

If the key can't be found in the dictionary, the new element is appended. If a value with the same key is found, and if the type and size are the same, the existing value is updated with the new value. If the type or size don't match, the dictionary is not altered.

Template Parameters
TThe type of the values to add.
Parameters
keyThe key for this element. The maximum length is 255 characters.
listThe list of values to add. The maximum size is 65535 bytes.
Return values
trueThe element was added to the dictionary successfully, or the existing element with the same key was updated with the new value.
falseThe buffer is full, the key or data length is too large, or the type and size don't match the ones of the existing element with the same key.

Definition at line 282 of file KV_Builder.hpp.

◆ add() [6/7]

bool add ( const char *  key,
const std::array< T, N > &  array 
)
inlineinherited

Add a key-value pair to the dictionary, or update the existing value with the same key.

The data of the element is an std::array of values.

If the key can't be found in the dictionary, the new element is appended. If a value with the same key is found, and if the type and size are the same, the existing value is updated with the new value. If the type or size don't match, the dictionary is not altered.

Template Parameters
TThe type of the values to add.
NThe number of values to add.
Parameters
keyThe key for this element. The maximum length is 255 characters.
arrayThe array of values to add. The maximum size is 65535 bytes.
Return values
trueThe element was added to the dictionary successfully, or the existing element with the same key was updated with the new value.
falseThe buffer is full, the key or data length is too large, or the type and size don't match the ones of the existing element with the same key.

Definition at line 316 of file KV_Builder.hpp.

◆ add() [7/7]

bool add ( const char *  key,
const std::vector< T > &  vector 
)
inlineinherited

Add a key-value pair to the dictionary, or update the existing value with the same key.

The data of the element is an std::vector of values.

If the key can't be found in the dictionary, the new element is appended. If a value with the same key is found, and if the type and size are the same, the existing value is updated with the new value. If the type or size don't match, the dictionary is not altered.

Template Parameters
TThe type of the values to add.
Parameters
keyThe key for this element. The maximum length is 255 characters.
vectorThe vector of values to add. The maximum size is 65535 bytes.
Return values
trueThe element was added to the dictionary successfully, or the existing element with the same key was updated with the new value.
falseThe buffer is full, the key or data length is too large, or the type and size don't match the ones of the existing element with the same key.

Definition at line 348 of file KV_Builder.hpp.

◆ clear()

void clear ( )
inlineinherited

Clear all elements of the dictionary.

Definition at line 355 of file KV_Builder.hpp.

◆ print() [1/2]

void print ( std::ostream &  os) const
inherited

Definition at line 133 of file KV_Builder.cpp.

◆ print() [2/2]

void print ( Print &  os) const
inherited

Dump the dictionary buffer to the given output stream in a human-readable format (offset + hexadecimal + ASCII).

Parameters
osThe stream to print to.

Definition at line 140 of file KV_Builder.cpp.

◆ printPython() [1/2]

void printPython ( std::ostream &  os) const
inherited

Definition at line 134 of file KV_Builder.cpp.

◆ printPython() [2/2]

void printPython ( Print &  os) const
inherited

Dump the dictionary buffer to the given output stream as a Python bytes object.

Parameters
osThe stream to print to.

Definition at line 141 of file KV_Builder.cpp.

◆ writeHeader()

uint8_t * writeHeader ( const char *  key,
uint8_t  typeID,
size_t  length 
)
privateinherited

Write the header of a new element into the buffer, advance the write pointer, and return a pointer to where the data should be written.

Returns a null pointer if the element is too large for the buffer, and in that case, the write pointer is unaltered.

Definition at line 29 of file KV_Builder.cpp.

◆ append()

bool append ( const char *  key,
const T *  data,
size_t  count 
)
privateinherited

Append the new element to the buffer.

Returns false if the element is too large for the buffer.

Definition at line 22 of file KV_Builder.ipp.

◆ overwrite() [1/2]

bool overwrite ( KV_Iterator::iterator  existing,
const T *  data,
size_t  count 
)
privateinherited

Overwrite the existing element referenced by existing with the new data, if the type and size match.

Returns false if the type or size doesn't match, and in that case, nothing is overwritten.

Definition at line 32 of file KV_Builder.ipp.

◆ overwrite() [2/2]

void overwrite ( uint8_t *  buffer,
const T *  data,
size_t  count 
)
privateinherited

(Over)write the data of an element to the buffer.

Definition at line 14 of file KV_Builder.ipp.

◆ getBuffer()

const uint8_t* getBuffer ( ) const
inlineinherited

Get the buffer containing the dictionary.

Definition at line 421 of file KV_Builder.hpp.

◆ getBufferSize()

size_t getBufferSize ( ) const
inlineinherited

Get the total size of the buffer.

Definition at line 423 of file KV_Builder.hpp.

◆ getLength()

size_t getLength ( ) const
inlineinherited

Get the length of the used part of the buffer.

Definition at line 425 of file KV_Builder.hpp.

◆ changeBuffer()

void changeBuffer ( uint8_t *  buffer,
size_t  size 
)
inlineinherited

Replace the buffer.

Definition at line 428 of file KV_Builder.hpp.

◆ find()

KV_Iterator::iterator find ( const char *  key) const
inherited

Get the element with the given key.

Parameters
keyThe unique key of the element to retreive.
Returns
An iterator to the element with the given key, or a default-constructed KV_Iterator::iterator if the key was not found in the dictionary.

Definition at line 146 of file KV_Builder.cpp.

Member Data Documentation

◆ buffer

std::array<uint8_t, N> buffer = {{}}
private

Definition at line 459 of file KV_Builder.hpp.

◆ bufferSize

size_t bufferSize
privateinherited

Definition at line 388 of file KV_Builder.hpp.

◆ maxLen

size_t maxLen = bufferSize
privateinherited

The maximum remaining length that is still free.

Starts off as the buffer size, reaches zero when the buffer is full.

Definition at line 391 of file KV_Builder.hpp.

◆ bufferwritelocation

uint8_t* bufferwritelocation = buffer
privateinherited

A pointer to the first free/unused byte in the buffer.

Definition at line 394 of file KV_Builder.hpp.


The documentation for this class was generated from the following file: