MeshNet  1.0.0
Public Member Functions | Protected Member Functions | Protected Attributes
MicroFIFO< T, BufferSize > Class Template Reference

Optimized FIFO buffer for microcontrolers. More...

#include <meshBuffers.h>

Inheritance diagram for MicroFIFO< T, BufferSize >:
Inheritance graph
[legend]
Collaboration diagram for MicroFIFO< T, BufferSize >:
Collaboration graph
[legend]

Public Member Functions

const T & front () const
 Gets the first (oldest) item from the buffer. More...
 
T & front ()
 Gets the first (oldest) item from the buffer. More...
 
const T & back () const
 Gets the last (newest) item from the buffer. More...
 
T & back ()
 Gets the last (newest) item from the buffer. More...
 
bool get (T &item, uint8_t index) const
 Gets item from the buffer by index. More...
 
const T & operator[] (int index) const
 Gets item from the buffer by index. More...
 
T & operator[] (int index)
 Gets item from the buffer by index. More...
 
void push (T const &item)
 Pushes item after last item in buffer. When buffer is full, first (oldest) item will be overwritten and lostItemsCount() will be incremented by 1. More...
 
void push (T &&item)
 Pushes item after last item in buffer. When buffer is full, first (oldest) item will be overwritten and lostItemsCount() will be incremented by 1. More...
 
T && popMove ()
 Reads and removes first (oldest) item from the buffer. When buffer is empty, exception may be thrown. This method is recommended to use only if std::move will be used. More...
 
bool pop ()
 Removes first (oldest) item from the buffer. More...
 
bool empty () const
 Checks if buffer is empty. More...
 
uint8_t count () const
 Gets how many items are stored in buffer. More...
 
const uint8_t size () const
 Gets maximum count of items, that can be stored in buffer. More...
 
uint8_t lostItemsCount () const
 
void restartLostItemsCounter ()
 Restarts lostItemsCount counter. More...
 
bool removeAt (uint8_t index)
 Removes item at specified index. After removing, count() will return new value. More...
 
uint8_t removeMulti (uint8_t startPos_, uint8_t itmCount)
 Removes number of item from specified index. After removing, count() will return new value. More...
 
uint8_t removeIf (bool(*dec)(const T &))
 Removes specified items. More...
 
void clear ()
 Clears whole buffer. More...
 
size_t getRawDataSize () const
 Calculates, how many bytes will be printed, when using command printRaw. More...
 
virtual size_t printRaw (OSTREAM &stream, const char *customTag)
 Prints raw FIFO data through stream. Those data can be decoded in PC or in another MCU. More...
 
virtual size_t print (OSTREAM &stream, bool showIndexes=true) const
 Prints items as table from FIFO buffer to stream. More...
 
virtual size_t sizeOf () const
 Gets size of the whole FIFO in bytes. More...
 
size_t printTo (Print &p) const override
 Makes this class printable. More...
 

Protected Member Functions

int16_t getRawIndex (uint8_t index) const
 Gets raw index in buffer from index. Index starts from startPos. Raw index starts from the beginning of the buffer. More...
 
const T * getRowPtr (uint8_t index) const
 Gets pointer to the item in buffer by index. More...
 
T * getRowPtr (uint8_t index)
 Gets pointer to the item in buffer by index. More...
 
bool isRawIndexOccupied (uint8_t rawIndex) const
 Checks if there is item stored at raw index. More...
 

Protected Attributes

buffer [BufferSize]
 
uint8_t startPos = 0
 
uint8_t endPos = 0
 
uint8_t usedSize = 0
 
uint8_t lostItems = 0
 

Detailed Description

template<typename T, uint8_t BufferSize>
class MicroFIFO< T, BufferSize >

Optimized FIFO buffer for microcontrolers.

Size of the FIFO buffer in this example is 255 rows with type MeshMessage.

How data are stored:

