MADARA  3.4.1
madara::knowledge::containers::CircularBufferConsumer Class Reference

This container stores a thread-safe, personalized consumer for CircularBuffer instances. More...

#include <CircularBufferConsumer.h>

Public Member Functions

 CircularBufferConsumer ()
 Default constructor. More...
 
 CircularBufferConsumer (const std::string &name, KnowledgeBase &knowledge)
 Constructor. More...
 
 CircularBufferConsumer (const std::string &name, Variables &knowledge)
 Constructor. More...
 
virtual ~CircularBufferConsumer ()=default
 Destructor. More...
 
KnowledgeRecord consume (size_t &dropped) const
 Consumes the record at the local index (not the producer index) More...
 
template<typename T >
void consume (T &value) const
 Consumes the record at the local index (not the producer index) More...
 
template<typename T >
void consume (T &value, size_t &dropped) const
 Consumes the record at the local index (not the producer index) More...
 
KnowledgeRecord consume (void) const
 Consumes the record at the local index (not the producer index) More...
 
std::vector< KnowledgeRecordconsume_earliest (size_t count) const
 Consumes earliest records from the local index in the buffer. More...
 
std::vector< KnowledgeRecordconsume_earliest (size_t count, size_t &dropped) const
 Consumes earliest records from the local index in the buffer. More...
 
template<typename T >
void consume_earliest (size_t count, std::vector< T > &values) const
 Consumes earliest records from the local index in the buffer. More...
 
template<typename T >
void consume_earliest (size_t count, std::vector< T > &values, size_t &dropped) const
 Consumes earliest records from the local index in the buffer. More...
 
std::vector< KnowledgeRecordconsume_latest (size_t count) const
 Consumes the record at the local index (not the producer index) More...
 
std::vector< KnowledgeRecordconsume_latest (size_t count, size_t &dropped) const
 Consumes latest records added by the producer and updates the local index to the producer index. More...
 
template<typename T >
void consume_latest (size_t count, std::vector< T > &values) const
 Consumes latest records added by the producer and updates the local index to the producer index. More...
 
template<typename T >
void consume_latest (size_t count, std::vector< T > &values, size_t &dropped) const
 Consumes latest records added by the producer and updates the local index to the producer index. More...
 
size_t count (void) const
 Returns the number of records in the CircularBufferConsumer. More...
 
size_t get_dropped (void) const
 Returns the number of known drops since last consume. More...
 
std::string get_name (void) const
 Returns the name of the variable. More...
 
knowledge::KnowledgeRecord inspect (KnowledgeRecord::Integer position) const
 Retrieves a record at a position relative to local index. More...
 
std::vector< KnowledgeRecordinspect (KnowledgeRecord::Integer position, size_t count) const
 Retrieves a vector of records at a position relative to local index. More...
 
template<typename T >
void inspect (KnowledgeRecord::Integer position, size_t count, std::vector< T > &values) const
 Retrieves a vector of records at a position relative to local index. More...
 
template<typename T >
void inspect (KnowledgeRecord::Integer position, T &value) const
 Retrieves a record at a position relative to local index. More...
 
bool operator!= (const CircularBufferConsumer &value) const
 Checks for inequality. More...
 
bool operator== (const CircularBufferConsumer &value) const
 Checks for equality. More...
 
std::vector< KnowledgeRecordpeek_latest (size_t count) const
 Peeks at the most recently added records. More...
 
template<typename T >
void peek_latest (size_t count, std::vector< T > &values) const
 Peeks at the most recently added records. More...
 
template<typename T >
void peek_latest (T &value) const
 Peeks at the most recently added value. More...
 
knowledge::KnowledgeRecord peek_latest (void) const
 Peeks at the most recently added record. More...
 
size_t remaining (void) const
 Returns the number of records remaining that have not been consumed with get_latest, get, or get_earliest. More...
 
void resize (void)
 Resizes the buffer size to the producer's buffer size. More...
 
void resync (void)
 Sets the local index to the current buffer index. More...
 
void set_index (KnowledgeRecord::Integer index)
 Sets the local index to an arbitrary position. More...
 
void set_name (const std::string &name, KnowledgeBase &knowledge)
 Sets the variable name that this refers to. More...
 
void set_name (const std::string &name, Variables &knowledge)
 Sets the variable name that this refers to. More...
 
size_t size (void) const
 Returns the maximum size of the CircularBufferConsumer. More...
 

