|
HLCC Documentation 2.2.0
|
A first-in first-out buffer with a fixed size that replaces its oldest element if full. More...
#include <circularBufferConcurrent.hpp>
Public Member Functions | |
| CircularBufferConcurrent (const std::string &name, std::size_t capacity, const log4cplus::Logger &logger) | |
| ~CircularBufferConcurrent () | |
| CircularBufferConcurrent (const CircularBufferConcurrent &)=delete | |
| CircularBufferConcurrent & | operator= (const CircularBufferConcurrent &)=delete |
| void | Push (T &&data) |
| Adds new data to the circular buffer. | |
| std::optional< T > | Poll (std::chrono::milliseconds timeout=std::chrono::milliseconds::zero()) |
| Gets the oldest element from the circular buffer. | |
| void | Clear () |
| int | Size () const |
| const std::unique_ptr< std::scoped_lock< std::recursive_mutex > > | Lock () const |
| void | SetDiscardListener (std::function< void(T &)> discard_listener) |
| boost::circular_buffer< T > & | GetCb () |
A first-in first-out buffer with a fixed size that replaces its oldest element if full.
It is thead-safe and in addition allows clients to share its internal mutex, so that they can synchronize their own activities with the calls to the circular buffer. It also allows clients to be notified when old data has been dropped on inserting new data.
It is modeled after the Java class elt.prototype.framework.util.CircularBuffer.
HSO TOOD: Doxygen link to methods in the above description?
TODO: Check using https://www.boost.org/doc/libs/1_76_0/doc/html/lockfree.html instead of circular_buffer.
Note that CircularBufferConcurrent<T> is declared in the .hpp file and defined defined in the .ipp file. There is no .cpp file. This avoids linker problems, since the definition must be in scope at the point of use. Repeated compilation of the same template instantiations could be avoided using "extern template" (https://arne-mertz.de/2019/02/extern-template-reduce-compile-times/), with known optimizer issues. With C++ 20 modules, this problem will be removed.
|
explicit |
| hlcc::oldbmux::CircularBufferConcurrent< T >::~CircularBufferConcurrent | ( | ) |
|
delete |
| void hlcc::oldbmux::CircularBufferConcurrent< T >::Clear | ( | ) |
| boost::circular_buffer< T > & hlcc::oldbmux::CircularBufferConcurrent< T >::GetCb | ( | ) |
Exposes the underlying circular buffer, e.g for inspection or manipulation of buffered elements. This should be used with extreme care, e.g. only while this buffer is locked (see Lock()).
| const std::unique_ptr< std::scoped_lock< std::recursive_mutex > > hlcc::oldbmux::CircularBufferConcurrent< T >::Lock | ( | ) | const |
Locks the mutex that is internally used by the circular buffer. The client may use this method to perform tasks that need the buffer to be locked.
|
delete |
| std::optional< T > hlcc::oldbmux::CircularBufferConcurrent< T >::Poll | ( | std::chrono::milliseconds | timeout = std::chrono::milliseconds::zero() | ) |
Gets the oldest element from the circular buffer.
The optional timeout parameter allows blocking for up to the given time if no data is available. If it is zero or omitted, this method returns without waiting, even if no data is available.
| timeout | The timeout in ms for waiting for data, if none is available. Note that the client can provide different units for the chrono::duration, which will implicitly be converted to the chrono duration in ms. |
| void hlcc::oldbmux::CircularBufferConcurrent< T >::Push | ( | T && | data | ) |
Adds new data to the circular buffer.
Discards oldest data if the buffer is full, in which case a registered discard listener gets notified.
| data |
| void hlcc::oldbmux::CircularBufferConcurrent< T >::SetDiscardListener | ( | std::function< void(T &)> | discard_listener | ) |
Allows setting a single listener for data that gets discarded when the buffer is full.
This callback function gets invoked AFTER the new element has replaced the old element. The old element is passed as an argument, e.g. so that the listener impl can set an exception on a contained promise. The new element can be found in the buffer, e.g. for the listener impl to transfer some data from the discarded to the newly inserted element.
| int hlcc::oldbmux::CircularBufferConcurrent< T >::Size | ( | ) | const |