index (not physically stored) Item [T]
0 XXX
1 XXX
... ...
XXX - depends on BufferSize XXX

Member Function Documentation

◆ back() [1/2]

template<typename T , uint8_t BufferSize>
T& MicroFIFO< T, BufferSize >::back ( )
inline

Gets the last (newest) item from the buffer.

Note
Always check, if there is at least one available item in the buffer, because the exception can be thrown.
Returns
Returns the last (newest) item from the buffer.

◆ back() [2/2]

template<typename T , uint8_t BufferSize>
const T& MicroFIFO< T, BufferSize >::back ( ) const
inline

Gets the last (newest) item from the buffer.

Note
Always check, if there is at least one available item in the buffer, because the exception can be thrown.
Returns
Returns the last (newest) item from the buffer.

◆ clear()

template<typename T , uint8_t BufferSize>
void MicroFIFO< T, BufferSize >::clear ( )
inline

Clears whole buffer.

◆ count()

template<typename T , uint8_t BufferSize>
uint8_t MicroFIFO< T, BufferSize >::count ( ) const
inline

Gets how many items are stored in buffer.

Returns
Returns how many items are stored in buffer.

◆ empty()

template<typename T , uint8_t BufferSize>
bool MicroFIFO< T, BufferSize >::empty ( ) const
inline

Checks if buffer is empty.

Returns
Returns true when buffer is empty.

◆ front() [1/2]

template<typename T , uint8_t BufferSize>
T& MicroFIFO< T, BufferSize >::front ( )
inline

Gets the first (oldest) item from the buffer.

Note
Always check, if there is at least one available item in the buffer, because the exception can be thrown.
Returns
Returns the first (oldest) item from the buffer.

◆ front() [2/2]

template<typename T , uint8_t BufferSize>
const T& MicroFIFO< T, BufferSize >::front ( ) const
inline

Gets the first (oldest) item from the buffer.

Note
Always check, if there is at least one available item in the buffer, because the exception can be thrown.
Returns
Returns the first (oldest) item from the buffer.

◆ get()

template<typename T , uint8_t BufferSize>
bool MicroFIFO< T, BufferSize >::get ( T &  item,
uint8_t  index 
) const
inline

Gets item from the buffer by index.

Parameters
itemReference to item, that will be filled by data from buffer.
indexIndex of item to get. Index 0 is the first (oldest) item from the buffer, same as using front() method.
Returns
Returns true when item was read and false, when there is no available item at specified index.

◆ getRawDataSize()

template<typename T , uint8_t BufferSize>
size_t MicroFIFO< T, BufferSize >::getRawDataSize ( ) const
inline

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

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

◆ getRawIndex()

template<typename T , uint8_t BufferSize>
int16_t MicroFIFO< T, BufferSize >::getRawIndex ( uint8_t  index) const
inlineprotected

Gets raw index in buffer from index. Index starts from startPos. Raw index starts from the beginning of the buffer.

Parameters
indexIndex to convert.
Returns
Returns converted index to raw index.

◆ getRowPtr() [1/2]

template<typename T , uint8_t BufferSize>
T* MicroFIFO< T, BufferSize >::getRowPtr ( uint8_t  index)
inlineprotected

Gets pointer to the item in buffer by index.

Parameters
indexIndex of the item to get.
Returns
Returns pointer to the item in buffer or NULL when there is no item at specified index.

◆ getRowPtr() [2/2]

template<typename T , uint8_t BufferSize>
const T* MicroFIFO< T, BufferSize >::getRowPtr ( uint8_t  index) const
inlineprotected

Gets pointer to the item in buffer by index.

Parameters
indexIndex of the item to get.
Returns
Returns pointer to the item in buffer or NULL when there is no item at specified index.

◆ isRawIndexOccupied()

template<typename T , uint8_t BufferSize>
bool MicroFIFO< T, BufferSize >::isRawIndexOccupied ( uint8_t  rawIndex) const
inlineprotected

Checks if there is item stored at raw index.

