TRingBuffer

Copyright Epic Games, Inc. All Rights Reserved.

Choose your operating system:

Windows

macOS

Linux

References

Module

Core

Header

/Engine/Source/Runtime/Core/Public/Containers/RingBuffer.h

Include

#include "Containers/RingBuffer.h"

Syntax

template<typename T, typename AllocatorT>
class TRingBuffer

Remarks

RingBuffer - an array with a Front and Back pointer and with implicit wraparound to the beginning of the array when reaching the end of the array when iterating from Front to Back Useful for providing O(1) push/pop at the end of the array (for Queue or Stack) while still having high cache coherency during iteration. Not threadsafe; caller must ensure there is no simultaneous access from multiple threads.

Implementation Details: Relies on unsigned arithmetics and ever increasing Front and Back indices to avoid having to store an extra element or maintain explicit empty state. Capacity will always be rounded up to the next power of two, to provide rapid masking of the index.

Constructors

Name Description

Public function

TRingBuffer()

Construct Empty Queue with capacity 0.

Public function

TRingBuffer

(
    SizeType InitialCapacity
)

Construct Empty Queue with the given initial requested capacity.

Public function

TRingBuffer

(
    std::initializer_list< ElementType ...
)

Construct a Queue with initial state (from front to back) equal to the given list.

Public function

TRingBuffer

(
    TRingBuffer&& Other
)

Public function

TRingBuffer

(
    const TRingBuffer& Other
)

Public function

TRingBuffer

(
    uint32 BufferDataSize
)

Constructor

TRingBuffer implementation

Destructors

Name Description

Public function

~TRingBuffer()

Default Destructor

Public function

~TRingBuffer()

Functions

Name Description

Public function

IndexType

 

Add

(
    const ElementType& Element
)

Add a new element after the back pointer of the RingBuffer, resizing if necessary.

Public function

IndexType

 

Add

(
    ElementType&& Element
)

Add a new element after the back pointer of the RingBuffer, resizing if necessary.

Public function

ElementType ...

 

Add_GetRef

(
    const ElementType& Element
)

Add a new element after the back pointer of the RingBuffer, resizing if necessary.

Public function

ElementType ...

 

Add_GetRef

(
    ElementType&& Element
)

Add a new element after the back pointer of the RingBuffer, resizing if necessary.

Public function

IndexType

 

AddFront

(
    const ElementType& Element
)

Add a new element before the front pointer of the RingBuffer, resizing if necessary.

Public function

IndexType

 

AddFront

(
    ElementType&& Element
)

Add a new element before the front pointer of the RingBuffer, resizing if necessary.

Public function

ElementType ...

 

AddFront_GetRef

(
    const ElementType& Element
)

Add a new element before the front pointer of the RingBuffer, resizing if necessary.

Public function

ElementType ...

 

AddFront_GetRef

(
    ElementType&& Element
)

Add a new element before the front pointer of the RingBuffer, resizing if necessary.

Public function

IndexType

 

AddFrontUninitialized()

Add a new element before the front pointer of the RingBuffer, resizing if necessary.

Public function

ElementType ...

 

AddFrontUninitialized_GetRef()

Add a new element before the front pointer of the RingBuffer, resizing if necessary.

Public function

IndexType

 

AddUninitialized()

Add a new element after the back pointer of the RingBuffer, resizing if necessary.

Public function

ElementType ...

 

AddUninitialized_GetRef()

Add a new element after the back pointer of the RingBuffer, resizing if necessary.

Public function

TIterator

 

begin()

Begin iterator for ranged-for

Public function Const

TConstIterat...

 

begin()

Begin iterator for const ranged-for

Public function Const

const DataTy...

 

Bottom()

Gets the first data word in the FIFO (i.e. oldest).

Public function

DataType &

 

Bottom()

Gets the first data word in the FIFO (i.e. oldest).

Public function Const

uint32

 

BottomIndex()

Gets the buffer index that the first data word is stored in.

Public function

TArrayView< ...

 

Compact()

