class::var::Ring

class var::Ring
  : public var::Data

Ring is a ring buffer (or circular buffer) that uses a local memory buffer for the data. It is a first in/first out style buffer. Where push() adds data to the front() and pop() removes data from the back().

The push() method can will pop() a value if the buffer is full and overflow is allowed.

The Ring can handle items of any type.

Ring<u32> ring(32); //32 32-bit word ring buffer
u32 next = 20;
ring.write(&next, 1);
ring.read(&next, 1); //read into next variable

Members

Ring(

T * buf,
const arg::Count count
)

Ring(

const arg::Count count
)

~Ring()
T & at(

const arg::ImplicitPosition position
)

const T & at(

const arg::ImplicitPosition position
) const

T & back()
const T & back() const
u32 count() const
u32 count_ready() const
T & front()
const T & front() const
bool is_empty() const
bool is_full() const
bool is_overflow_allowed() const
Ring & operator<<(

const Data & data
)

void pop()
int push(

const T & value
)

int rotate_backward()
int rotate_forward()
void set_overflow_allowed(

bool value
)

Data to_linear_data() const

Details

Ring(

T * buf,
const arg::Count count
)

Constructs a new ring buffer.

Parameters

  • buf A pointer to the data

  • size The number of bytes in the new ring buffer


Ring(

const arg::Count count
)

Constructs a new ring buffer.

Parameters

  • size The number of bytes to allocate for the new buffer.

~Ring()

T & at(

const arg::ImplicitPosition position
)

Accesses the item at the specified position.

Parameters

  • pos The position in the ring with 0 being the oldest valid data.

Returns

A reference to the item at pos.


const T & at(

const arg::ImplicitPosition position
) const

Accesses a read-only copy of the item at the specified position.

Parameters

  • pos The position in the ring with 0 being the oldest valid data.

Returns

A read-only reference to the item at pos.


T & back()

Access the item at the back of the ring (the oldest valid data).


const T & back() const

Access the item (read-only) at the back of the ring (the oldest valid data).


u32 count() const

u32 count_ready() const

Returns the number of items in the Ring that are ready to be popped.

If this value is zero, then front() and back() have undefined results. Also at() should not be passed parameters that are greater than or equaly to count_ready().


T & front()

Access the item at the back of the ring (the newest valid data).


const T & front() const

Access the item (read-only) at the back of the ring (the newest valid data).


bool is_empty() const

bool is_full() const

bool is_overflow_allowed() const

Returns true if overflow is allowed.


Ring & operator<<(

const Data & data
)


void pop()

Pops a value from the back of the buffer.

See also: push()


int push(

const T & value
)

Pushes a value on the front of the buffer.

The ring buffer is a first-in first-out buffer. So The first object pushed is the first one popped.


int rotate_backward()

Rotates all the values in the buffer from front to back.

Returns

Zero if the ring is full and the rotate is completed, -1 if the ring is not full (no action taken)

All items will be shifed toward the back. The back item will be carried to the front.

This method requires the buffer to be full (is_full() == true) or no action is taken.

The operation is completed by adjusting the head and the tail of the ring. Data does not move location in memory.


int rotate_forward()

Rotates all the values in the buffer from front to back.

Returns

Zero if the ring is full and the rotate is completed, -1 if the ring is not full (no action taken)

All items will be shifted toward the front. The front item will be carried to the back.

This method requires the buffer to be full (is_full() == true) or no action is taken.

The operation is completed by adjusting the head and the tail of the ring. Data does not move location in memory.


void set_overflow_allowed(

bool value
)

Sets a flag to allow overflow.

Parameters

  • value true to allow overflow

If overflow is allowed, writes will keep the newest data and drop the oldest data.

If overflow is disallowed, writes will only work until the buffer is full and will then return an error.


Data to_linear_data() const

Puts the data in the Ring buffer in a linear data object.


X

Thanks for Coming!

Subscribe to news and updates