SignalLogger
A library for signal logging.
signal_logger::Buffer< ValueType_ > Class Template Reference

Thread-safe circular buffer. More...

#include <Buffer.hpp>

Inheritance diagram for signal_logger::Buffer< ValueType_ >:
Inheritance graph

Classes

struct  is_buffer_default_type
 Default type trait. More...
 
struct  is_buffer_default_type< T, typename std::enable_if<!traits::is_eigen_matrix< T >::value >::type >
 

Public Types

typedef boost::circular_buffer< ValueType_ > circular_buffer_type
 Forward typedef as circular_buffer_type. More...
 
typedef circular_buffer_type::size_type size_type
 Forward typedef for size_type. More...
 

Public Member Functions

template<typename V = ValueType_>
 Buffer (const V *const ptr, const std::string &name, typename std::enable_if< is_buffer_default_type< V >::value >::type *=0)
 
template<typename V = ValueType_>
 Buffer (const V *const ptr, const std::string &name, typename std::enable_if< traits::is_eigen_matrix< V >::value >::type *=0)
 
void transfer (Buffer &other)
 basically a copy constructor however, the container entries are moved More...
 
bool collect ()
 Push data into the buffer (if looping or not full). More...
 
bool read (ValueType_ *pItem)
 
vector_type< ValueType_ > readNewValues ()
 
ValueType_ getElementCopyAtPosition (std::size_t idx) const
 
const ValueType_ *const getPointerAtPosition (std::size_t idx) const
 
void allocate (bool enabled)
 Allocate buffer size of memory. More...
 
virtual std::size_t getBufferSize () const
 
virtual void setBufferSize (std::size_t bufferSize)
 
virtual BufferType getBufferType () const
 
virtual void setBufferType (const BufferType bufferType)
 
virtual std::size_t noUnreadItems () const
 
virtual std::size_t noTotalItems () const
 
void clear ()
 Clear the buffer. More...
 
void resetUnreadItems ()
 Clear the buffer. More...
 

Private Member Functions

 Buffer (const Buffer &)
 Disabled copy constructor. More...
 
Bufferoperator= (const Buffer &)
 Disabled assign operator. More...
 
 Buffer (const ValueType_ *const ptr, const std::string &name, const std::size_t rows, const std::size_t cols)
 
template<typename V = ValueType_>
bool pushElementFront (const ValueType_ *const item, typename std::enable_if< is_buffer_default_type< V >::value >::type *=0)
 
template<typename V = ValueType_>
bool pushElementFront (const ValueType_ *const item, typename std::enable_if< traits::is_eigen_matrix< V >::value >::type *=0)
 

Private Attributes

const ValueType_ *const ptr_
 Pointer to value. More...
 
const std::string name_
 Name. More...
 
BufferType bufferType_
 Is the buffer "circulating", refreshing old entries with new ones. More...
 
std::size_t bufferSize_
 Size of the buffer (no elements, can differ from buffer capacity) More...
 
circular_buffer_type container_
 Circular buffer. More...
 
size_type noUnreadItems_
 Number of unread items. More...
 
size_type noItems_
 Number of items in the buffer (read and unread) More...
 
size_type noNewItems_
 Counter for readNewValues function. More...
 
const Eigen::Index rows_
 Eigen specific entries (1 in other cases) More...
 
const Eigen::Index cols_
 
std::mutex mutex_
 Mutex protecting accessing this container. More...
 

Detailed Description

template<typename ValueType_>
class signal_logger::Buffer< ValueType_ >

Thread-safe circular buffer.

Buffer that stores all elements in a circular manner. Allows looping, thus overwriting old entries with newer ones, keeps count of the unread items, push and pop influence this count. If buffer is empty no elements can be popped, if buffer is full and not looping no elements can be pushed. Popping does not delete the item, only sets unread item count . A copy of all the elements stored in the buffer (read and unread items) can be obtained. This implementation of a thread-safe circular buffer is adapted from here: http://www.boost.org/doc/libs/1_54_0/libs/circular_buffer/doc/circular_buffer.html#boundedbuffer

Member Typedef Documentation

◆ circular_buffer_type

