Direct-BT
2.3.1
Direct-BT - Direct Bluetooth Programming.
|
Ring buffer implementation, a.k.a circular buffer, exposing lock-free get*(..) and put*(..) methods. More...
#include <ringbuffer.hpp>
Public Types | |
typedef Value_type | value_type |
typedef value_type * | pointer |
typedef const value_type * | const_pointer |
typedef value_type & | reference |
typedef const value_type & | const_reference |
typedef Size_type | size_type |
typedef std::make_signed< size_type >::type | difference_type |
Public Member Functions | |
Size_type | waitForElements (const Size_type min_count, const int timeoutMS) noexcept |
Blocks until at least count elements have been put for subsequent get() and getBlocking(). More... | |
Size_type | waitForFreeSlots (const Size_type min_count, const int timeoutMS) noexcept |
Blocks until at least count free slots become available for subsequent put() and putBlocking(). More... | |
std::string | toString () const noexcept |
Returns a short string representation incl. More... | |
void | dump (FILE *stream, std::string prefix) const noexcept |
Debug functionality - Dumps the contents of the internal array. More... | |
ringbuffer (const NullValue_type &nullelem_, const std::vector< Value_type > ©From) noexcept | |
Create a full ring buffer instance w/ the given array's net capacity and content. More... | |
ringbuffer (const NullValue_type &nullelem_, const Value_type *copyFrom, const Size_type copyFromSize) noexcept | |
ringbuffer (const NullValue_type &nullelem_, const Size_type capacity) noexcept | |
Create an empty ring buffer instance w/ the given net capacity . More... | |
~ringbuffer () noexcept | |
ringbuffer (const ringbuffer &_source) noexcept | |
ringbuffer & | operator= (const ringbuffer &_source) noexcept |
ringbuffer (ringbuffer &&o) noexcept=default | |
ringbuffer & | operator= (ringbuffer &&o) noexcept=default |
Size_type | capacity () const noexcept |
Returns the net capacity of this ring buffer. More... | |
void | clear () noexcept |
Releasing all elements by assigning nullelem . More... | |
void | reset (const Value_type *copyFrom, const Size_type copyFromCount) noexcept |
clear() all elements and add all copyFrom elements thereafter. More... | |
void | reset (const std::vector< Value_type > ©From) noexcept |
Size_type | getSize () const noexcept |
Returns the number of elements in this ring buffer. More... | |
Size_type | getFreeSlots () const noexcept |
Returns the number of free slots available to put. More... | |
bool | isEmpty () const noexcept |
Returns true if this ring buffer is empty, otherwise false. More... | |
bool | isFull () const noexcept |
Returns true if this ring buffer is full, otherwise false. More... | |
Value_type | peek () noexcept |
Peeks the next element at the read position w/o modifying pointer, nor blocking. More... | |
bool | peek (Value_type &result) noexcept |
Peeks the next element at the read position w/o modifying pointer, nor blocking. More... | |
Value_type | peekBlocking (const int timeoutMS=0) noexcept |
Peeks the next element at the read position w/o modifying pointer, but with blocking. More... | |
bool | peekBlocking (Value_type &result, const int timeoutMS=0) noexcept |
Peeks the next element at the read position w/o modifying pointer, but with blocking. More... | |
Value_type | get () noexcept |
Dequeues the oldest enqueued element if available, otherwise null. More... | |
bool | get (Value_type &result) noexcept |
Dequeues the oldest enqueued element if available, otherwise null. More... | |
Value_type | getBlocking (const int timeoutMS=0) noexcept |
Dequeues the oldest enqueued element. More... | |
bool | getBlocking (Value_type &result, const int timeoutMS=0) noexcept |
Dequeues the oldest enqueued element. More... | |
Size_type | get (Value_type *dest, const Size_type dest_len, const Size_type min_count) noexcept |
Dequeues the oldest enqueued min(dest_len, getSize()>=min_count) elements by copying them into the given consecutive 'dest' storage. More... | |
Size_type | getBlocking (Value_type *dest, const Size_type dest_len, const Size_type min_count, const int timeoutMS=0) noexcept |
Dequeues the oldest enqueued min(dest_len, getSize()>=min_count) elements by copying them into the given consecutive 'dest' storage. More... | |
bool | drop (const Size_type count) noexcept |
Drops. More... | |
bool | dropBlocking (const Size_type count, const int timeoutMS=0) noexcept |
Drops. More... | |
bool | put (Value_type &&e) noexcept |
Enqueues the given element by moving it into this ringbuffer storage. More... | |
bool | putBlocking (Value_type &&e, const int timeoutMS=0) noexcept |
Enqueues the given element by moving it into this ringbuffer storage. More... | |
bool | put (const Value_type &e) noexcept |
Enqueues the given element by copying it into this ringbuffer storage. More... | |
bool | putBlocking (const Value_type &e, const int timeoutMS=0) noexcept |
Enqueues the given element by copying it into this ringbuffer storage. More... | |
bool | put (const Value_type *first, const Value_type *last) noexcept |
Enqueues the given range of consecutive elements by copying it into this ringbuffer storage. More... | |
bool | putBlocking (const Value_type *first, const Value_type *last, const int timeoutMS=0) noexcept |
Enqueues the given range of consecutive elementa by copying it into this ringbuffer storage. More... | |
void | recapacity (const Size_type newCapacity) |
Resizes this ring buffer's capacity. More... | |
Static Public Attributes | |
constexpr static const bool | uses_memcpy = use_memcpy |
constexpr static const bool | uses_memset = use_memset |
Ring buffer implementation, a.k.a circular buffer, exposing lock-free get*(..) and put*(..) methods.
Implementation utilizes the Always Keep One Slot Open, hence implementation maintains an internal array of capacity
plus one!
Implementation is thread safe if:
Following methods acquire the global multi-read and -write mutex:
Characteristics:
Empty | writePos == readPos | size == 0 |
Full | writePos == readPos - 1 | size == capacity |
Empty [RW][][ ][ ][ ][ ][ ][ ] ; W==R Avail [ ][ ][R][.][.][.][.][W] ; W > R Avail [.][.][.][W][ ][ ][R][.] ; W < R - 1 Full [.][.][.][.][.][W][R][.] ; W==R-1
See also:
We would like to passNullValue_type nullelem
as a non-type template parameter of typeNullValue_type
, a potential Class. However, this is only allowed in C++20 and we use C++17 for now. Hence we have to passNullValue_type nullelem
in the constructor.
Definition at line 115 of file ringbuffer.hpp.
typedef const value_type* jau::ringbuffer< Value_type, NullValue_type, Size_type, use_memcpy, use_memset >::const_pointer |
Definition at line 124 of file ringbuffer.hpp.
typedef const value_type& jau::ringbuffer< Value_type, NullValue_type, Size_type, use_memcpy, use_memset >::const_reference |
Definition at line 126 of file ringbuffer.hpp.
typedef std::make_signed<size_type>::type jau::ringbuffer< Value_type, NullValue_type, Size_type, use_memcpy, use_memset >::difference_type |
Definition at line 128 of file ringbuffer.hpp.
typedef value_type* jau::ringbuffer< Value_type, NullValue_type, Size_type, use_memcpy, use_memset >::pointer |
Definition at line 123 of file ringbuffer.hpp.
typedef value_type& jau::ringbuffer< Value_type, NullValue_type, Size_type, use_memcpy, use_memset >::reference |
Definition at line 125 of file ringbuffer.hpp.
typedef Size_type jau::ringbuffer< Value_type, NullValue_type, Size_type, use_memcpy, use_memset >::size_type |
Definition at line 127 of file ringbuffer.hpp.
typedef Value_type jau::ringbuffer< Value_type, NullValue_type, Size_type, use_memcpy, use_memset >::value_type |
Definition at line 122 of file ringbuffer.hpp.
|
inlinenoexcept |
Create a full ring buffer instance w/ the given array's net capacity and content.
Example for a 10 element Integer array:
Integer[] source = new Integer[10]; // fill source with content .. ringbuffer<Integer> rb = new ringbuffer<Integer>(source);
isFull() returns true on the newly created full ring buffer.
Implementation will allocate an internal array with size of array copyFrom
plus one, and copy all elements from array copyFrom
into the internal array.
nullelem | The null value used to zero removed elements on get*(..) and clear() |
copyFrom | mandatory source array determining ring buffer's net capacity() and initial content. |
IllegalArgumentException | if copyFrom is nullptr |
Definition at line 799 of file ringbuffer.hpp.
|
inlinenoexcept |
nullelem | The null value used to zero removed elements on get*(..) and clear() |
copyFrom | |
copyFromSize |
Definition at line 812 of file ringbuffer.hpp.
|
inlinenoexcept |
Create an empty ring buffer instance w/ the given net capacity
.
Example for a 10 element Integer array:
ringbuffer<Integer> rb = new ringbuffer<Integer>(10, Integer[].class);
isEmpty() returns true on the newly created empty ring buffer.
Implementation will allocate an internal array of size capacity
plus one.
nullelem | The null value used to zero removed elements on get*(..) and clear() |
arrayType | the array type of the created empty internal array. |
capacity | the initial net capacity of the ring buffer |
Definition at line 838 of file ringbuffer.hpp.
|
inlinenoexcept |
Definition at line 845 of file ringbuffer.hpp.
|
inlinenoexcept |
Definition at line 852 of file ringbuffer.hpp.
|
defaultnoexcept |
|
inlinenoexcept |
Returns the net capacity of this ring buffer.
Definition at line 891 of file ringbuffer.hpp.
|
inlinenoexcept |
Releasing all elements by assigning nullelem
.
isEmpty() will return true
and getSize() will return 0
after calling this method.
Definition at line 900 of file ringbuffer.hpp.
|
inlinenoexcept |
Drops.
oldest enqueued elements.
Method is non blocking and returns immediately;.
count | number of elements to drop from ringbuffer. |
Definition at line 1112 of file ringbuffer.hpp.
|
inlinenoexcept |
Drops.
oldest enqueued elements.
timeoutMS
defaults to zero, i.e. infinitive blocking until an element available via put.
Otherwise this methods blocks for the given milliseconds.
count | number of elements to drop from ringbuffer. |
Definition at line 1126 of file ringbuffer.hpp.
|
inlinenoexcept |
Debug functionality - Dumps the contents of the internal array.
Definition at line 766 of file ringbuffer.hpp.
|
inlinenoexcept |
Dequeues the oldest enqueued element if available, otherwise null.
The returned ring buffer slot will be set to nullelem
to release the reference and move ownership to the caller.
Method is non blocking and returns immediately;.
nullelem
. Definition at line 1004 of file ringbuffer.hpp.
|
inlinenoexcept |
Dequeues the oldest enqueued element if available, otherwise null.
The returned ring buffer slot will be set to nullelem
to release the reference and move ownership to the caller.
Method is non blocking and returns immediately;.
result | storage for the resulting value if successful, otherwise nullelem if empty. |
Definition at line 1021 of file ringbuffer.hpp.
|
inlinenoexcept |
Dequeues the oldest enqueued min(dest_len, getSize()>=min_count)
elements by copying them into the given consecutive 'dest' storage.
The returned ring buffer slot will be set to nullelem
to release the reference and move ownership to the caller.
Method is non blocking and returns immediately;.
dest | pointer to first storage element of count consecutive elements. |
dest_len | number of consecutive elements in dest and maximum number of elements to get |
min_count | minimum number of consecutive elements to get |
Definition at line 1079 of file ringbuffer.hpp.
|
inlinenoexcept |
Dequeues the oldest enqueued element.
The returned ring buffer slot will be set to nullelem
to release the reference and move ownership to the caller.
timeoutMS
defaults to zero, i.e. infinitive blocking until an element available via put.
Otherwise this methods blocks for the given milliseconds.
nullelem
if timeout occurred. Definition at line 1040 of file ringbuffer.hpp.
|
inlinenoexcept |
Dequeues the oldest enqueued element.
The returned ring buffer slot will be set to nullelem
to release the reference and move ownership to the caller.
timeoutMS
defaults to zero, i.e. infinitive blocking until an element available via put.
Otherwise this methods blocks for the given milliseconds.
result | storage for the resulting value if successful, otherwise nullelem if empty. |
Definition at line 1059 of file ringbuffer.hpp.
|
inlinenoexcept |
Dequeues the oldest enqueued min(dest_len, getSize()>=min_count)
elements by copying them into the given consecutive 'dest' storage.
The returned ring buffer slot will be set to nullelem
to release the reference and move ownership to the caller.
timeoutMS
defaults to zero, i.e. infinitive blocking until an element available via put.
Otherwise this methods blocks for the given milliseconds.
dest | pointer to first storage element of count consecutive elements. |
dest_len | number of consecutive elements in dest and maximum number of elements to get |
min_count | minimum number of consecutive elements to get |
timeoutMS |
Definition at line 1100 of file ringbuffer.hpp.
|
inlinenoexcept |
Returns the number of free slots available to put.
Definition at line 935 of file ringbuffer.hpp.
|
inlinenoexcept |
Returns the number of elements in this ring buffer.
Definition at line 926 of file ringbuffer.hpp.
|
inlinenoexcept |
Returns true if this ring buffer is empty, otherwise false.
Definition at line 938 of file ringbuffer.hpp.
|
inlinenoexcept |
Returns true if this ring buffer is full, otherwise false.
Definition at line 941 of file ringbuffer.hpp.
|
inlinenoexcept |
Definition at line 864 of file ringbuffer.hpp.
|
defaultnoexcept |
|
inlinenoexcept |
Peeks the next element at the read position w/o modifying pointer, nor blocking.
nullelem
if empty, otherwise the element which would be read next. Definition at line 947 of file ringbuffer.hpp.
|
inlinenoexcept |
Peeks the next element at the read position w/o modifying pointer, nor blocking.
result | storage for the resulting value if successful, otherwise nullelem if empty. |
Definition at line 957 of file ringbuffer.hpp.
|
inlinenoexcept |
Peeks the next element at the read position w/o modifying pointer, but with blocking.
timeoutMS
defaults to zero, i.e. infinitive blocking until an element available via put.
Otherwise this methods blocks for the given milliseconds.
nullelem
if empty or timeout occurred, otherwise the element which would be read next. Definition at line 972 of file ringbuffer.hpp.
|
inlinenoexcept |
Peeks the next element at the read position w/o modifying pointer, but with blocking.
timeoutMS
defaults to zero, i.e. infinitive blocking until an element available via put.
Otherwise this methods blocks for the given milliseconds.
result | storage for the resulting value if successful, otherwise nullelem if empty. |
Definition at line 987 of file ringbuffer.hpp.
|
inlinenoexcept |
Enqueues the given element by copying it into this ringbuffer storage.
Returns true if successful, otherwise false in case buffer is full.
Method is non blocking and returns immediately;.
Definition at line 1167 of file ringbuffer.hpp.
|
inlinenoexcept |
Enqueues the given range of consecutive elements by copying it into this ringbuffer storage.
Returns true if successful, otherwise false in case buffer is full.
Method is non blocking and returns immediately;.
first | pointer to first consecutive element to range of value_type [first, last) |
last | pointer to last consecutive element to range of value_type [first, last) |
Definition at line 1196 of file ringbuffer.hpp.
|
inlinenoexcept |
Enqueues the given element by moving it into this ringbuffer storage.
Returns true if successful, otherwise false in case buffer is full.
Method is non blocking and returns immediately;.
Definition at line 1140 of file ringbuffer.hpp.
|
inlinenoexcept |
Enqueues the given element by copying it into this ringbuffer storage.
timeoutMS
defaults to zero, i.e. infinitive blocking until a free slot becomes available via get.
Otherwise this methods blocks for the given milliseconds.
Definition at line 1180 of file ringbuffer.hpp.
|
inlinenoexcept |
Enqueues the given range of consecutive elementa by copying it into this ringbuffer storage.
timeoutMS
defaults to zero, i.e. infinitive blocking until a free slot becomes available via get.
Otherwise this methods blocks for the given milliseconds.
first | pointer to first consecutive element to range of value_type [first, last) |
last | pointer to last consecutive element to range of value_type [first, last) |
timeoutMS |
Definition at line 1212 of file ringbuffer.hpp.
|
inlinenoexcept |
Enqueues the given element by moving it into this ringbuffer storage.
timeoutMS
defaults to zero, i.e. infinitive blocking until a free slot becomes available via get.
Otherwise this methods blocks for the given milliseconds.
Definition at line 1153 of file ringbuffer.hpp.
|
inline |
Resizes this ring buffer's capacity.
New capacity must be greater than current size.
Definition at line 1222 of file ringbuffer.hpp.
|
inlinenoexcept |
Definition at line 918 of file ringbuffer.hpp.
|
inlinenoexcept |
clear() all elements and add all copyFrom
elements thereafter.
copyFrom | Mandatory array w/ length capacity() to be copied into the internal array. |
Definition at line 911 of file ringbuffer.hpp.
|
inlinenoexcept |
Returns a short string representation incl.
size/capacity and internal r/w index (impl. dependent).
Definition at line 758 of file ringbuffer.hpp.
|
inlinenoexcept |
Blocks until at least count
elements have been put for subsequent get() and getBlocking().
min_count | minimum number of put slots |
timeoutMS |
Definition at line 701 of file ringbuffer.hpp.
|
inlinenoexcept |
Blocks until at least count
free slots become available for subsequent put() and putBlocking().
min_count | minimum number of free slots |
timeoutMS |
Definition at line 733 of file ringbuffer.hpp.
|
staticconstexpr |
Definition at line 117 of file ringbuffer.hpp.
|
staticconstexpr |
Definition at line 118 of file ringbuffer.hpp.