Parameters
Rawindex, where to check if item is stored here. Raw index starts from the beginning of the buffer.
Returns
Returns true when there is item stored at raw index.

◆ lostItemsCount()

template<typename T , uint8_t BufferSize>
uint8_t MicroFIFO< T, BufferSize >::lostItemsCount ( ) const
inline

@breif Gets how many items were lost (overwrote).

Returns
Returns how many items were lost (overwrote).

◆ operator[]() [1/2]

template<typename T , uint8_t BufferSize>
T& MicroFIFO< T, BufferSize >::operator[] ( int  index)
inline

Gets item from the buffer by index.

Note
Always check, if there is at available item in the buffer at specified index, because the exception can be thrown.
Parameters
indexIndex of item to get. Index 0 is the first (oldest) item from the buffer, same as using front() method.

◆ operator[]() [2/2]

template<typename T , uint8_t BufferSize>
const T& MicroFIFO< T, BufferSize >::operator[] ( int  index) const
inline

Gets item from the buffer by index.

Note
Always check, if there is at available item in the buffer at specified index, because the exception can be thrown.
Parameters
indexIndex of item to get. Index 0 is the first (oldest) item from the buffer, same as using front() method.

◆ pop()

template<typename T , uint8_t BufferSize>
bool MicroFIFO< T, BufferSize >::pop ( )
inline

Removes first (oldest) item from the buffer.

Note
To read first (oldest) item from this buffer use front() method before pop().
Returns
Returns true if item was removed and false if not.

◆ popMove()

template<typename T , uint8_t BufferSize>
T&& MicroFIFO< T, BufferSize >::popMove ( )
inline

Reads and removes first (oldest) item from the buffer. When buffer is empty, exception may be thrown. This method is recommended to use only if std::move will be used.

Note
Always check, if there is at least one available item in the buffer, because the exception can be thrown.
Returns
Returns first (oldest) item from the buffer.

◆ print()

template<typename T , uint8_t BufferSize>
virtual size_t MicroFIFO< T, BufferSize >::print ( OSTREAM stream,
bool  showIndexes = true 
) const
inlinevirtual

Prints items as table from FIFO buffer to stream.

Parameters
streamStream, where data will be printed.
showIndexesTrue to show indexes of items in printed table.
Returns
Returns number of printed characters.

◆ printRaw()

template<typename T , uint8_t BufferSize>
virtual size_t MicroFIFO< T, BufferSize >::printRaw ( OSTREAM stream,
const char *  customTag 
)
inlinevirtual

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

Data structure

Byte Data Data name
0-3 "MFF:" Data label (Micro FIFO).
4-7 Custom tag
7 Allocated buffer size (Max number of rows).
8 Size of one row in bytes. (255 = unknown, size of each row has to be stored at the begining of row data)
9 Used rows count (length and CRC bytes not included).
10.....x Raw data
last 4 bytes CRC-32 (excluded 0-3 bytes)
Parameters
streamStream where to print raw data.
customTagCustom data type tag. Maximum length is 4 characters.
Returns
Returns number of printed bytes.

◆ printTo()

template<typename T , uint8_t BufferSize>
size_t MicroFIFO< T, BufferSize >::printTo ( Print &  p) const
inlineoverride

Makes this class printable.

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

◆ push() [1/2]

template<typename T , uint8_t BufferSize>
void MicroFIFO< T, BufferSize >::push ( T &&  item)
inline

Pushes item after last item in buffer. When buffer is full, first (oldest) item will be overwritten and lostItemsCount() will be incremented by 1.

Note
If Type supports moving, it value will be moved.
Parameters
itemItem to push into buffer.

◆ push() [2/2]

template<typename T , uint8_t BufferSize>
void MicroFIFO< T, BufferSize >::push ( T const &  item)
inline

Pushes item after last item in buffer. When buffer is full, first (oldest) item will be overwritten and lostItemsCount() will be incremented by 1.

