#include <evo/buffer_queue.h>
template<class T, class TSize = SizeT>
class evo::BufferQueue< T, TSize >
Fast buffer-based queue, implemented with a ring-buffer.
- Template Parameters
-
T | Item type to use, copied with assignment operator |
TSize | Size type to use for queue size (must be unsigned integer) – default: SizeT |
- Features
- Efficient buffer based queue, especially for simple (POD or Byte-Copy) types
- This allocates a fixed size buffer and is not resizable, size is always a power of 2
- For a dynamic size queue (or stack) see: List
- Adding items does not allocate memory (though the item assignment operator may)
- Popped (removed or cleared) items are left as-is in buffer, to be overwritten as new items are added
- All operations take constant time, except copy constructor and assignment operator which take linear time (to copy items)
Note that this is not a full EvoContainer and doesn't have iterators.
- Example
int main() {
queue.add(1);
queue.add(2);
queue.add(3);
int a = queue.pop();
int b = queue.pop();
int c = queue.pop();
return 0;
}
◆ Item
◆ Size
Queue size integer type (always unsigned)
◆ This
◆ BufferQueue() [1/2]
Constructor, sets buffer size.
- Parameters
-
size | Buffer size to use as item count, rouned to next power of 2 if needed |
◆ BufferQueue() [2/2]
Copy constructor.
- Not exception safe: Item assignment operator must not throw
- Parameters
-
◆ ~BufferQueue()
◆ add()
bool add |
( |
const T & |
item, |
|
|
bool |
force = false |
|
) |
| |
|
inline |
Add item to queue.
- Exception safe: Queue is unchanged if item copy throws
- Parameters
-
item | Item to add, copied with assignment operator |
force | Whether 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
-
force | Whether to overwrite oldest item when full, false to fail if full |
- Returns
- Pointer to added item, NULL if full (and force is false)
◆ clear()
Clear all items from queue, making it empty.
◆ empty()
Get whether queue is empty.
- Returns
- Whether empty, same as used() == 0
◆ full()
Get whether queue is full.
- Returns
- Whether full, same as used() == size()
◆ operator=()
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]
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
-
size | Set to number of contiguous items from pointer, 0 if empty |
- Returns
- Oldest item pointer, NULL if empty
◆ pop() [1/2]
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
-
item | Stores popped item, copied with assignment operator |
- Returns
- Whether item popped, false if queue is empty
◆ pop() [2/2]
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()
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
-
size | Number 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()
Get buffer size.
- Returns
- Buffer size as item count, always a power of 2
◆ used()
Get used item count.
- Returns
- Item count used, 0 if queue is empty
◆ DEFAULT_SIZE
const Size DEFAULT_SIZE = 128 |
|
static |
◆ MIN_SIZE
The documentation for this class was generated from the following file: