BleSerialPeripheralRK
Public Member Functions
RingBuffer< T > Class Template Reference

Thread and interrupt-safe (with caveats) circular buffer (ring buffer) class. More...

#include <RingBuffer.h>

Public Member Functions

 RingBuffer (T *elems, size_t size)
 Construct a buffer of size elements of T. More...
 
 ~RingBuffer ()
 Destructor.
 
size_t availableForRead () const
 Returns the number of elements that can be read right now (0 = nothing can be read right now) More...
 
T * preRead () const
 Non-copy version of read. Returns a pointer to the data to be read next or NULL if there is no data right now. More...
 
void postRead ()
 Indicates that you have finished reading the data in the pointer returned by preRead() and it can be reused. More...
 
bool read (T *elem)
 Read with copy. You can use this instead of preRead() and postRead(). More...
 
void readClear ()
 Clear outstanding entries, called from the read thread.
 
T * preWrite () const
 Non-copy version of write. Returns a pointer to the buffer to write to or NULL if there is no space. More...
 
void postWrite ()
 Commits the write. Only call if preWrite() returned a non-NULL value.
 
bool write (const T *elem)
 Write with copy. You can use this instead of preWrite() and postWrite(). elem is copied. More...
 

Detailed Description

template<class T>
class RingBuffer< T >

Thread and interrupt-safe (with caveats) circular buffer (ring buffer) class.

Home: https://github.com/rickkas7/SerialBufferRK License: MIT This class assumes a single reader thread and a single writer thread (or interrupt). For example, it works great if you read out of loop() and write from a single interrupt handler. It is not safe for multiple reader or multiple writer use cases!

Assumption: Writing a size_t value is atomic. It definitely is safe on ARM processors.

Constructor & Destructor Documentation

◆ RingBuffer()

template<class T>
RingBuffer< T >::RingBuffer ( T *  elems,
size_t  size 
)
inline

Construct a buffer of size elements of T.

Parameters
elemsPointer to a buffer of size elements of type T
sizeNumber of elements

Member Function Documentation

◆ availableForRead()

template<class T>
size_t RingBuffer< T >::availableForRead ( ) const
inline

Returns the number of elements that can be read right now (0 = nothing can be read right now)

This is mainly for informational purposes. It's more efficient to call preRead() and check for a non-NULL return value than it is to call availableForRead().

◆ postRead()

template<class T>
void RingBuffer< T >::postRead ( )
inline

Indicates that you have finished reading the data in the pointer returned by preRead() and it can be reused.

Only call postRead() if preRead() returned a non-null value!

◆ preRead()

template<class T>
T* RingBuffer< T >::preRead ( ) const
inline

Non-copy version of read. Returns a pointer to the data to be read next or NULL if there is no data right now.

If preRead() returns a non-null value you must call postRead() to consume the data, otherwise the next time you call preRead() you'll get the same data back. Don't call postRead() if you get NULL back from preRead()!

It's OK to not call postRead() if you're doing a peek at the data - look at the data that will be read without removing it.

◆ preWrite()

template<class T>
T* RingBuffer< T >::preWrite ( ) const
inline

Non-copy version of write. Returns a pointer to the buffer to write to or NULL if there is no space.

If preWrite() returns a non-null value you must call postWrite() to commit the data, otherwise the data will not be saved. Don't call postWrite() if you get NULL back from preWrite()!

◆ read()

template<class T>
bool RingBuffer< T >::read ( T *  elem)
inline

Read with copy. You can use this instead of preRead() and postRead().

Parameters
elemFilled in with the data that was read. Left unchanged if there's no data to be read.
Returns
Returns true if an element was copied or false if there was no data to read.

◆ write()

template<class T>
bool RingBuffer< T >::write ( const T *  elem)
inline

Write with copy. You can use this instead of preWrite() and postWrite(). elem is copied.

Returns
Returns true if the operation succeeded (there was space in the buffer).

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