Private Member Functions

void check_all (const char *func) const
 
void check_context (const char *func) const
 
void check_name (const char *func) const
 
KnowledgeRecord::Integer increment (KnowledgeRecord::Integer base, KnowledgeRecord::Integer value) const
 Increments the base by the value, using size as a boundary. More...
 

Private Attributes

Vector buffer_
 Underlying array of records. More...
 
ThreadSafeContextcontext_
 guard for access and changes More...
 
Integer index_
 Index for latest item in circular buffer. More...
 
KnowledgeRecord::Integer local_index_
 Index for latest item read by. More...
 
std::string name_
 Prefix of variable. More...
 

Detailed Description

This container stores a thread-safe, personalized consumer for CircularBuffer instances.

Unlike functions in CircularBuffer, almost all operations in CircularBufferConsumer use a local index into the CircularBuffer and encourage one-time access of buffer elements.

Definition at line 34 of file CircularBufferConsumer.h.

Constructor & Destructor Documentation

◆ CircularBufferConsumer() [1/3]

madara::knowledge::containers::CircularBufferConsumer::CircularBufferConsumer ( )
inline

Default constructor.

Definition at line 20 of file CircularBufferConsumer.inl.

◆ CircularBufferConsumer() [2/3]

madara::knowledge::containers::CircularBufferConsumer::CircularBufferConsumer ( const std::string &  name,
KnowledgeBase knowledge 
)
inline

Constructor.

Parameters
namename of the integer in the knowledge base
knowledgethe knowledge base that will contain the vector
Exceptions
exceptions::NameExceptionbad name ("")

Definition at line 79 of file CircularBufferConsumer.inl.

◆ CircularBufferConsumer() [3/3]

madara::knowledge::containers::CircularBufferConsumer::CircularBufferConsumer ( const std::string &  name,
Variables knowledge 
)
inline

Constructor.

Parameters
namethe name of the map within the variable context
knowledgethe variable context
Exceptions
exceptions::NameExceptionbad name ("")

Definition at line 89 of file CircularBufferConsumer.inl.

◆ ~CircularBufferConsumer()

virtual madara::knowledge::containers::CircularBufferConsumer::~CircularBufferConsumer ( )
virtualdefault

Destructor.

Member Function Documentation

◆ check_all()

void madara::knowledge::containers::CircularBufferConsumer::check_all ( const char *  func) const
inlineprivate

Definition at line 44 of file CircularBufferConsumer.inl.

◆ check_context()

void madara::knowledge::containers::CircularBufferConsumer::check_context ( const char *  func) const
inlineprivate

Definition at line 34 of file CircularBufferConsumer.inl.

◆ check_name()

void madara::knowledge::containers::CircularBufferConsumer::check_name ( const char *  func) const
inlineprivate

Definition at line 25 of file CircularBufferConsumer.inl.

◆ consume() [1/4]

madara::knowledge::KnowledgeRecord madara::knowledge::containers::CircularBufferConsumer::consume ( size_t &  dropped) const
inline

Consumes the record at the local index (not the producer index)

Returns
the last added record. exists() will return false if the record is invalid
Parameters
droppedthe number of dropped packets. Drops can occur when the producer produces faster than the consumer can consume. This value is essentially index_ - local_index - size ().
Exceptions
exceptions::IndexExceptionbuffer has zero size

Definition at line 159 of file CircularBufferConsumer.inl.

◆ consume() [2/4]

template<typename T >
void madara::knowledge::containers::CircularBufferConsumer::consume ( T &  value) const

Consumes the record at the local index (not the producer index)

Parameters
valuethe last added value.
Exceptions
exceptions::IndexExceptionif no data to consume

◆ consume() [3/4]

template<typename T >
void madara::knowledge::containers::CircularBufferConsumer::consume ( T &  value,
size_t &  dropped 
) const

Consumes the record at the local index (not the producer index)

Parameters
valuethe last added value.
droppedthe number of dropped packets. Drops can occur when the producer produces faster than the consumer can consume. This value is essentially index_ - local_index - size ().
Exceptions
exceptions::IndexExceptionif no data to consume

◆ consume() [4/4]

madara::knowledge::KnowledgeRecord madara::knowledge::containers::CircularBufferConsumer::consume ( void  ) const
inline

Consumes the record at the local index (not the producer index)

