Skip to main content
Version: 11 Nov 2025

CircularBuffer

NameSpace: MagicLeap

This is a specialized utility class meant to be used within the Unity SDK for very specific cases. This should not be taken as a general purpose Circular buffer as it does not cater to all possible use cases. More...

Inherits from:
IEnumerator,
IEnumerable

Detailed Description​

package template <T > class CircularBuffer : IEnumerator 

Template Parameters:

  • T Type of data to store in the buffer

Public Fields​

Count​

Number of elements stored in the buffer


public int Count { get; set; }


Public Methods​

CircularBuffer​

Create a fixed size uninitialized buffer. Use Enqueue / Dequeue / TryDequeue / Count / Capacity / Clear / Resize and the enumerator.

public CircularBuffer(
uint fixedSize
)

Parameters

TypeNameDescription
uintfixedSize

void Clear​

Remove all elements from the buffer. This doesn't actually remove the elements, only resets the queue front/back markers. Thus, the ref-count of objects doesn't go down on calling this function.

public void Clear()

CircularBuffer< T > Create​

Create a CircularBuffer with preinitialized objects. When creating the buffer with this factory method, use the Get() method to get a reference to the next available object. DO NOT use any other methods available in this class as they;re relevant only when the CircularBuffer is instantiated with a fixed size and no preinitialized objects.

public static CircularBuffer< T > Create(
params T [] objects
)

Parameters

TypeNameDescription
params T []objectsArray of objects to initialize the Circular Buffer with.

Returns:


int Dequeue​

Dequeue an entire array of elements. Uses Array.Copy() and is thus more efficient than calling TryDequeue() in a loop.

public int Dequeue(
T [] data
)

Parameters

TypeNameDescription
T []dataArray to dequeue into

Returns: Number of elements that were dequeued into the provided array


void Enqueue​

Pushes a new element at the end of the queue. Will replace the front element if queue is full. TODO : ^ make this behavior configurable

public void Enqueue(
T value
)

Parameters

TypeNameDescription
TvalueValue to enqueue

void Enqueue​

Enqueue an entire array at the end of the queue. Uses Array.Copy() and is thus more efficient than calling Enqueue(T value) in a loop.

public void Enqueue(
T [] values
)

Parameters

TypeNameDescription
T []valuesArray of values to enqueue

T Get​

Gets the next available pre-initialized object.

public T Get()

Returns: Next object in the circular buffer


IEnumerator GetEnumerator​

Get the enumerator implemented for this collection

public IEnumerator GetEnumerator()

Returns: Enumerator


bool MoveNext​

Move to the next element when using this class as an IEnumerator

public bool MoveNext()

Returns: True if there are still more elements remaining in the queue, false if end of collection has been reached


T Peek​

Gets the curent pre-initialized object in use.

public T Peek()

Returns: Current object in the circular buffer


void Reset​

Reset the flags for the enumerator

public void Reset()

void Resize​

Change the capacity of the buffer. If new capacity is more than the previous one, all old data is copied into the new buffer. If new capacity is less than previous one, only the latest newCapacity number of elements are preserved.

public void Resize(
uint newCapacity
)

Parameters

TypeNameDescription
uintnewCapacityNew capacity

bool TryDequeue​

Attempt to dequeue a value from the front of the queue.

public bool TryDequeue(
out T value
)

Parameters

TypeNameDescription
out TvalueDequeued value

Returns: True if dequeue was successful, false otherwise


Public Attributes​

Capacity​

Max number of elements the buffer can hold.


public int Capacity => buffer.Length;


Current​

Get the current value when enumerating the buffer


public object Current => buffer[enumeratorIndex];