Parameters
itemItem to push into buffer.

◆ removeAt()

template<typename T , uint8_t BufferSize>
bool MicroFIFO< T, BufferSize >::removeAt ( uint8_t  index)
inline

Removes item at specified index. After removing, count() will return new value.

Optimizations

Some optimizations are done here to reduce remove time.

  1. std::Move is used to move rows. This can reduce moving time, because no allocation is needed when items in FIFO has allocated space.
  2. FIFO is splitted to half before removing. If index is in lower half, rows from start until index are moved up. If index is in higher half, rows from end until index are moved down. This can reduce complexity to half of it's value.
    Note
    This operation can take some time, because some rows has to be moved, that's why it is not recommended to call it often.
    Parameters
    indexIndex of item to remove.
    Returns
    Returns true if item was removed.

◆ removeIf()

template<typename T , uint8_t BufferSize>
uint8_t MicroFIFO< T, BufferSize >::removeIf ( bool(*)(const T &)  dec)
inline

Removes specified items.

Optimizations

Some optimizations are done here to reduce remove time.

  1. std::Move is used to move rows. This can reduce moving time, because no allocation is needed when items in FIFO has allocated space.
    Note
    This operation can take some time, because some rows has to be moved, that's why it is not recommended to call it often.
    Parameters
    decFunction, which returns true to remove item.
    Returns
    Returns count of removed items.

◆ removeMulti()

template<typename T , uint8_t BufferSize>
uint8_t MicroFIFO< T, BufferSize >::removeMulti ( uint8_t  startPos_,
uint8_t  itmCount 
)
inline

Removes number of item from specified index. After removing, count() will return new value.

Optimizations

Some optimizations are done here to reduce remove time.

  1. std::Move is used to move rows. This can reduce moving time, because no allocation is needed when items in FIFO has allocated space.
  2. FIFO is splitted to half before removing. If the most of removed part is in lower half, rows from start until index are moved up. If the most of removed part is in higher half, rows from end until index are moved down. This can reduce complexity to half of it's value.
    Note
    This operation can take some time, because some rows has to be moved, that's why it is not recommended to call it often.
    Parameters
    startPos_Index of first item to remove.
    itmCountCount of items to remove (including first one).
    Returns
    Returns count of removed items.

◆ restartLostItemsCounter()

template<typename T , uint8_t BufferSize>
void MicroFIFO< T, BufferSize >::restartLostItemsCounter ( )
inline

Restarts lostItemsCount counter.

◆ size()

template<typename T , uint8_t BufferSize>
const uint8_t MicroFIFO< T, BufferSize >::size ( ) const
inline

Gets maximum count of items, that can be stored in buffer.

Returns
Returns maximum count of items, that can be stored in buffer.

◆ sizeOf()

template<typename T , uint8_t BufferSize>
virtual size_t MicroFIFO< T, BufferSize >::sizeOf ( ) const
inlinevirtual

Gets size of the whole FIFO in bytes.

Returns
Returns size of the whole FIFO in bytes.

Field Documentation

◆ buffer

template<typename T , uint8_t BufferSize>
T MicroFIFO< T, BufferSize >::buffer[BufferSize]
protected

◆ endPos

template<typename T , uint8_t BufferSize>
uint8_t MicroFIFO< T, BufferSize >::endPos = 0
protected

◆ lostItems

template<typename T , uint8_t BufferSize>
uint8_t MicroFIFO< T, BufferSize >::lostItems = 0
protected

◆ startPos

template<typename T , uint8_t BufferSize>
uint8_t MicroFIFO< T, BufferSize >::startPos = 0
protected

◆ usedSize

template<typename T , uint8_t BufferSize>
uint8_t MicroFIFO< T, BufferSize >::usedSize = 0
protected

The documentation for this class was generated from the following file:
MicroFIFO
Optimized FIFO buffer for microcontrolers.
Definition: meshBuffers.h:36