Returns
the last added record. exists() will return false if the record is invalid
Exceptions
exceptions::IndexExceptionbuffer has zero size

Definition at line 134 of file CircularBufferConsumer.inl.

◆ consume_earliest() [1/4]

std::vector< KnowledgeRecord > madara::knowledge::containers::CircularBufferConsumer::consume_earliest ( size_t  count) const
inline

Consumes earliest records from the local index in the buffer.

Parameters
countthe maximum number of records to return
Returns
the last added records
Exceptions
exceptions::IndexExceptionif index is unreachable

Definition at line 415 of file CircularBufferConsumer.inl.

◆ consume_earliest() [2/4]

std::vector< KnowledgeRecord > madara::knowledge::containers::CircularBufferConsumer::consume_earliest ( size_t  count,
size_t &  dropped 
) const
inline

Consumes earliest records from the local index in the buffer.

Parameters
countthe maximum number of records to return
droppedthe number of dropped packets. Drops can occur when the producer produces faster than the consumer can consume. This value is essentially index_ - local_index - size ().
Returns
the last added records
Exceptions
exceptions::IndexExceptionif index is unreachable

Definition at line 450 of file CircularBufferConsumer.inl.

◆ consume_earliest() [3/4]

template<typename T >
void madara::knowledge::containers::CircularBufferConsumer::consume_earliest ( size_t  count,
std::vector< T > &  values 
) const

Consumes earliest records from the local index in the buffer.

Parameters
countthe maximum number of records to return
valuesthe last added records
Exceptions
exceptions::IndexExceptionif index is unreachable

◆ consume_earliest() [4/4]

template<typename T >
void madara::knowledge::containers::CircularBufferConsumer::consume_earliest ( size_t  count,
std::vector< T > &  values,
size_t &  dropped 
) const

Consumes earliest records from the local index in the buffer.

Parameters
countthe maximum number of records to return
valuesthe last added records
droppedthe number of dropped packets. Drops can occur when the producer produces faster than the consumer can consume. This value is essentially index_ - local_index - size ().
Exceptions
exceptions::IndexExceptionif index is unreachable

◆ consume_latest() [1/4]

std::vector< KnowledgeRecord > madara::knowledge::containers::CircularBufferConsumer::consume_latest ( size_t  count) const
inline

Consumes the record at the local index (not the producer index)

Parameters
countthe maximum number of records to return
Returns
the last added records
Exceptions
exceptions::IndexExceptionif index is unreachable

Definition at line 365 of file CircularBufferConsumer.inl.

◆ consume_latest() [2/4]

std::vector< KnowledgeRecord > madara::knowledge::containers::CircularBufferConsumer::consume_latest ( size_t  count,
size_t &  dropped 
) const
inline

Consumes latest records added by the producer and updates the local index to the producer index.

Parameters
countthe maximum number of records to return
droppedthe number of dropped packets. Drops can occur when the producer produces faster than the consumer can consume. This value is essentially index_ - local_index - size ().
Returns
the last added records
Exceptions
exceptions::IndexExceptionif index is unreachable

Definition at line 389 of file CircularBufferConsumer.inl.

◆ consume_latest() [3/4]

template<typename T >
void madara::knowledge::containers::CircularBufferConsumer::consume_latest ( size_t  count,
std::vector< T > &  values 
) const

Consumes latest records added by the producer and updates the local index to the producer index.

Parameters
countthe maximum number of records to return
valuesthe last added records
Exceptions
exceptions::IndexExceptionif index is unreachable

◆ consume_latest() [4/4]

template<typename T >
void madara::knowledge::containers::CircularBufferConsumer::consume_latest ( size_t  count,
std::vector< T > &  values,
size_t &  dropped 
) const

Consumes latest records added by the producer and updates the local index to the producer index.

Parameters
countthe maximum number of records to return
valuesthe last added records
droppedthe number of dropped packets. Drops can occur when the producer produces faster than the consumer can consume. This value is essentially index_ - local_index - size ().
Exceptions
exceptions::IndexExceptionif index is unreachable

◆ count()

size_t madara::knowledge::containers::CircularBufferConsumer::count ( void  ) const
inline

Returns the number of records in the CircularBufferConsumer.

Returns
the number of records in the CircularBufferConsumer
Exceptions
exceptions::ContextExceptionif name or context haven't been set appropriately

Definition at line 286 of file CircularBufferConsumer.inl.

◆ get_dropped()

