MeshNet  1.0.0
Public Member Functions | Protected Member Functions | Protected Attributes
MicroTable< RowT, RowsCount, IndexIsValue > Class Template Reference

This class emulates table storage. This table is created without dynamic allocation. Rows in table are represented by RowT. More...

#include <meshTables.h>

Inheritance diagram for MicroTable< RowT, RowsCount, IndexIsValue >:
Inheritance graph
[legend]
Collaboration diagram for MicroTable< RowT, RowsCount, IndexIsValue >:
Collaboration graph
[legend]

Public Member Functions

 MicroTable (const uint32_t rowLife)
 Constructor. More...
 
const RowT & get (int16_t index) const
 Gets row from table by index. More...
 
RowT get (int16_t index)
 Gets row from table by index. More...
 
const RowT & operator[] (int16_t index) const
 Gets row from table by index. More...
 
RowT operator[] (int16_t index)
 Gets row from table by index. More...
 
bool set (int16_t index, const RowT &row)
 Sets row values in table. Row life time is set to maximum. More...
 
bool set (int16_t index, const RowT &row, int16_t rawLifeTime_)
 Sets row values in table. More...
 
bool remove (const RowT &rowRef)
 Removes first row, which is equal to rowRef. More...
 
bool remove (int16_t index)
 Removes row at index. More...
 
void clear ()
 Removes all rows data. More...
 
void validate ()
 Decreases all valid column values in specified interval (row lifetime). If valid value is 0, row will not be longer valid. More...
 
bool setRawRemainingLifeTime (int16_t index, uint8_t rawLifeTime_=255)
 Sets row life time. Valid counter for this rowcan be changed, only when row is still valid. More...
 
virtual int16_t getIndexOf (const RowT &rowRef) const
 Gets index of row which is equal to rowRef. More...
 
int16_t getFirstBlankRowIndex (int16_t startPos=0) const
 Gets index of first blank row. More...
 
int16_t getOldestRowIndex () const
 Gets index of first oldest row. More...
 
bool exists (int16_t index) const
 Checks if row at specified index exists in table. More...
 
bool exists (const RowT &rowRef) const
 Checks if any row, which is equal to rowRef exists in table. More...
 
bool isRowBlank (int16_t index) const
 Checks if row is blank or not. Row, that is not valid is blank. More...
 
int16_t size () const
 Gets capacity of table in rows. More...
 
int16_t count () const
 Gets number of used rows in buffer. More...
 
uint8_t getMaxRowLifeTime () const
 Gets the maximum row life time in millisseconds. More...
 
int16_t getRealUsedSize () const
 Gets real used count of rows in table. This value is calculated like this: last_used_row_index + 1. More...
 
template<class = typename std::enable_if<!IndexIsValue>::type>
uint8_t getFragmentation () const
 Calculates fragmentation in %. Fragmentation is calculated like this: (getRealUsedSize() - count())/getRealUsedSize() More...
 
template<class = typename std::enable_if<!IndexIsValue>::type>
void defragment ()
 Removes blank spaces between rows. Call this to speed up searching when buffer is fragmented. More...
 
uint32_t getRowRemainingLifeTime (int16_t index) const
 Gets selected row's remaining life time in milliseconds. More...
 
uint8_t getRawRowRemainingLifeTime (int16_t index) const
 Gets selected row's raw remaining life time. More...
 
uint32_t getLifeTimeResolution () const
 Gets life time resolution in milliseconds. More...
 
uint8_t convertLifeTimeToRaw (uint32_t lifeTime) const
 Converts life time in milliseconds to raw life time value. Value is rounded up. More...
 
uint32_t convertRawToLifeTime (uint8_t rawLifeTime) const
 Converts raw life time value to life time in milliseconds. Value is aproximate. To get accurate result, use getRowRemainingLifeTime() method. More...
 
virtual size_t getRawDataSize () const
 Calculates, how many bytes will be printed, when using command printRaw. More...
 
virtual size_t printRaw (OSTREAM &stream, const char tableIdentifier[4]) const
 Prints raw table data through stream. Those data can be decoded in PC or in another MCU. More...
 
size_t print (OSTREAM &stream, bool showIndexes=true, bool doNotPrintBlank=true, bool showRemainingRowLifetime=true) const
 Prints items from table to stream. More...
 
