PrevNextUpHome SophiaFramework UNIVERSE 5.3
SFXRingBuffer
Class which represents a ring buffer.
#include <SFXRingBuffer.h.hpp>
class SFXRingBuffer;
SFMTYPEDEFCLASS(SFXRingBuffer)

Collaboration diagram

 Collaboration diagram of SFXRingBufferClass

Description

SFXRingBuffer is the class which represents a ring buffer.

Figure 266. Overview of the Ring Buffer

Overview of the Ring Buffer

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.

To allocate the memory of the ring buffer, call the SFXRingBuffer::Allocate function. The size of the allocated buffer memory cannot be changed.

To write onto / read from the ring buffer, call the SFXRingBuffer::Write / SFXRingBuffer::Read function.

To get the size of the allocated buffer memory of the ring buffer, call the SFXRingBuffer::GetSize function. To get the size of the currently writeble / readable data, call the SFXRingBuffer::GetWritableSize / SFXRingBuffer::GetReadableSize function.

The allocated buffer memory will be released automatically, however you can explicitly release it by calling the SFXRingBuffer::Free function. By calling the SFXRingBuffer::Allocate function again after calling the SFXRingBuffer::Free function, you can change the size of the ring buffer. But the data which have been written into the ring buffer will be lost.

To use directly the write / read pointer which the ring buffer contains internally, call the SFXRingBuffer::GetWritableBuffer / SFXRingBuffer::SeekWrite / SFXRingBuffer::GetReadableBuffer / SFXRingBuffer::SeekRead function.

Example

SFXRingBuffer ring;   // ring buffer
UInt32        wsize;  // writable size onto the ring buffer
UInt32        rsize;  // readable size from the ring buffer

// set the size of the ring buffer to 256 byte(allocate 256 bytes of memory to the ring buffer)
ring.Allocate(256);

// the ring buffer is empty just after the memory allocation
wsize = ring.GetWritableSize();  // wsize = 256
rsize = ring.GetReadableSize();  // rsize = 0


SInt32 n = 123;
ring.Write(&n, sizeof(n));   // write 4 byte of signed integer(n = 123)

wsize = ring.GetWritableSize();  // wsize = 252
rsize = ring.GetReadableSize();  // rsize = 4

SInt32 m;
ring.Read(&m, sizeof(m));    // read 4 byte of signed integer(m = 123)

Reference

Ring Buffer Class

Member

Constructor/Destructor
SFXRingBuffer( Void )
Constructor of the SFXRingBuffer class.
~SFXRingBuffer( Void )
Destructor of the SFXRingBuffer class.
Public Functions
SFCError Allocate( UInt32 size )
Allocate the specified size of memory to this buffer.
static
SFXRingBufferConstRef
EmptyInstance( Void )
Get an empty ring buffer.
SInt32 FirstIndexOf( SFXBufferConstRef buffer , SInt32 index = SINT32_MINIMUM )
Get the first index to match the specified data, searching from the biginning index to the write pointer.
SInt32 FirstIndexOf( VoidConstPtr buffer , UInt32 size , SInt32 index = SINT32_MINIMUM )
Get the first index to match the specified data, searching from the biginning index to the write pointer.
SInt32 FirstIndexOf( Byte byte , SInt32 index = SINT32_MINIMUM )
Get the first index to match the specified data, searching from the biginning index to the write pointer.
Void Free( Void )
Release the internal buffer memory of this ring buffer.
VoidConstPtr GetReadableBuffer( UInt32Ptr size )
Get the read pointer to the internal buffer memory, which is allocated to this ring buffer.
UInt32 GetReadableSize( Void )
Get the total size of the data area that can be currently read from this ring buffer.[in bytes]
UInt32 GetSize( Void )
Get the size of the internal buffer memory.
VoidPtr GetWritableBuffer( UInt32Ptr size )
Get the write pointer to the internal buffer memory, which is allocated to this ring buffer.
UInt32 GetWritableSize( Void )
Get the total size of the data area that can be currently written into this ring buffer.[in bytes]
SInt32 LastIndexOf( SFXBufferConstRef buffer , SInt32 index = SINT32_MAXIMUM )
Get the last index to match the specified data, searching from the biginning index to the read pointer.
SInt32 LastIndexOf( VoidConstPtr buffer , UInt32 buffer , SInt32 index = SINT32_MAXIMUM )
Get the last index to match the specified data, searching from the biginning index to the read pointer.
SInt32 LastIndexOf( Byte byte , SInt32 index = SINT32_MAXIMUM )
Get the last index to match the specified data, searching from the biginning index to the read pointer.
SFCError Read( VoidPtr buffer , UInt32 size )
Read the data from this ring buffer.
SFCError Read( SFXBufferPtr buffer )
Read the data from this ring buffer.
Void Reset( Void )
Clear all the written data from this ring buffer.
SFCError SeekRead( UInt32 size )
Move forward the read pointer by the specified size.
SFCError SeekWrite( UInt32 size )
Move forward the write pointer by the specified size.
SFCError Write( VoidConstPtr buffer , UInt32 size )
Write the data onto this ring buffer.
SFCError Write( SFXBufferConstRef buffer )
Write the data onto this ring buffer.

