PrevNextUpHome SophiaFramework UNIVERSE 5.3

14.4. Ring Buffer Class

SFXRingBuffer is the class which represents a ring buffer.

Figure 14.1. Overview of the Ring Buffer

Overview of the Ring Buffer

Conceptually, the ring buffer is a chunk of memory arranged in the circular manner.

The ring buffer has the read pointer and the write pointer which advance clockwisely. When data are written into the ring buffer through the write pointer, the write pointer will advance. When data are read from the ring buffer through the read pointer, the read pointer will advance.

[Note] Note
The read pointer cannot go beyond the write pointer. Conversely, the write pointer cannot go beyond the read pointer.

Example 14.10. Method to use the SFXRingBuffer class

Byte data1[] = {'1', '2', '3', '4', '5', '6', '7', '8'};
Byte data2[] = {'9', 'a', 'b', 'c', 'd', 'e'};
SFXRingBuffer ring;
SFXBuffer buffer;

// set size of ring buffer to 12 bytes

// write 8-bytes data
ring.Write(data1, sizeof(data1));

// reserve buffer for reading

// read 4 byte
ring.Read(&buffer); // buffer = "1234"

// write 6-bytes data
ring.Write(data2, sizeof(data2));

// reserve buffer for reading

// read 10 bytes
ring.Read(&buffer); // buffer = "56789abcde"