size_t printTo (Print &p) const override
 Makes this class printable. More...
 

Protected Member Functions

virtual void onCreate (int16_t index, const RowT &newValue, uint8_t newRawLifeTime)
 This method is called when valid row is set to blank space. It is called after count calculation is done, but before buffer is updated. More...
 
virtual void onSet (int16_t index, const RowT &oldValue, uint8_t oldRawLifeTime, const RowT &newValue, uint8_t newRawLifeTime)
 This method is called when valid row is set to new value. It is called after count calculation is done, but before buffer is updated. More...
 
virtual void onRemove (int16_t index, const RowT &oldValue, uint8_t oldRawLifeTime, bool removedByValidation=false)
 This method is called when valid row is removed. It is called after count calculation is done, but before buffer is updated. More...
 
virtual void onClear ()
 This method is called when table is cleared. It is called after cleaning is done, so all variables are fresh. More...
 
virtual void onRowLifeChanged (int16_t index, uint8_t old_raw, uint8_t new_raw)
 This method is called when table is cleared. It is called after cleaning is done, so all variables are fresh. More...
 

Protected Attributes

RowT buffer [RowsCount]
 
uint8_t lifeTime [RowsCount]
 
uint32_t lastValidationTime = 0
 
int16_t usedBufferSize = 0
 
int16_t realUsedSize = 0
 
const uint32_t ValidationInterval = 0
 
const uint32_t RowLife = 0
 

Detailed Description

template<typename RowT, int16_t RowsCount, bool IndexIsValue = false>
class MicroTable< RowT, RowsCount, IndexIsValue >

This class emulates table storage. This table is created without dynamic allocation. Rows in table are represented by RowT.

Template parameters

RowT - row type RowsCount - maximum count of rows, table capacity IndexIsValue - True if index column is taken as value. Default is false. If set to true, defragmentation cannot be done and printRow() method will contain extra parameter, where index is copyied.

RowT requirements

Constructor & Destructor Documentation

◆ MicroTable()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
MicroTable< RowT, RowsCount, IndexIsValue >::MicroTable ( const uint32_t  rowLife)
inline

Constructor.

Parameters
rowLifeLife time of each row in milliseconds. Value has to be higher than 0, but it is recommended to set value to higher than 100 seconds.

Member Function Documentation

◆ clear()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
void MicroTable< RowT, RowsCount, IndexIsValue >::clear ( )
inline

Removes all rows data.

◆ convertLifeTimeToRaw()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
uint8_t MicroTable< RowT, RowsCount, IndexIsValue >::convertLifeTimeToRaw ( uint32_t  lifeTime) const
inline

Converts life time in milliseconds to raw life time value. Value is rounded up.

Parameters
lifeTimeLife time in milliseconds.
Returns
Returns raw life time value. Value from 0 to 255.

◆ convertRawToLifeTime()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
uint32_t MicroTable< RowT, RowsCount, IndexIsValue >::convertRawToLifeTime ( uint8_t  rawLifeTime) const
inline

Converts raw life time value to life time in milliseconds. Value is aproximate. To get accurate result, use getRowRemainingLifeTime() method.

Parameters
rawLifeTimeRaw life time.
Returns
Returns converted life time value in milliseconds.

◆ count()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
int16_t MicroTable< RowT, RowsCount, IndexIsValue >::count ( ) const
inline

Gets number of used rows in buffer.

Returns
Returns number of used rows in buffer.

◆ defragment()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
template<class = typename std::enable_if<!IndexIsValue>::type>
void MicroTable< RowT, RowsCount, IndexIsValue >::defragment ( )
inline

Removes blank spaces between rows. Call this to speed up searching when buffer is fragmented.

◆ exists() [1/2]

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
bool MicroTable< RowT, RowsCount, IndexIsValue >::exists ( const RowT &  rowRef) const
inline

Checks if any row, which is equal to rowRef exists in table.

Parameters
rowRefReference to row, which will be compared with searched rows.
Returns
Returns true if any row, which is equal to rowRef exists in table.

◆ exists() [2/2]

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
bool MicroTable< RowT, RowsCount, IndexIsValue >::exists ( int16_t  index) const
inline

Checks if row at specified index exists in table.

Parameters
indexIndex of row to check.
Returns
Returns true when row at specified index exists in table.