Shift all elements so that the front pointer's location in memory is less than the back pointer's.

Public function Const

IndexType

 

ConvertPointerToIndex

(
    const ElementType* Ptr
)

Given a pointer to an Element anywhere in memory, return the index of the element in the RingBuffer, or INDEX_NONE if it is not present.

Public function

uint32

 

Dequeue

(
    DataType* ValBuf,
    const uint32& BufLen
)

Take the next set of data words from the FIFO buffer.

Public function

bool

 

Dequeue

(
    DataType& ValOut
)

Take the next data word from the FIFO buffer.

Public function

IndexType

 

Emplace

(
    ArgsType&&... Args
)

Add a new element after the back pointer of the RingBuffer, resizing if necessary.

Public function

ElementType ...

 

Emplace_GetRef

(
    ArgsType&&... Args
)

Add a new element after the back pointer of the RingBuffer, resizing if necessary.

Public function

IndexType

 

EmplaceFront

(
    ArgsType&&... Args
)

Add a new element before the front pointer of the RingBuffer, resizing if necessary.

Public function

ElementType ...

 

EmplaceFront_GetRef

(
    ArgsType&&... Args
)

Add a new element before the front pointer of the RingBuffer, resizing if necessary.

Public function

void

 

Empty

(
    SizeType Capacity
)

Empty the RingBuffer, destructing any elements and releasing the RingBuffer's storage.

Public function

void

 

Empty()

Clears memory and indexes

Public function

TIterator

 

end()

End iterator for ranged-for

Public function Const

TConstIterat...

 

end()

End iterator for const ranged-for

Public function

void

 

Enqueue

(
    const DataType* ValBuf,
    const uint32& BufLen
)

Pushes a buffer of data words to the end of the FIFO. WILL OVERWRITE OLDEST if full.

Public function

void

 

Enqueue

(
    const DataType& Val
)

Pushes a data word to the end of the FIFO. WILL OVERWRITE OLDEST if full.

Public function Const

const Elemen...

 

First()

Return a const reference to the element at the front pointer of the RingBuffer.

Public function

ElementType ...

 

First()

Return a reference to the element at the front pointer of the RingBuffer.

Public function Const

SIZE_T

 

GetAllocatedSize()

Helper function to return the amount of memory allocated by this container.

Public function Const

const Elemen...

 

GetAtIndexNoCheck

(
    IndexType Index
)

Unsafely return a const reference to the value at the given Index.

Public function

ElementType ...

 

GetAtIndexNoCheck

(
    IndexType Index
)

Unsafely return a writable reference to the value at the given Index.

Public function Const

void

 

GetShaHash

(
    FSHAHash& OutHash
)

Get the SHA1 hash for the data currently in the FIFO

Public function Const

bool

 

IsEmpty()

Returns true if the RingBuffer is empty.

Public function Const

bool

 

IsValidIndex

(
    IndexType Index
)

Tests if index is valid, i.e. greater than or equal to zero, and less than the number of elements in the array.

Public function Const

const Elemen...

 

Last()

Return a const reference to the element at the back pointer of the RingBuffer.

Public function

ElementType ...

 

Last()

Return a reference to the element at the back pointer of the RingBuffer.

Public function Const

IndexType

 

Max()

Current allocated Capacity,

this will always be a power of two, or the special case 0.

Public function Const

uint32

 

NextIndex()

Gets the buffer index that the next enqueued word will get stored in.

Public function Const

IndexType

 

Num()

Gets the number of elements in the RingBuffer.

Public function Const

uint32

 

Peek

(
    DataType* ValBuf,
    const uint32& BufLen
)

Get the next set of data words from the FIFO buffer without removing them.

Public function Const

bool

 

Peek

(
    DataType& ValOut
)

Get the next data word from the FIFO buffer without removing it.

Public function

void

 

Pop

(
    SizeType PopCount
)

Pop the given number of arguments (default: 1) from the back pointer of the RingBuffer.

Public function

void

 

PopFront

(
    SizeType PopCount
)