SFXRingBuffer::SFXRingBuffer
Constructor of the SFXRingBuffer class.
[ public, explicit ]
SFXRingBuffer(Void);

Description

Different from the SFXBuffer::SFXBuffer constructor, this constructor will not allocate the internal buffer memory. Therefore, it is necessary to call the SFXRingBuffer::Allocate function for allocating the internal buffer memory.

Reference

SFXRingBuffer::Allocate | SFXBuffer::SFXBuffer


SFXRingBuffer::~SFXRingBuffer
Destructor of the SFXRingBuffer class.
[ public ]
~SFXRingBuffer(Void);

Description

[Note] Note
If the internal buffer memory is allocated, it will be released automatically with the SFXRingBuffer::Free function.

Reference

SFXRingBuffer::Free


SFXRingBuffer::Allocate
Allocate the specified size of memory to this buffer.
[ public ]
SFCError Allocate(
    UInt32 size   // memory size to allocate
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If insufficient memory: SFERR_NO_MEMORY

Description

This function allocates the specified size of memory to this ring buffer.

[Note] Note
The behaviours of some functions are not defined when they are called without the internal buffer memory being allocated.
[Caution] Note

The data which this ring buffer contains before this function is executed will be destroyed.

Example

SFXRingBuffer ring;

// allocate 256 bytes of memory to the ring buffer
if (ring.Allocate(256) == SFERR_NO_ERROR) {
    ...
}

Reference

SFXRingBuffer::Free | SFXRingBuffer::GetSize | SFXRingBuffer::SFXRingBuffer


SFXRingBuffer::EmptyInstance
Get an empty ring buffer.
[ public, static ]
SFXRingBufferConstRef EmptyInstance(Void);

Description

Get an instance that represents an empty ring buffer.


SFXRingBuffer::FirstIndexOf
Get the first index to match the specified data, searching from the biginning index to the write pointer.
[ public, const ]
SInt32 FirstIndexOf(
    SFXBufferConstRef buffer        // buffer object to match
    SInt32 index = SINT32_MINIMUM   // beginning index to search from
);
[ public, const ]
SInt32 FirstIndexOf(
    VoidConstPtr buffer             // buffer data to match
    UInt32 size                     // size of the buffer data to match
    SInt32 index = SINT32_MINIMUM   // beginning index to search from
);
[ public, const ]
SInt32 FirstIndexOf(
    Byte byte                       // Byte data to match
    SInt32 index = SINT32_MINIMUM   // beginning index to search from
);

Return value

  • If succeeds: The first index from the read pointer where the specified data is found.
  • If failed: -1

Description

This function gets the first index to match the specified data, searching from the biginning index(default: the read poinetr) to the write pointer.

You can specify the position other than the read pointer as the biginning index to search from in the index argument.

[Note] Note

The index value of the read pointer is regarded as "0".

The data at the write pointer is not included in the searching.

Example

static ByteConst data[] = {
        '1', '2', '3', '4', '5', '6', '7', '8'
};

SFXRingBuffer ring;
SFXBuffer buffer;

// set the size of the ring buffer to 256 bytes
if (ring.Allocate(256) == SFERR_NO_ERROR) {

    // write 8 byte of data
    if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {
        
        // get the first index of SFXRingBuffer object to match the specified data, searching from the beginning
        TRACE("FirstIndexOf('2') = %d", ring.FirstIndexOf('2'));  // FirstIndexOf('2') = 1
        TRACE("FirstIndexOf('9') = %d", ring.FirstIndexOf('9'));  // FirstIndexOf('9') = -1
    }
}

Reference

SFXRingBuffer::LastIndexOf


SFXRingBuffer::Free
Release the internal buffer memory of this ring buffer.
[ public ]
Void Free(Void);

Description

This function releases the internal buffer memory allocated to this ring buffer with the SFXRingBuffer::Allocate function.

[Note] Note
If no buffer memory is allocated to this ring buffer, nothing will happen.

Example

SFXRingBuffer ring;

// allocate 256 byte of memory
if (ring.Allocate(256) == SFERR_NO_ERROR) {
    ...
    
    ring.Free();  // release memory explicitly
}

Reference

SFXRingBuffer::Allocate


SFXRingBuffer::GetReadableBuffer
Get the read pointer to the internal buffer memory, which is allocated to this ring buffer.
[ public, const ]
VoidConstPtr GetReadableBuffer(
    UInt32Ptr size   // size of the data area that can be read directly from the internal buffer memory through the read pointer
);

Description

This function gets the read pointer to the internal buffer memory of the SFXHeap<Byte> type, which is allocated to this ring buffer.

Just after this function is executed, the size of the data area that can be read directly from the internal buffer memory through the read pointer will be set to the "size" argument.

You can read data directly from the internal buffer memory by using this read pointer and the size argument as the return value.

[Tip] Tip
As in the example below, this function is used to write data readable from the ring buffer onto the output stream.
[Caution] CAUTION

When the value of the read pointer is bigger than that of the write pointer and that of the write pointer does not equal 0, the value of the size argument as the return value which is the data size readable directly from the internal buffer does not equal the return value of the SFXRingBuffer::GetReadableSize function which is the total data size currently readable from the ring buffer.

In this case, to read the size of data returned by the SFXRingBuffer::GetReadableSize function, it is necessary to call both the SFXRingBuffer::GetReadableBuffer function and the SFXRingBuffer::SeekRead function twice.

For more details, see the below.

buffer[]: internal buffer of the ring buffer
B_SIZE:   size of the internal buffer
r_ptr:    read pointer
w_ptr:    write

[Ⅰ] In case of 0 ≦ r_ptr ≦ w_ptr ≦ B_SIZE - 1:

Just after this function is executed, 
"&buffer[r_ptr]" will be returned and the size argument will be set to "w_ptr - r_ptr". 
On the other hand, the return value of the SFXRingBuffer::GetReadableSize function will be also "w_ptr - r_ptr". 
In this case, by calling the SFXRingBuffer::GetReadableBuffer function and the SFXRingBuffer::SeekRead function once, 
the size of data, return value of the SFXRingBuffer::GetReadableSize function, can be read.

[Ⅱ] In case of 0 = w_ptr < r_ptr ≦ B_SIZE - 1:

Just after this function is executed, 
"&buffer[r_ptr]" will be returned and the size argument will be set to "B_SIZE - r_ptr". 
On the other hand, the return value of the SFXRingBuffer::GetReadableSize function will be also "B_SIZE - r_ptr". 
In this case, by calling the SFXRingBuffer::GetReadableBuffer function and the SFXRingBuffer::SeekRead function once, 
the size of data, return value of the SFXRingBuffer::GetReadableSize function, can be read too.

[Ⅲ] In case of 0 < w_ptr < r_ptr ≦ B_SIZE - 1:

Just after this function is executed, 
"&buffer[r_ptr]" will be returned and the size argument will be set to "B_SIZE - r_ptr". 
On the other hand, the return value of the SFXRingBuffer::GetReadableSize function will be also "B_SIZE - r_ptr +  w_ptr", 
which is different from the return value of the SFXRingBuffer::GetReadableSize function 
since "w_ptr ≠ 0" is valid.

After this, if "SFXRingBuffer::SeekRead(B_SIZE - r_ptr);" is executed, 
"r_ptr = 0" and "0 =  r_ptr < w_ptr ≦ B_SIZE - 1" will be valid. 

Then, if the SFXRingBuffer::GetReadableBuffer function and the SFXRingBuffer::SeekRead function is called once more(total twice), 
all the size of the data, return value of the SFXRingBuffer::GetReadableSize function, can be read.

Example

The code to write all the data readable from the ring buffer onto the output stream is as follows:

Void WriteRingBufferToFile(SFXBinaryStreamWriterRef stream, SFXRingBufferRef ring)
{
    UInt32 totalSize;     // total size of the data which can be read from the ring buffer
    UInt32 readableSize;  // size of the data which can be read from the internal buffer per operation
    VoidPtr readPtr;      // read pointer

    totalSize = ring.GetReadableSize();
    readPtr = ring.GetReadableBuffer(&readableSize);

    stream.Write(readPtr, readableSize);
    ring.SeekRead(readPtr, readableSize);

    if (totalSize -= readableSize > 0) {

        readPtr = ring.GetReadableBuffer(&readableSize);
        stream.Write(readPtr, readableSize);
    }
}

Reference

SFXRingBuffer::GetReadableSize | SFXRingBuffer::SeekRead | SFXRingBuffer::GetWritableBuffer | SFXHeap


SFXRingBuffer::GetReadableSize
Get the total size of the data area that can be currently read from this ring buffer.[in bytes]
[ public, const ]
UInt32 GetReadableSize(Void);

Return value

Total size of the data area that can be currently read from this ring buffer.[in bytes]

Description

This function gets the total size of the data area that can be currently read from this ring buffer.[in bytes]

[Note] Difference from the return value of the size argument of the SFXRingBuffer::GetReadableBuffer function

The return value of the size argument of the SFXRingBuffer::GetReadableBuffer function is the size of the data area that can be read directly from the internal buffer memory of the SFXHeap<Byte> type through the read pointer.

In general, the following inequality holds. For more details, see the description of the SFXRingBuffer::GetReadableBuffer function.

"Total data size currently readable from the ring buffer" >= "Data size directly readable from the internal buffer memory"

Example

static ByteConst data[] = {
        '1', '2', '3', '4', '5', '6', '7', '8'
};

SFXRingBuffer ring;
SFXBuffer buffer;

// set the size of the ring buffer to 256 bytes
if (ring.Allocate(256) == SFERR_NO_ERROR) {

    TRACE("GetSize = %d", ring.GetSize());                  // GetSize = 256
    TRACE("GetWritableSize = %d", ring.GetWritableSize());  // GetWritableSize = 256
    TRACE("GetReadableSize = %d", ring.GetReadableSize());  // GetReadableSize = 0
		

    // write 8 bytes of data onto the ring buffer
    if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {

        TRACE("GetSize = %d", ring.GetSize());                   // GetSize = 256
        TRACE("GetWritableSize = %d", ring.GetWritableSize());   // GetWritableSize = 248
        TRACE("GetReadableSize = %d", ring.GetReadableSize());   // GetReadableSize = 8

        // set the size of the buffer for reading to 4 byte
        buffer.SetSize(4);

        // read 4 byte of data from the ring buffer
        if (ring.Read(&buffer) == SFERR_NO_ERROR) {

            TRACE("GetSize = %d", ring.GetSize());                   // GetSize = 256
            TRACE("GetWritableSize = %d", ring.GetWritableSize());   // GetWritableSize = 252
            TRACE("GetReadableSize = %d", ring.GetReadableSize());   // GetReadableSize = 4

        }
    }
} 

Reference

SFXRingBuffer::Read | SFXRingBuffer::GetReadableBuffer | SFXRingBuffer::GetWritableSize | SFXHeap


SFXRingBuffer::GetSize
Get the size of the internal buffer memory.
[ public, const ]
UInt32 GetSize(Void);

Description

This function gets the size of the internal buffer memory allocated to this ring buffer with the SFXRingBuffer::Allocate function.

[Note] Note

From the property of the ring buffer, the following equation is always valid.

SFXRingBuffer::GetSize() == SFXRingBuffer::GetWritableSize() + SFXRingBuffer::GetReadableSize()

Example

static ByteConst data[] = {
        '1', '2', '3', '4', '5', '6', '7', '8'
};
SFXRingBuffer ring;
SFXBuffer buffer;

// set the size of ring buffer to 256 bytes
if (ring.Allocate(256) == SFERR_NO_ERROR) {

    TRACE("GetSize = %d", ring.GetSize());                  // GetSize = 256
    TRACE("GetWritableSize = %d", ring.GetWritableSize());  // GetWritableSize = 256
    TRACE("GetReadableSize = %d", ring.GetReadableSize());  // GetReadableSize = 0
		

    // write 8 byte of data
    if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {

        TRACE("GetSize = %d", ring.GetSize());                   // GetSize = 256
        TRACE("GetWritableSize = %d", ring.GetWritableSize());   // GetWritableSize = 248
        TRACE("GetReadableSize = %d", ring.GetReadableSize());   // GetReadableSize = 8

        // allocate buffer for reading
        buffer.SetSize(4);

        // read 4 byte of data
        if (ring.Read(&buffer) == SFERR_NO_ERROR) {

            TRACE("GetSize = %d", ring.GetSize());                   // GetSize = 256
            TRACE("GetWritableSize = %d", ring.GetWritableSize());   // GetWritableSize = 252
            TRACE("GetReadableSize = %d", ring.GetReadableSize());   // GetReadableSize = 4

        }
    }
}

Reference

SFXRingBuffer::Allocate | SFXRingBuffer::GetReadableSize | SFXRingBuffer::GetWritableSize


SFXRingBuffer::GetWritableBuffer
Get the write pointer to the internal buffer memory, which is allocated to this ring buffer.
[ public ]
VoidPtr GetWritableBuffer(
    UInt32Ptr size   // size of data area that can be written directly onto the internal buffer memory through the write pointer
);

Description

This function gets the write pointer to the internal buffer memory of the SFXHeap<Byte> type, which is allocated to this ring buffer.

Just after this function is executed, the size of the data area that can be written directly onto the internal buffer memory through the write pointer will be set to the "size" argument.

You can write data directly onto the internal buffer memory by using this write pointer and the size argument as the return value.

[Tip] Tip
As in the example below, this function is used to write data readable from the input stream onto the ring buffer.
[Caution] CAUTION

When the value of the write pointer may be bigger than that of the read pointer and that of the read pointer does not equal 0, the value of the size argument as the return value which is the data size writable directly onto the internal buffer does not equal the return value of the SFXRingBuffer::GetWritableSize function which is the total data size currently writable onto the ring buffer.

In this case, to write the size of data returned by the SFXRingBuffer::GetWritableSize function, it is necessary to call both the SFXRingBuffer::GetWritableBuffer function and the SFXRingBuffer::SeekWrite function twice.

For more details, see the below.

buffer[]: internal buffer of the ring buffer
B_SIZE:   size of the internal buffer
r_ptr:    read pointer
w_ptr:    write

[Ⅰ] In case of 0 ≦ w_ptr ≦ r_ptr ≦ B_SIZE - 1:

Just after this function is executed, 
"&buffer[w_ptr]" will be returned and the size argument will be set to "r_ptr - w_ptr". 
On the other hand, the return value of the SFXRingBuffer::GetWritableSize function will be also "r_ptr - w_ptr". 
In this case, by calling the SFXRingBuffer::GetWritableBuffer function and the SFXRingBuffer::SeekWrite function once, 
the size of data, return value of the SFXRingBuffer::GetWritableSize function, can be written.

[Ⅱ] In case of 0 = r_ptr < w_ptr ≦ B_SIZE - 1:

Just after this function is executed, 
"&buffer[w_ptr]" will be returned and the size argument will be set to "B_SIZE - w_ptr". 
On the other hand, the return value of the SFXRingBuffer::GetWritableSize function will be also "B_SIZE - w_ptr". 
In this case, by calling the SFXRingBuffer::GetWritableBuffer function and the SFXRingBuffer::SeekWrite function once, 
the size of data, return value of the SFXRingBuffer::GetWritableSize function, can be written too.


[Ⅲ] In case of 0 < r_ptr < w_ptr ≦ B_SIZE - 1:

Just after this function is executed, 
"&buffer[w_ptr]" will be returned and the size argument will be set to "B_SIZE - w_ptr". 
On the other hand, the return value of the SFXRingBuffer::GetWritableSize function will be "B_SIZE - w_ptr +  r_ptr",
which is different from the return value of the SFXRingBuffer::GetWritableSize function 
since "r_ptr ≠ 0" is valid.

After this, if "SFXRingBuffer::SeekWrite(B_SIZE - w_ptr);" is executed, 
"w_ptr = 0" and "0 =  w_ptr < r_ptr ≦ B_SIZE - 1" will be valid. 

Then, if the SFXRingBuffer::GetWritableBuffer function and the SFXRingBuffer::SeekWrite function is called once more(total twice), 
all the size of the data, return value of the SFXRingBuffer::GetWritableSize function, can be written.

Example

The code to write data readable from the input stream onto the ring buffer as many as possible is as follows:

Void WriteRingBufferFromFile(SFXBinaryStreamReaderRef stream, SFXRingBufferRef ring)
{
    UInt32 totalSize;     // total size of the data that can be written into the ring buffer
    UInt32 availSize;     // total size of the data that can be read from the input stream
    UInt32 writableSize;  // size of the data which can be written into the internal buffer per operation
    VoidPtr writePtr;     // write pointer
    UInt32 writtenSize;   // size of the data that has been written actually

    totalSize = ring.GetWritableSize();
    availSize = stream.GetReadableSize();

    if (availSize > 0) {
 
        writePtr = ring.GetWritableBuffer(&writableSize);
        writtenSize = (availSize < writableSize) ? availSize : writableSize;

        stream.Read(writePtr, writtenSize);
        ring.SeekWrite(writePtr, writtenSize);

        totalSize -= writtenSize;
        availSize -= writtenSize;
    }
    if (totalSize > 0 && availSize > 0) {

        writePtr = ring.GetWritableBuffer(&writableSize);
        writtenSize = (availSize < writableSize) ? availSize : writableSize;

        stream.Read(writePtr, writtenSize);
        ring.SeekWrite(writePtr, writtenSize);
    }
}

Reference

SFXRingBuffer::GetWritableSize | SFXRingBuffer::SeekWrite | SFXRingBuffer::GetReadableBuffer | SFXHeap


SFXRingBuffer::GetWritableSize
Get the total size of the data area that can be currently written into this ring buffer.[in bytes]
[ public, const ]
UInt32 GetWritableSize(Void);

Return value

Total size of the data area that can be currently written into this ring buffer.[in bytes]

Description

This function gets the total size of the data area that can be currently written into this ring buffer.[in bytes]

[Note] Difference from the return value of the size argument of the SFXRingBuffer::GetWritableBuffer function

The return value of the size argument of the SFXRingBuffer::GetWritableBuffer function is the size of the data area that can be written directly onto the internal buffer memory of the SFXHeap<Byte> type through the write pointer.

In general, the following inequality holds. For more details, see the description of the SFXRingBuffer::GetWritableBuffer function.

"Total data size currently writable onto the ring buffer" >= "Data size directly writable onto the internal buffer memory"

Example

static ByteConst data[] = {
        '1', '2', '3', '4', '5', '6', '7', '8'
};

SFXRingBuffer ring;
SFXBuffer buffer;

// set the size of the ring buffer to 256 bytes
if (ring.Allocate(256) == SFERR_NO_ERROR) {

    TRACE("GetSize = %d", ring.GetSize());                  // GetSize = 256
    TRACE("GetWritableSize = %d", ring.GetWritableSize());  // GetWritableSize = 256
    TRACE("GetReadableSize = %d", ring.GetReadableSize());  // GetReadableSize = 0
		

    // write 8 bytes of data onto the ring buffer
    if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {

        TRACE("GetSize = %d", ring.GetSize());                   // GetSize = 256
        TRACE("GetWritableSize = %d", ring.GetWritableSize());   // GetWritableSize = 248
        TRACE("GetReadableSize = %d", ring.GetReadableSize());   // GetReadableSize = 8

        // set the size of the buffer for reading to 4 byte
        buffer.SetSize(4);

        // read 4 byte of data from the ring buffer
        if (ring.Read(&buffer) == SFERR_NO_ERROR) {

            TRACE("GetSize = %d", ring.GetSize());                   // GetSize = 256
            TRACE("GetWritableSize = %d", ring.GetWritableSize());   // GetWritableSize = 252
            TRACE("GetReadableSize = %d", ring.GetReadableSize());   // GetReadableSize = 4

        }
    }
} 

Reference

SFXRingBuffer::Write | SFXRingBuffer::GetWritableBuffer | SFXRingBuffer::GetReadableSize | SFXHeap


SFXRingBuffer::LastIndexOf
Get the last index to match the specified data, searching from the biginning index to the read pointer.
[ public, const ]
SInt32 LastIndexOf(
    SFXBufferConstRef buffer        // buffer object to match
    SInt32 index = SINT32_MAXIMUM   // beginning index to search from
);
[ public, const ]
SInt32 LastIndexOf(
    VoidConstPtr buffer             // buffer data to match
    UInt32 buffer                   // size of the buffer data to match
    SInt32 index = SINT32_MAXIMUM   // beginning index to search from
);
[ public, const ]
SInt32 LastIndexOf(
    Byte byte                       // byte data to match
    SInt32 index = SINT32_MAXIMUM   // beginning index to search from
);

Return value

  • If succeeds: The last index from the read pointer where the specified data is found.
  • If failed: -1

Description

This function gets the last index to match the specified data, searching from the biginning index(default: the write poinetr) to the read pointer.

You can specify the position other than the write pointer as the biginning index to search from in the index argument.

[Note] Note

The index value of the read pointer is regarded as "0".

The data at the write pointer is not included in the searching.

Example

static ByteConst data[] = {
        '1', '2', '3', '4', '5', '6', '7', '8'
};

SFXRingBuffer ring;
SFXBuffer buffer;

// set the size of the ring buffer to 256 bytes
if (ring.Allocate(256) == SFERR_NO_ERROR) {

    // write 8 byte of data
    if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {
        
        // get the last index of SFXRingBuffer object to match the specified data, searching from the end
        TRACE("LastIndexOf('2') = %d", ring.LastIndexOf('2'));  // LastIndexOf('2') = 1
        TRACE("LastIndexOf('9') = %d", ring.LastIndexOf('9'));  // LastIndexOf('9') = -1
    }
}

Reference

SFXRingBuffer::FirstIndexOf


SFXRingBuffer::Read
Read the data from this ring buffer.
[ public ]
SFCError Read(
    VoidPtr buffer   // buffer for reading data
    UInt32 size      // data size to read
);
[ public ]
SFCError Read(
    SFXBufferPtr buffer   // buffer for reading data
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If argument is invalid: SFERR_INVALID_PARAM
  • If the value of the "size" argument exceeds the return value of SFXRingBuffer::GetReadableSize function: SFERR_FAILED

Description

This function reads the data into the specified argument from this ring buffer.

[Tip] Tip
The data whose size is less than or equals the return value of the SFXRingBuffer::GetReadableSize function can be read from this ring buffer.

Example

static ByteConst data[] = {
        '1', '2', '3', '4', '5', '6', '7', '8'
};

SFXRingBuffer ring;
SFXBuffer buffer;

// set the size of the ring buffer to 256 bytes
if (ring.Allocate(256) == SFERR_NO_ERROR) {

    // write 8 bytes of data onto the ring buffer
    if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {

        // set the size of the buffer for reading to 4 byte
        buffer.SetSize(4);

        // read 4 byte of data from the ring buffer
        if (ring.Read(&buffer) == SFERR_NO_ERROR) {

            // display the content of the buffer
            TRACE("%c, %c, %c, %c", buffer[0], buffer[1], buffer[2], buffer[3]); // 1, 2, 3, 4
                
        }
    }
}

Reference

SFXRingBuffer::Write | SFXRingBuffer::GetReadableSize


SFXRingBuffer::Reset
Clear all the written data from this ring buffer.
[ public ]
Void Reset(Void);

Description

This function clears all the written data from this ring buffer.

[Note] Note
Just after calling this function, the size of the readable data area will become "0", and the size of the writable data area will equal that of the ring buffer.

Reference

SFXRingBuffer::GetSize | SFXRingBuffer::GetReadableSize | SFXRingBuffer::GetWritableSize


SFXRingBuffer::SeekRead
Move forward the read pointer by the specified size.
[ public ]
SFCError SeekRead(
    UInt32 size   // size to move forward the read pointer
);

Return value

Description

This function moves forward the read pointer by the size specified in the "size" argument.

Example

static ByteConst data[] = {
        '1', '2', '3', '4', '5', '6', '7', '8'
};

SFXRingBuffer ring;
SFXBuffer buffer;

// set the size of the ring buffer to 256 bytes
if (ring.Allocate(256) == SFERR_NO_ERROR) {

    // write 8 bytes of data onto the ring buffer
    if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {

        // set the size of the buffer for reading to 4 byte
        buffer.SetSize(4);

        // move forward the read pointer
        ring.SeekRead(2);

        // read 4 byte of data from the ring buffer
        if (ring.Read(&buffer) == SFERR_NO_ERROR) {

            // display the content of the buffer
            TRACE("%c, %c, %c, %c", buffer[0], buffer[1], buffer[2], buffer[3]); // 3, 4, 5, 6
                
        }
    }
}

Reference

SFXRingBuffer::GetReadableBuffer | SFXRingBuffer::GetReadableSize


SFXRingBuffer::SeekWrite
Move forward the write pointer by the specified size.
[ public ]
SFCError SeekWrite(
    UInt32 size   // size to move forward the write pointer
);

Return value

Description

This function moves forward the write pointer by the size specified in the "size" argument.

Example

static ByteConst data[] = {
        '1', '2', '3', '4', '5', '6', '7', '8'
};

SFXRingBuffer ring;
SFXBuffer buffer;

// set the size of the ring buffer to 256 bytes
if (ring.Allocate(256) == SFERR_NO_ERROR) {

    // move forward the write pointer
    ring.SeekWrite(2);

    // write 8 byte of data
    if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {

        // set the size of the buffer for reading to 4 byte
        buffer.SetSize(4);

        // read 4 byte of data from the ring buffer
        if (ring.Read(&buffer) == SFERR_NO_ERROR) {

            // display the content of the buffer
            TRACE("%c, %c, %c, %c", buffer[0], buffer[1], buffer[2], buffer[3]); // , , 1, 2
                
        }
    }
}

Reference

SFXRingBuffer::GetWritableBuffer | SFXRingBuffer::GetWritableSize


SFXRingBuffer::Write
Write the data onto this ring buffer.
[ public ]
SFCError Write(
    VoidConstPtr buffer   // buffer for writing data
    UInt32 size           // data size to write
);
[ public ]
SFCError Write(
    SFXBufferConstRef buffer   // buffer for writing data
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If argument is invalid: SFERR_INVALID_PARAM
  • If "size" argument exceeds the return value of SFXRingBuffer::GetWritableSize function: SFERR_FAILED

Description

This function writes the data from the specified argument onto this ring buffer.

[Tip] Tip
The data whose size is less than or equals the return value of the SFXRingBuffer::GetWritableSize function can be written into this ring buffer.

Example

static ByteConst data[] = {
        '1', '2', '3', '4', '5', '6', '7', '8'
};

SFXRingBuffer ring;
SFXBuffer buffer;

// set the size of the ring buffer to 256 bytes
if (ring.Allocate(256) == SFERR_NO_ERROR) {

    // write 8 bytes of data onto the ring buffer
    if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {

        // set the size of the buffer for reading to 4 byte
        buffer.SetSize(4);

        // read 4 byte of data from the ring buffer
        if (ring.Read(&buffer) == SFERR_NO_ERROR) {

            // display the content of the buffer
            TRACE("%c, %c, %c, %c", buffer[0], buffer[1], buffer[2], buffer[3]); // 1, 2, 3, 4
                
        }
    }
}

Reference

SFXRingBuffer::Read | SFXRingBuffer::GetWritableSize