MLIB
Loading...
Searching...
No Matches
mlib::async_buffer< M > Class Template Reference

A template class that implements "asynchronous ring buffer". More...

#include <syncque.h>

Inheritance diagram for mlib::async_buffer< M >:
mlib::ring_buffer< M >

Public Member Functions

 async_buffer (size_t limit_)
 Creates a buffer with the given maximum size.
 
virtual void produce (const M &obj)
 Append an element to buffer.
 
virtual bool consume (M &result, int timeout=INFINITE)
 
bool empty ()
 Return true if buffer is empty.
 
bool full () const
 Return true if buffer is at capacity.
 
size_t size () const
 Return buffer size.
 
void clear ()
 Remove all elements from buffer.
 

Protected Attributes

semaphore sema
 semaphore counts buffer size
 
criticalsection update
 critical section protects buffer's integrity
 

Additional Inherited Members

- Protected Types inherited from mlib::ring_buffer< M >
using value_type
 
using reference
 
using const_reference
 
using size_type
 
- Protected Member Functions inherited from mlib::ring_buffer< M >
 ring_buffer (size_t size)
 Constructor.
 
 ring_buffer ()
 Default constructor.
 
 ring_buffer (const ring_buffer &other)
 Copy constructor.
 
 ring_buffer (std::initializer_list< M > il)
 Initializer list constructor.
 
ring_buffer & operator= (const ring_buffer &rhs)
 Assignment operator.
 
bool operator== (const ring_buffer< M > &other) const
 Equality operator.
 
bool operator!= (const ring_buffer< M > &other) const
 Inequality operator.
 
 operator std::vector< M > () const
 Vector conversion operator.
 
void push_back (const M &item)
 Inserts new element in buffer.
 
iterator begin ()
 Return an iterator pointing to first (oldest) element in buffer.
 
const_iterator begin () const
 Return a const iterator pointing to first (oldest) element in buffer.
 
const_iterator cbegin ()
 Return a const iterator pointing to first (oldest) element in buffer.
 
iterator end ()
 Return an iterator pointing past the last (newest) element in buffer.
 
const_iterator end () const
 Return a const iterator pointing past the last (newest) element in buffer.
 
const_iterator cend ()
 Return an iterator pointing past the last (newest) element in buffer.
 
void pop_front ()
 Remove oldest element from buffer.
 
M & front ()
 Return a reference to first (oldest) element in buffer.
 
const M & front () const
 Return a reference to first (oldest) element in buffer.
 
M & back ()
 Return reference to last (newest) element in buffer.
 
const M & back () const
 Return reference to last (newest) element in buffer.
 
iterator erase (const_iterator &ptr)
 
void clear (void)
 Remove all elements from buffer.
 
bool empty (void) const
 Return true if buffer is empty.
 
bool full (void) const
 Return true if buffer is full.
 
size_t capacity () const
 Return maximum buffer size.
 
void resize (size_t new_cap)
 (Re)allocate buffer with a different capacity
 
size_t size () const
 Return number of elements in buffer.
 

Detailed Description

template<class M>
class mlib::async_buffer< M >

A template class that implements "asynchronous ring buffer".

These are producer/consumer buffers which can hold "messages" and deliver them in FIFO order. Attempting to consume from an empty buffer will block the calling thread until a message arrives. Producing in a full buffer replaces the "oldest" element with the new one.

Member Function Documentation

◆ consume()

template<class M>
virtual bool mlib::async_buffer< M >::consume ( M & result,
int timeout = INFINITE )
inlinevirtual

Extract the first element in queue Return true if an element was extracted or false if a timeout occurred.


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