Pop the given number of elements (default: 1) from the front pointer of the RingBuffer.

Public function

void

 

PopFrontNoCheck

(
    SizeType PopCount
)

Unsafely pop the given number of arguments (default: 1) from the front pointer of the RingBuffer.

Public function

ElementType

 

PopFrontValue()

Pop one element from the front pointer of the RingBuffer and return the popped value.

Public function

void

 

PopNoCheck

(
    SizeType PopCount
)

Pop the given number of elements (default: 1) from the back pointer of the RingBuffer.

Public function

ElementType

 

PopValue()

Pop one element from the back pointer of the RingBuffer and return the popped value.

Public function

SizeType

 

Remove

(
    const ElementType& Item
)

Removes as many instances of Item as there are in the array, maintaining order but not indices.

Public function

SizeType

 

RemoveAll

(
    PredicateType Predicate
)

Removes as many instances of Item as there are in the array, maintaining order but not indices.

Public function

void

 

RemoveAt

(
    IndexType Index
)

Remove the value at the given index from the RingBuffer, and shift values ahead or behind it into its location to fill the hole.

Public function

void

 

Reserve

(
    SizeType RequiredCapacity
)

Set the capacity to the maximum of the current capacity and the (next power of two greater than or equal to) the given capacity.

Public function

void

 

Reset()

Empty the RingBuffer, destructing any elements in the RingBuffer but not releasing the RingBuffer's storage.

Public function Const

uint32

 

RingDataSize()

Gets the size of the data buffer.

Public function Const

uint32

 

RingDataUsage()

Gets the number of words currently in the FIFO.

Public function Const

int32

 

SerialCompare

(
    const DataType* SerialBuffer,
    uint32 CompareLen
)

Compare the memory in the FIFO to the memory in the given buffer

Public function Const

void

 

Serialize

(
    DataType* SerialBuffer
)

Serializes the internal buffer into the given buffer

Public function

void

 

ShiftIndexToBack

(
    IndexType Index
)

Move the value at the given index into the back pointer of the RingBuffer, and shift all elements behind of it up by one to make room for it.

Public function

void

 

ShiftIndexToFront

(
    IndexType Index
)

Move the value at the given index into the front pointer of the RingBuffer, and shift all elements ahead of it down by one to make room for it.

Public function Const

const DataTy...

 

Top()

Gets the last data word in the FIFO (i.e. most recently pushed).

Public function

DataType &

 

Top()

Gets the last data word in the FIFO (i.e. most recently pushed).

Public function Const

uint32

 

TopIndex()

Gets the buffer index that the last data word is stored in.

Public function Const

uint64

 

TotalDataPushed()

Gets the total number of words that have been pushed through this buffer since clearing

Public function

void

 

Trim()

Set the capacity to the minimum power of two (or 0) greater than or equal to the current number of elements in the RingBuffer.

Operators

Name Description

Public function Const

bool

 

operator!=

(
    const TRingBuffer< ElementType, Oth...
)

Public function Const

const Elemen...

 

operator[]

(
    IndexType Index
)

Return a const reference to the value at the given Index.

Public function

ElementType ...

 

operator[]

(
    IndexType Index
)

Return a writable reference to the value at the given Index.

Public function Const

const DataTy...

 

operator[]

(
    const int32& Index
)

Square bracket operators for accessing data in the buffer by FIFO index.

Public function

DataType &

 

operator[]

(
    const int32& Index
)

Public function

TRingBuffer ...

 

operator=

(
    const TRingBuffer& Other
)

Public function

TRingBuffer ...

 

operator=

(
    TRingBuffer&& Other
)

Public function Const

bool

 

operator==

(
    const TRingBuffer< ElementType, Oth...
)

Typedefs

Name

Description

Allocator

The Allocator type being used

ElementType

Type of the Elements in the container.

IndexType

Type used to request values at a given index in the container.

SizeType

Type used to communicate size and capacity and counts

StorageModuloType

Type used for variables that are indexes into the underlying storage.

TConstIterator

TIterator

Iterator type used for ranged-for traversal.