◆ get() [1/2]

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
RowT MicroTable< RowT, RowsCount, IndexIsValue >::get ( int16_t  index)
inline

Gets row from table by index.

Parameters
indexIndex of row. It's value cannot be greather or equal than RowCount.
Returns
Returns copy of row.

◆ get() [2/2]

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
const RowT& MicroTable< RowT, RowsCount, IndexIsValue >::get ( int16_t  index) const
inline

Gets row from table by index.

Parameters
indexIndex of row. It's value cannot be greather or equal than RowCount.
Returns
Returns reference to row.

◆ getFirstBlankRowIndex()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
int16_t MicroTable< RowT, RowsCount, IndexIsValue >::getFirstBlankRowIndex ( int16_t  startPos = 0) const
inline

Gets index of first blank row.

Parameters
startPosPosition, where to start search.
Returns
Returns index of first blank row or -1 if table is full.

◆ getFragmentation()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
template<class = typename std::enable_if<!IndexIsValue>::type>
uint8_t MicroTable< RowT, RowsCount, IndexIsValue >::getFragmentation ( ) const
inline

Calculates fragmentation in %. Fragmentation is calculated like this: (getRealUsedSize() - count())/getRealUsedSize()

Returns
Returns fragmentation in %. Value is from 0 to 100.

◆ getIndexOf()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
virtual int16_t MicroTable< RowT, RowsCount, IndexIsValue >::getIndexOf ( const RowT &  rowRef) const
inlinevirtual

Gets index of row which is equal to rowRef.

Parameters
rowRefReference to row, which will be compared with searched rows.
Returns
Returns index of row which is equal to rowRef.

◆ getLifeTimeResolution()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
uint32_t MicroTable< RowT, RowsCount, IndexIsValue >::getLifeTimeResolution ( ) const
inline

Gets life time resolution in milliseconds.

◆ getMaxRowLifeTime()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
uint8_t MicroTable< RowT, RowsCount, IndexIsValue >::getMaxRowLifeTime ( ) const
inline

Gets the maximum row life time in millisseconds.

Returns
Returns the maximum row life time in millisseconds.

◆ getOldestRowIndex()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
int16_t MicroTable< RowT, RowsCount, IndexIsValue >::getOldestRowIndex ( ) const
inline

Gets index of first oldest row.

Returns
Returns index of first oldest row or -1 if table is full.

◆ getRawDataSize()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
virtual size_t MicroTable< RowT, RowsCount, IndexIsValue >::getRawDataSize ( ) const
inlinevirtual

Calculates, how many bytes will be printed, when using command printRaw.

Returns
Returns how many bytes will be printed, when using command printRaw.

◆ getRawRowRemainingLifeTime()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
uint8_t MicroTable< RowT, RowsCount, IndexIsValue >::getRawRowRemainingLifeTime ( int16_t  index) const
inline

Gets selected row's raw remaining life time.

Parameters
indexIndex of row.
Returns
Returns raw life time. Value from 0 to 255.

◆ getRealUsedSize()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
int16_t MicroTable< RowT, RowsCount, IndexIsValue >::getRealUsedSize ( ) const
inline

Gets real used count of rows in table. This value is calculated like this: last_used_row_index + 1.

Returns
Returns real used count of rows in table or 0 when no row in table is used.

◆ getRowRemainingLifeTime()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
uint32_t MicroTable< RowT, RowsCount, IndexIsValue >::getRowRemainingLifeTime ( int16_t  index) const
inline

Gets selected row's remaining life time in milliseconds.

Parameters
indexIndex of row.
Returns
Returns selected row remaining life time in milliseconds.

◆ isRowBlank()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
bool MicroTable< RowT, RowsCount, IndexIsValue >::isRowBlank ( int16_t  index) const
inline

Checks if row is blank or not. Row, that is not valid is blank.

Parameters
indexIndex of row.
Returns
Returns if row is blank or not.

◆ onClear()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
virtual void MicroTable< RowT, RowsCount, IndexIsValue >::onClear ( )
inlineprotectedvirtual

This method is called when table is cleared. It is called after cleaning is done, so all variables are fresh.

Reimplemented in MeshRouteTable< BufferSize >, and MeshRouteTable< ROUTETABLE_SIZE >.

◆ onCreate()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
virtual void MicroTable< RowT, RowsCount, IndexIsValue >::onCreate ( int16_t  index,
const RowT &  newValue,
uint8_t  newRawLifeTime 
)
inlineprotectedvirtual