size_t madara::knowledge::containers::CircularBufferConsumer::get_dropped ( void  ) const
inline

Returns the number of known drops since last consume.

Returns
the number of drops

Definition at line 480 of file CircularBufferConsumer.inl.

◆ get_name()

std::string madara::knowledge::containers::CircularBufferConsumer::get_name ( void  ) const
inline

Returns the name of the variable.

Returns
name of the variable

Definition at line 267 of file CircularBufferConsumer.inl.

◆ increment()

madara::knowledge::KnowledgeRecord::Integer madara::knowledge::containers::CircularBufferConsumer::increment ( KnowledgeRecord::Integer  base,
KnowledgeRecord::Integer  value 
) const
inlineprivate

Increments the base by the value, using size as a boundary.

Parameters
basethe base value to increment
valuethe value to increment base by
Returns
the resulting index

Definition at line 112 of file CircularBufferConsumer.inl.

◆ inspect() [1/4]

madara::knowledge::KnowledgeRecord madara::knowledge::containers::CircularBufferConsumer::inspect ( KnowledgeRecord::Integer  position) const
inline

Retrieves a record at a position relative to local index.

Parameters
positionthe relative position of the requested record from the latest added record. Can be negative
Returns
the record at the position in the CircularBufferConsumer
Exceptions
exceptions::ContextExceptionif name or context haven't been set appropriately
exceptions::IndexExceptionif index is out of range/invalid

Definition at line 186 of file CircularBufferConsumer.inl.

◆ inspect() [2/4]

std::vector< KnowledgeRecord > madara::knowledge::containers::CircularBufferConsumer::inspect ( KnowledgeRecord::Integer  position,
size_t  count 
) const
inline

Retrieves a vector of records at a position relative to local index.

Parameters
positionthe relative position of the requested record from the latest added record. Can be negative
countthe maximum number of records to return
Returns
the values at the position in the CircularBufferConsumer
Exceptions
exceptions::ContextExceptionif name or context haven't been set appropriately
exceptions::IndexExceptionif index is out of range/invalid

Definition at line 223 of file CircularBufferConsumer.inl.

◆ inspect() [3/4]

template<typename T >
void madara::knowledge::containers::CircularBufferConsumer::inspect ( KnowledgeRecord::Integer  position,
size_t  count,
std::vector< T > &  values 
) const

Retrieves a vector of records at a position relative to local index.

Parameters
positionthe relative position of the requested record from the latest added record. Can be negative
countthe maximum number of records to return
valuesthe values at the position in the CircularBufferConsumer
Exceptions
exceptions::ContextExceptionif name or context haven't been set appropriately
exceptions::IndexExceptionif index is out of range/invalid

◆ inspect() [4/4]

template<typename T >
void madara::knowledge::containers::CircularBufferConsumer::inspect ( KnowledgeRecord::Integer  position,
T &  value 
) const

Retrieves a record at a position relative to local index.

Parameters
positionthe relative position of the requested record from the latest added record. Can be negative
valuethe record at the position in the CircularBufferConsumer
Exceptions
exceptions::ContextExceptionif name or context haven't been set appropriately
exceptions::IndexExceptionif index is out of range/invalid

◆ operator!=()

bool madara::knowledge::containers::CircularBufferConsumer::operator!= ( const CircularBufferConsumer value) const
inline

Checks for inequality.

Parameters
valuethe value to compare to
Returns
true if inequal, false otherwise

Definition at line 105 of file CircularBufferConsumer.inl.

◆ operator==()

bool madara::knowledge::containers::CircularBufferConsumer::operator== ( const CircularBufferConsumer value) const
inline

Checks for equality.

Parameters
valuethe value to compare to
Returns
true if equal, false otherwise

Definition at line 99 of file CircularBufferConsumer.inl.

◆ peek_latest() [1/4]

std::vector< KnowledgeRecord > madara::knowledge::containers::CircularBufferConsumer::peek_latest ( size_t  count) const
inline

Peeks at the most recently added records.

This does not use or modify the local index. It instead references what the producer most recently added.

Parameters
countthe maximum number of items to look for
Returns
the last added records up to a certain count
Exceptions
exceptions::IndexExceptionif index is out of range/invalid

Definition at line 499 of file CircularBufferConsumer.inl.

◆ peek_latest() [2/4]

template<typename T >
void madara::knowledge::containers::CircularBufferConsumer::peek_latest ( size_t  count,
std::vector< T > &  values 
) const