template<typename ValueType_>
typedef boost::circular_buffer<ValueType_> signal_logger::Buffer< ValueType_ >::circular_buffer_type

Forward typedef as circular_buffer_type.

◆ size_type

template<typename ValueType_>
typedef circular_buffer_type::size_type signal_logger::Buffer< ValueType_ >::size_type

Forward typedef for size_type.

Constructor & Destructor Documentation

◆ Buffer() [1/4]

template<typename ValueType_>
template<typename V = ValueType_>
signal_logger::Buffer< ValueType_ >::Buffer ( const V *const  ptr,
const std::string &  name,
typename std::enable_if< is_buffer_default_type< V >::value >::type *  = 0 
)
inlineexplicit

Constructor for non-eigen-types

Parameters
ptrpointer to the data to be buffered

◆ Buffer() [2/4]

template<typename ValueType_>
template<typename V = ValueType_>
signal_logger::Buffer< ValueType_ >::Buffer ( const V *const  ptr,
const std::string &  name,
typename std::enable_if< traits::is_eigen_matrix< V >::value >::type *  = 0 
)
inlineexplicit

Constructor for eigen-types (initializes rows_ and cols_ correctly)

Parameters
ptrpointer to the data to be buffered

◆ Buffer() [3/4]

template<typename ValueType_>
signal_logger::Buffer< ValueType_ >::Buffer ( const Buffer< ValueType_ > &  )
private

Disabled copy constructor.

◆ Buffer() [4/4]

template<typename ValueType_>
signal_logger::Buffer< ValueType_ >::Buffer ( const ValueType_ *const  ptr,
const std::string &  name,
const std::size_t  rows,
const std::size_t  cols 
)
inlineprivate

Member Function Documentation

◆ allocate()

template<typename ValueType_>
void signal_logger::Buffer< ValueType_ >::allocate ( bool  enabled)
inline

Allocate buffer size of memory.

◆ clear()

template<typename ValueType_>
void signal_logger::Buffer< ValueType_ >::clear ( )
inlinevirtual

Clear the buffer.

Implements signal_logger::BufferInterface.

◆ collect()

template<typename ValueType_>
bool signal_logger::Buffer< ValueType_ >::collect ( )
inline

Push data into the buffer (if looping or not full).

◆ getBufferSize()

template<typename ValueType_>
virtual std::size_t signal_logger::Buffer< ValueType_ >::getBufferSize ( ) const
inlinevirtual
Returns
size of the buffer

Implements signal_logger::BufferInterface.

◆ getBufferType()

template<typename ValueType_>
virtual BufferType signal_logger::Buffer< ValueType_ >::getBufferType ( ) const
inlinevirtual
Returns
type of the buffer

Implements signal_logger::BufferInterface.

◆ getElementCopyAtPosition()

template<typename ValueType_>
ValueType_ signal_logger::Buffer< ValueType_ >::getElementCopyAtPosition ( std::size_t  idx) const
inline

Get copy of an item. Not decrementing unread items.