This method is called when valid row is set to blank space. It is called after count calculation is done, but before buffer is updated.

Parameters
indexIndex of row.
newValueReference to new value, to be inserted to row.
newRawLifeTimeNew raw life time value.

Reimplemented in MeshRouteTable< BufferSize >, and MeshRouteTable< ROUTETABLE_SIZE >.

◆ onRemove()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
virtual void MicroTable< RowT, RowsCount, IndexIsValue >::onRemove ( int16_t  index,
const RowT &  oldValue,
uint8_t  oldRawLifeTime,
bool  removedByValidation = false 
)
inlineprotectedvirtual

This method is called when valid row is removed. It is called after count calculation is done, but before buffer is updated.

Parameters
indexIndex of row.
oldValueReference to removed value.
oldRawLifeTimeOld raw life time value.
removedByValidationTrue if item was removed by validation.

Reimplemented in MeshRouteTable< BufferSize >, MeshRouteTable< ROUTETABLE_SIZE >, MeshOIDTable< BufferSize >, and MeshOIDTable< OIDTABLE_SIZE >.

◆ onRowLifeChanged()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
virtual void MicroTable< RowT, RowsCount, IndexIsValue >::onRowLifeChanged ( int16_t  index,
uint8_t  old_raw,
uint8_t  new_raw 
)
inlineprotectedvirtual

This method is called when table is cleared. It is called after cleaning is done, so all variables are fresh.

Parameters
indexIndex of changed row.
old_rawOld raw life time value (0-255).
new_rawNew raw life time value (0-255).

Reimplemented in MeshMACTable< BufferSize >.

◆ onSet()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
virtual void MicroTable< RowT, RowsCount, IndexIsValue >::onSet ( int16_t  index,
const RowT &  oldValue,
uint8_t  oldRawLifeTime,
const RowT &  newValue,
uint8_t  newRawLifeTime 
)
inlineprotectedvirtual

This method is called when valid row is set to new value. It is called after count calculation is done, but before buffer is updated.

Parameters
indexIndex of row.
oldValueReference to old value at index.
oldRawLifeTimeOld raw life time value.
newValueReference to new value, to be inserted to row.
newRawLifeTimeNew raw life time value.

Reimplemented in MeshOIDTable< BufferSize >, and MeshOIDTable< OIDTABLE_SIZE >.

◆ operator[]() [1/2]

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
RowT MicroTable< RowT, RowsCount, IndexIsValue >::operator[] ( int16_t  index)
inline

Gets row from table by index.

Parameters
indexIndex of row. It's value cannot be greather or equal than RowCount.
Returns
Returns copy of row.

◆ operator[]() [2/2]

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
const RowT& MicroTable< RowT, RowsCount, IndexIsValue >::operator[] ( int16_t  index) const
inline

Gets row from table by index.

Parameters
indexIndex of row. It's value cannot be greather or equal than RowCount.
Returns
Returns reference to row.

◆ print()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
size_t MicroTable< RowT, RowsCount, IndexIsValue >::print ( OSTREAM stream,
bool  showIndexes = true,
bool  doNotPrintBlank = true,
bool  showRemainingRowLifetime = true 
) const
inline

Prints items from table to stream.

Parameters
streamStream, where data will be printed.
showIndexesTrue to show indexes of items in printed table.
doNotPrintBlankDo not print blank row (when valid == 0).
showRemainingRowLifetimeTrue to show remaining row lifetime or false to show Valid value only.
Returns
Returns number of printed characters.

◆ printRaw()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
virtual size_t MicroTable< RowT, RowsCount, IndexIsValue >::printRaw ( OSTREAM stream,
const char  tableIdentifier[4] 
) const
inlinevirtual

Prints raw table data through stream. Those data can be decoded in PC or in another MCU.

Data structure

Byte Data name
0:3 Table identifier
4:5 Maximum rows count. See size() method.
6:9 Maximum row lifetime in milliseconds.
10:13 Time elapsed from last validation in milliseconds.
14:15 Used rows count. See count() method.
(loop: i = 0) Loop begin
16 + (i*sizeof(RowT+2)) Row's "i" index high byte
16 + (i*sizeof(RowT+2) + 1) Row's "i" index low byte
16 + (i*sizeof(RowT+2) + 2) Raw row value. Size of this value is sizeof(RowT)
16 + (i*sizeof(RowT+2) + x) Another bytes reserved for raw row value, if sizeof(RowT) > 1

