Evo C++ Library v0.5.1
Public Types | Public Member Functions | Static Public Attributes | List of all members
BufferQueue< T, TSize > Class Template Reference

#include <evo/buffer_queue.h>

Detailed Description

template<class T, class TSize = SizeT>
class evo::BufferQueue< T, TSize >

Fast buffer-based queue, implemented with a ring-buffer.

Template Parameters
TItem type to use, copied with assignment operator
TSizeSize type to use for queue size (must be unsigned integer) – default: SizeT
Features

Note that this is not a full EvoContainer and doesn't have iterators.

Example
using namespace evo;
int main() {
BufferQueue<int> queue(20); // rounds to 32 (power of 2)
queue.add(1);
queue.add(2);
queue.add(3);
int a = queue.pop(); // set to 1
int b = queue.pop(); // set to 2
int c = queue.pop(); // set to 3
return 0;
}

Public Types

typedef T Item
 Item type. More...
 
typedef TSize Size
 Queue size integer type (always unsigned) More...
 
typedef BufferQueue< T, TSize > This
 This type More...
 

Public Member Functions

 BufferQueue (Size size=DEFAULT_SIZE)
 Constructor, sets buffer size. More...
 
 BufferQueue (const This &src)
 Copy constructor. More...
 
 ~BufferQueue ()
 Destructor. More...
 
bool add (const T &item, bool force=false)
 Add item to queue. More...
 
T * advAdd (bool force=false)
 Advanced: Add new item to queue and get pointer to it. More...
 
void clear ()
 Clear all items from queue, making it empty. More...
 
bool empty () const
 Get whether queue is empty. More...
 
bool full () const
 Get whether queue is full. More...
 
Thisoperator= (const This &src)
 Assignment operator. More...
 
const T & peek () const
 Peek at oldest item in queue. More...
 
const T * peek (Size &size) const
 Peek at oldest items in queue. More...
 
bool pop (T &item)
 Pop oldest item from queue. More...
 
bool pop ()
 Pop oldest item from queue without returning it. More...
 
Size pop_size (Size size)
 Pop oldest items from queue in bulk. More...
 
Size size () const
 Get buffer size. More...
 
Size used () const
 Get used item count. More...
 

Static Public Attributes

static const Size DEFAULT_SIZE = 128
 Default size to use. More...
 
static const Size MIN_SIZE = 2
 Minimum size to use. More...
 

Member Typedef Documentation

◆ Item

typedef T Item

Item type.

◆ Size

typedef TSize Size

Queue size integer type (always unsigned)

◆ This

typedef BufferQueue<T,TSize> This

This type

Constructor & Destructor Documentation

◆ BufferQueue() [1/2]

BufferQueue ( Size  size = DEFAULT_SIZE)
inline

Constructor, sets buffer size.

Parameters
sizeBuffer size to use as item count, rouned to next power of 2 if needed

◆ BufferQueue() [2/2]

BufferQueue ( const This src)
inline

Copy constructor.

  • Not exception safe: Item assignment operator must not throw
Parameters
srcQueue to copy from

◆ ~BufferQueue()

~BufferQueue ( )
inline

Destructor.

Member Function Documentation

◆ add()

bool add ( const T &  item,
bool  force = false 
)
inline

Add item to queue.

  • Exception safe: Queue is unchanged if item copy throws
Parameters
itemItem to add, copied with assignment operator
forceWhether to overwrite oldest item when full, false to return false if full
Returns
Whether new item added, false if full or if oldest item was overwritten with force=true

◆ advAdd()

T* advAdd ( bool  force = false)
inline

Advanced: Add new item to queue and get pointer to it.

  • This doesn't actually reset or overwrite the item, but just returns a pointer to it
  • Use this to setup the new item directly, instead of making a copy with add()
Parameters
forceWhether to overwrite oldest item when full, false to fail if full
Returns
Pointer to added item, NULL if full (and force is false)

◆ clear()

void clear ( )
inline

Clear all items from queue, making it empty.

◆ empty()

bool empty ( ) const
inline

Get whether queue is empty.

Returns
Whether empty, same as used() == 0

◆ full()

bool full ( ) const
inline

Get whether queue is full.

Returns
Whether full, same as used() == size()

◆ operator=()

This& operator= ( const This src)
inline

Assignment operator.

  • Copies all items from another queue to this
  • This will resize the current queue to match src, all current items will be lost
  • Not exception safe: Item assignment operator must not throw

◆ peek() [1/2]

const T& peek ( ) const
inline

Peek at oldest item in queue.

  • Use pop() to remove this item
Returns
Reference to oldest item in queue, or oldest/first item if queue is empty

◆ peek() [2/2]

const T* peek ( Size size) const
inline

Peek at oldest items in queue.

  • This is useful for bulk processing items in queue, though may take up to 2 passes when the ring-buffer wraps around
  • This returns a pointer to the oldest items and the size of contiguous items from there
  • Use pop_size() with size to remove these items, then call this again to see any remaining items
Parameters
sizeSet to number of contiguous items from pointer, 0 if empty
Returns
Oldest item pointer, NULL if empty

◆ pop() [1/2]

bool pop ( T &  item)
inline

Pop oldest item from queue.

  • This doesn't really remove the item, but leaves it as-is in buffer to be overwritten later
  • Exception safe: Queue is unchanged if item copy throws
Parameters
itemStores popped item, copied with assignment operator
Returns
Whether item popped, false if queue is empty

◆ pop() [2/2]

bool pop ( )
inline

Pop oldest item from queue without returning it.

  • Use peek() const to get the item first
  • This doesn't really remove the item, but leaves it as-is in buffer to be overwritten later
Returns
Whether item popped, false if queue is empty

◆ pop_size()

Size pop_size ( Size  size)
inline

Pop oldest items from queue in bulk.

  • Use peek(Size&) const to get the items first
  • This doesn't really remove items, but leaves them as-is in buffer to be overwritten later
Parameters
sizeNumber of items to pop, ALL for all
Returns
Number of items popped, may be less than requested if queue is smaller, 0 if queue is empty

◆ size()

Size size ( ) const
inline

Get buffer size.

Returns
Buffer size as item count, always a power of 2

◆ used()

Size used ( ) const
inline

Get used item count.

Returns
Item count used, 0 if queue is empty

Member Data Documentation

◆ DEFAULT_SIZE

const Size DEFAULT_SIZE = 128
static

Default size to use.

◆ MIN_SIZE

const Size MIN_SIZE = 2
static

Minimum size to use.


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