Parameters
idxindex of the stored value (idx'th newest value)
Returns
val Copy of value at idx

◆ getPointerAtPosition()

template<typename ValueType_>
const ValueType_* const signal_logger::Buffer< ValueType_ >::getPointerAtPosition ( std::size_t  idx) const
inline

Get const pointer to a const item. Not decrementing unread items.

Parameters
idxindex of the stored value (idx'th newest value)
Returns
val const Pointer to const value at idx

◆ noTotalItems()

template<typename ValueType_>
virtual std::size_t signal_logger::Buffer< ValueType_ >::noTotalItems ( ) const
inlinevirtual
Returns
number of items stored in the buffer (read and unread)

Implements signal_logger::BufferInterface.

◆ noUnreadItems()

template<typename ValueType_>
virtual std::size_t signal_logger::Buffer< ValueType_ >::noUnreadItems ( ) const
inlinevirtual
Returns
number of unread elements

Implements signal_logger::BufferInterface.

◆ operator=()

template<typename ValueType_>
Buffer& signal_logger::Buffer< ValueType_ >::operator= ( const Buffer< ValueType_ > &  )
private

Disabled assign operator.

◆ pushElementFront() [1/2]

template<typename ValueType_>
template<typename V = ValueType_>
bool signal_logger::Buffer< ValueType_ >::pushElementFront ( const ValueType_ *const  item,
typename std::enable_if< is_buffer_default_type< V >::value >::type *  = 0 
)
inlineprivate

Push an element at front for default types

Parameters
itemitem to push at front
Returns
template specialization by return type

◆ pushElementFront() [2/2]

template<typename ValueType_>
template<typename V = ValueType_>
bool signal_logger::Buffer< ValueType_ >::pushElementFront ( const ValueType_ *const  item,
typename std::enable_if< traits::is_eigen_matrix< V >::value >::type *  = 0 
)
inlineprivate

Push an element at front for eigen types

Parameters
itemitem to push at front of underlying-type buffer
Returns
template specialization by return type

◆ read()

template<typename ValueType_>
bool signal_logger::Buffer< ValueType_ >::read ( ValueType_ *  pItem)
inline

Read an item from the buffer (if buffer not empty). Decrement no unread items.

Parameters
pItempointer to value in which popped value is stored

◆ readNewValues()

template<typename ValueType_>
vector_type<ValueType_> signal_logger::Buffer< ValueType_ >::readNewValues ( )
inline

Read a vector of values collected since the last call to this function from the buffer (if buffer not empty). Decrement no new items.

Returns
vec vector of all new items since last call to this function

◆ resetUnreadItems()

template<typename ValueType_>
void signal_logger::Buffer< ValueType_ >::resetUnreadItems ( )
inline

Clear the buffer.

◆ setBufferSize()

template<typename ValueType_>
virtual void signal_logger::Buffer< ValueType_ >::setBufferSize ( std::size_t  bufferSize)
inlinevirtual
Parameters
desiredsize of the buffer

Implements signal_logger::BufferInterface.

◆ setBufferType()

template<typename ValueType_>
virtual void signal_logger::Buffer< ValueType_ >::setBufferType ( const BufferType  bufferType)
inlinevirtual
Parameters
desiredsize of the buffer

Implements signal_logger::BufferInterface.

◆ transfer()

template<typename ValueType_>
void signal_logger::Buffer< ValueType_ >::transfer ( Buffer< ValueType_ > &  other)
inline

basically a copy constructor however, the container entries are moved

transfer to new buffer

Parameters
otherLvalue to other buffer

Member Data Documentation

◆ bufferSize_

template<typename ValueType_>
std::size_t signal_logger::Buffer< ValueType_ >::bufferSize_
private

Size of the buffer (no elements, can differ from buffer capacity)

◆ bufferType_

template<typename ValueType_>
BufferType signal_logger::Buffer< ValueType_ >::bufferType_
private

Is the buffer "circulating", refreshing old entries with new ones.

◆ cols_

template<typename ValueType_>
const Eigen::Index signal_logger::Buffer< ValueType_ >::cols_
private

◆ container_

template<typename ValueType_>
circular_buffer_type signal_logger::Buffer< ValueType_ >::container_
private

Circular buffer.

◆ mutex_

template<typename ValueType_>
std::mutex signal_logger::Buffer< ValueType_ >::mutex_
mutableprivate

Mutex protecting accessing this container.

◆ name_

template<typename ValueType_>
const std::string signal_logger::Buffer< ValueType_ >::name_
private

Name.

◆ noItems_

template<typename ValueType_>
size_type signal_logger::Buffer< ValueType_ >::noItems_
private

Number of items in the buffer (read and unread)

◆ noNewItems_

template<typename ValueType_>
size_type signal_logger::Buffer< ValueType_ >::noNewItems_
private

Counter for readNewValues function.

◆ noUnreadItems_

template<typename ValueType_>
size_type signal_logger::Buffer< ValueType_ >::noUnreadItems_
private

Number of unread items.

◆ ptr_

template<typename ValueType_>
const ValueType_* const signal_logger::Buffer< ValueType_ >::ptr_
private

Pointer to value.

◆ rows_

template<typename ValueType_>
const Eigen::Index signal_logger::Buffer< ValueType_ >::rows_
private

Eigen specific entries (1 in other cases)


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