Peeks at the most recently added records.

This does not use or modify the local index. It instead references what the producer most recently added.

Parameters
countthe maximum number of items to look for
valuesthe last added records up to a certain count
Exceptions
exceptions::IndexExceptionif index is out of range/invalid

◆ peek_latest() [3/4]

template<typename T >
void madara::knowledge::containers::CircularBufferConsumer::peek_latest ( T &  value) const

Peeks at the most recently added value.

This does not use or modify the local index. It instead references what the producer most recently added.

Parameters
valuethe last added value. exists() will return false if the record is invalid
Exceptions
exceptions::IndexExceptionif index is out of range/invalid

◆ peek_latest() [4/4]

madara::knowledge::KnowledgeRecord madara::knowledge::containers::CircularBufferConsumer::peek_latest ( void  ) const
inline

Peeks at the most recently added record.

This does not use or modify the local index. It instead references what the producer most recently added.

Returns
the last added record. exists() will return false if the record is invalid
Exceptions
exceptions::IndexExceptionif index is out of range/invalid

Definition at line 520 of file CircularBufferConsumer.inl.

◆ remaining()

size_t madara::knowledge::containers::CircularBufferConsumer::remaining ( void  ) const
inline

Returns the number of records remaining that have not been consumed with get_latest, get, or get_earliest.

Returns
the number of records remaining for get*
Exceptions
exceptions::ContextExceptionif name or context haven't been set appropriately

Definition at line 277 of file CircularBufferConsumer.inl.

◆ resize()

void madara::knowledge::containers::CircularBufferConsumer::resize ( void  )
inline

Resizes the buffer size to the producer's buffer size.

Definition at line 295 of file CircularBufferConsumer.inl.

◆ resync()

void madara::knowledge::containers::CircularBufferConsumer::resync ( void  )
inline

Sets the local index to the current buffer index.

Definition at line 304 of file CircularBufferConsumer.inl.

◆ set_index()

void madara::knowledge::containers::CircularBufferConsumer::set_index ( KnowledgeRecord::Integer  index)
inline

Sets the local index to an arbitrary position.

Parameters
indexthe new index to use

Definition at line 358 of file CircularBufferConsumer.inl.

◆ set_name() [1/2]

void madara::knowledge::containers::CircularBufferConsumer::set_name ( const std::string &  name,
KnowledgeBase knowledge 
)
inline

Sets the variable name that this refers to.

Parameters
namethe name of the variable in the knowledge base
knowledgethe knowledge base the variable is housed in
Exceptions
exceptions::NameExceptionbad name ("")

Definition at line 320 of file CircularBufferConsumer.inl.

◆ set_name() [2/2]

void madara::knowledge::containers::CircularBufferConsumer::set_name ( const std::string &  name,
Variables knowledge 
)
inline

Sets the variable name that this refers to.

Parameters
namethe name of the variable in the knowledge base
knowledgethe knowledge base the variable is housed in
Exceptions
exceptions::NameExceptionbad name ("")

Definition at line 339 of file CircularBufferConsumer.inl.

◆ size()

size_t madara::knowledge::containers::CircularBufferConsumer::size ( void  ) const
inline

Returns the maximum size of the CircularBufferConsumer.

Returns
the size of the CircularBufferConsumer

Definition at line 272 of file CircularBufferConsumer.inl.

Member Data Documentation

◆ buffer_

Vector madara::knowledge::containers::CircularBufferConsumer::buffer_
private

Underlying array of records.

Definition at line 402 of file CircularBufferConsumer.h.

◆ context_

ThreadSafeContext* madara::knowledge::containers::CircularBufferConsumer::context_
mutableprivate

guard for access and changes

Variable context that we are modifying

Definition at line 382 of file CircularBufferConsumer.h.

◆ index_

Integer madara::knowledge::containers::CircularBufferConsumer::index_
private

Index for latest item in circular buffer.

Definition at line 392 of file CircularBufferConsumer.h.

◆ local_index_

KnowledgeRecord::Integer madara::knowledge::containers::CircularBufferConsumer::local_index_
mutableprivate

Index for latest item read by.

Definition at line 397 of file CircularBufferConsumer.h.

◆ name_

std::string madara::knowledge::containers::CircularBufferConsumer::name_
private

Prefix of variable.

Definition at line 387 of file CircularBufferConsumer.h.


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