... | last byte in each loop | Row's "i" raw life time value (i <= used row count) | Loop end with condition - do while loop last 4 bytes | CRC-32 (excluded bytes 0:3)

Parameters
streamStream where to print raw data.
tableIdentifierTable identifier, which consists of 3 characters.
Returns
Returns number of printed bytes.

◆ printTo()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
size_t MicroTable< RowT, RowsCount, IndexIsValue >::printTo ( Print &  p) const
inlineoverride

Makes this class printable.

Parameters
pStream, where to print data.
Returns
Returns number of printed characters.

◆ remove() [1/2]

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
bool MicroTable< RowT, RowsCount, IndexIsValue >::remove ( const RowT &  rowRef)
inline

Removes first row, which is equal to rowRef.

Parameters
rowRefRow reference.
Returns
Returns true when row was removed.

◆ remove() [2/2]

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
bool MicroTable< RowT, RowsCount, IndexIsValue >::remove ( int16_t  index)
inline

Removes row at index.

Parameters
indexIndex of row.
Returns
Returns true when row was removed.

◆ set() [1/2]

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
bool MicroTable< RowT, RowsCount, IndexIsValue >::set ( int16_t  index,
const RowT &  row 
)
inline

Sets row values in table. Row life time is set to maximum.

Parameters
indexIndex of row.
rowRow with values, that will be copied to specified row.
Returns
Returns true when row was set.

◆ set() [2/2]

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
bool MicroTable< RowT, RowsCount, IndexIsValue >::set ( int16_t  index,
const RowT &  row,
int16_t  rawLifeTime_ 
)
inline

Sets row values in table.

Parameters
indexIndex of row.
rowRow with values, that will be copied to specified row.
rawLifeTime_Life time of row (255 max - set in constructor, 1 min, 0 clear). If set to -1, old value is kept.
Returns
Returns true when row was set.

◆ setRawRemainingLifeTime()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
bool MicroTable< RowT, RowsCount, IndexIsValue >::setRawRemainingLifeTime ( int16_t  index,
uint8_t  rawLifeTime_ = 255 
)
inline

Sets row life time. Valid counter for this rowcan be changed, only when row is still valid.

Parameters
indexIndex of row.
rawLifeTime_New valid counter value (max 255, min 1)
Returns
Returns true when row's life time was set.

◆ size()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
int16_t MicroTable< RowT, RowsCount, IndexIsValue >::size ( ) const
inline

Gets capacity of table in rows.

Returns
Returns maximum number of rows in buffer.

◆ validate()

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
void MicroTable< RowT, RowsCount, IndexIsValue >::validate ( )
inline

Decreases all valid column values in specified interval (row lifetime). If valid value is 0, row will not be longer valid.

Note
To achieve exact timing, call this method minimally 255 times (in almost exact intervals) per maximum row lifetime.

Field Documentation

◆ buffer

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
RowT MicroTable< RowT, RowsCount, IndexIsValue >::buffer[RowsCount]
protected

◆ lastValidationTime

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
uint32_t MicroTable< RowT, RowsCount, IndexIsValue >::lastValidationTime = 0
protected

◆ lifeTime

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
uint8_t MicroTable< RowT, RowsCount, IndexIsValue >::lifeTime[RowsCount]
protected

◆ realUsedSize

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
int16_t MicroTable< RowT, RowsCount, IndexIsValue >::realUsedSize = 0
protected

◆ RowLife

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
const uint32_t MicroTable< RowT, RowsCount, IndexIsValue >::RowLife = 0
protected

◆ usedBufferSize

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
int16_t MicroTable< RowT, RowsCount, IndexIsValue >::usedBufferSize = 0
protected

◆ ValidationInterval

template<typename RowT , int16_t RowsCount, bool IndexIsValue = false>
const uint32_t MicroTable< RowT, RowsCount, IndexIsValue >::ValidationInterval = 0
protected

The documentation for this class was generated from the following file:
MicroTable::clear
void clear()
Removes all rows data.
Definition: meshTables.h:586
OSTREAM
#define OSTREAM
Definition: meshConfig.h:41