![]() ![]() ![]()
|
BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1 |

SFXRingBuffer is a class that represents a ring buffer.

Conceptually, the ring buffer is a circular buffer. There are the write pointer and the read pointer that proceed clockwisely on the ring buffer.
After writing data on the ring buffer, the write pointer proceeds. Data can be read until the position written by the write pointer through the read pointer.
The write pointer cannot overtake the read pointer by writing data on the ring buffer.
UInt32 wsize, rsize; // create the SFXRingBuffer object SFXRingBuffer ring; // allocate 256 bytes of memory ring.Allocate(256); // since the buffer is empty just after memory allocation, // size of area to be written is 256, i.e. wsize = 0 wsize = ring.GetWritableSize(); // since the buffer is empty just after memory allocation, // size of area to be read is 0, i.e. rsize = 0 rsize = ring.GetReadableSize(); // write 4 byte of data SInt32 n = 123; ring.Write(&n, sizeof(n)); // size of area to be written is 252, i.e. wsize = 252 wsize = ring.GetWritableSize(); // size of area to be read is 4, i.e. rsize = 4 rsize = ring.GetReadableSize(); // read 4 byte of data SInt32 m; ring.Read(&m, sizeof(m)); if (n == m) { // OK }
| Constructor/Destructor |
|---|
|
SFXRingBuffer( Void ) Constructor of SFXRingBuffer class.
|
|
~SFXRingBuffer( Void ) Destructor of the SFXRingBuffer class.
|
| Public Functions | |
|---|---|
| SFCError |
Allocate(
UInt32 size
) Allocate buffer memory.
|
| static SFXRingBufferConstRef |
EmptyInstance( Void ) Get an empty ring buffer.
|
| SInt32 |
FirstIndexOf(
SFXBufferConstRef buffer
, SInt32 index = SINT32_MINIMUM
) FirstIndexOf( Byte byte , SInt32 index = SINT32_MINIMUM ) FirstIndexOf( VoidConstPtr buffer , UInt32 size , SInt32 index = SINT32_MINIMUM ) Get the first index of SFXRingBuffer object to match with the specified data, searching from the beginning.
|
| Void |
Free( Void ) Release the buffer memory that the SFXRingBuffer object manages.
|
| VoidConstPtr |
GetReadableBuffer(
UInt32Ptr size
) Get the read pointer.
|
| UInt32 |
GetReadableSize( Void ) Get the size of data area that can be read through the read pointer.
|
| UInt32 |
GetSize( Void ) Get the size of buffer memory.
|
| VoidPtr |
GetWritableBuffer(
UInt32Ptr size
) Get the write pointer.
|
| UInt32 |
GetWritableSize( Void ) Get the size of data area that can be written through the write pointer.
|
| SInt32 |
LastIndexOf(
SFXBufferConstRef buffer
, SInt32 index = SINT32_MAXIMUM
) LastIndexOf( Byte byte , SInt32 index = SINT32_MAXIMUM ) LastIndexOf( VoidConstPtr buffer , UInt32 buffer , SInt32 index = SINT32_MAXIMUM ) Get the last index of SFXRingBuffer object to match with the specified data, searching from the end.
|
| SFCError |
Read(
VoidPtr buffer
, UInt32 size
) Read( SFXBufferPtr buffer ) Read data.
|
| Void |
Reset( Void ) Clear the written data.
|
| SFCError |
SeekRead(
UInt32 size
) Set forward the read pointer.
|
| SFCError |
SeekWrite(
UInt32 size
) Set forward the write pointer.
|
| SFCError |
Write(
VoidConstPtr buffer
, UInt32 size
) Write( SFXBufferConstRef buffer ) Write data.
|
[ public, explicit ] SFXRingBuffer(Void);
Since memory is not allocated for the ring buffer in the constructor, it is necessary to call the SFXRingBuffer::Allocate function.
[ public ] ~SFXRingBuffer(Void);
When buffer memory is allocated, it will be released automatically by calling the SFXRingBuffer::Free function.
Since buffer memory is not allocated in the constructor, it is necessary to allocate buffer memory by using the SFXRingBuffer::Allocate function.
The behaviour is not defined when calling other functions without allocation.
SFXRingBuffer ring;
// allocate 256 byte of memory
if (ring.Allocate(256) == SFERR_NO_ERROR) {
...
}
[ public, static ] SFXRingBufferConstRef EmptyInstance(Void);
Get an instance that represents an empty ring buffer.
[ public, const ] SInt32 FirstIndexOf( SFXBufferConstRef buffer // buffer object to match with SInt32 index = SINT32_MINIMUM // starting index to search from );
[ public, const ] SInt32 FirstIndexOf( VoidConstPtr buffer // buffer data to match with UInt32 size // size of buffer data to match with SInt32 index = SINT32_MINIMUM // starting index to search from );
[ public, const ] SInt32 FirstIndexOf( Byte byte // byte data to match with SInt32 index = SINT32_MINIMUM // starting index to search from );
The index value of read pointer is regarded as "0".
To search from other than the beginning, specify the starting index to match with. (The origin index is 0.)
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) {
// write 8 byte of data
if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {
// get the first index of SFXRingBuffer object to match with 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
}
}
[ public ] Void Free(Void);
Release the buffer memory allocated by using the SFXRingBuffer::Allocate function.
If the SFXRingBuffer object has no buffer memory, nothing happens.
SFXRingBuffer ring; // allocate 256 byte of memory if (ring.Allocate(256) == SFERR_NO_ERROR) { ... ring.Free(); // release memory explicitly }
[ public, const ] VoidConstPtr GetReadableBuffer( UInt32Ptr size // size of data area that can be read through the read pointer );
Get the read pointer to the buffer memory allocated internally. By using this pointer, data can be read directly from the internal buffer.
The "size" parameter will be set to the size of data area that can be read through the read pointer. This size does not always equal the return value of SFXRingBuffer::GetReadableSize function, and usually it will be less than that value.
To read the size of data returned by the SFXRingBuffer::GetReadableSize function, it is necessary to call both SFXRingBuffer::GetReadableBuffer and SFXRingBuffer::SeekRead function several times.
Write all the data to be read from the ring buffer onto the output stream.
Void WriteRingBufferToFile(SFXBinaryStreamWriterRef stream, SFXRingBufferRef ring)
{
UInt32 totalSize; // total size of data that can be read from the ring buffer
totalSize = ring.GetReadableSize();
while (totalSize > 0) {
UInt32 readableSize;
VoidPtr readPtr; // read pointer
readPtr = ring.GetReadableBuffer(&readableSize);
stream.Write(readPtr, readableSize);
ring.SeekRead(readPtr, readableSize);
totalSize -= readableSize;
}
}
SFXRingBuffer::GetReadableBuffer | SFXRingBuffer::GetReadableSize | SFXRingBuffer::SeekRead
[ public, const ] UInt32 GetReadableSize(Void);
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
}
}
}
[ public, const ] UInt32 GetSize(Void);
From the property of ring buffer, the following equation is always valid.
SFXRingBuffer::GetSize = SFXRingBuffer::GetReadableSize + SFXRingBuffer::GetWritableSize
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
}
}
}
[ public ] VoidPtr GetWritableBuffer( UInt32Ptr size // size of data area that can be written through the write pointer );
Get the write pointer to the buffer memory allocated internally. By using this pointer, data can be written directly onto the internal buffer.
The "size" parameter will be set to the size of data area that can be written through the write pointer. This size does not always equal the return value of SFXRingBuffer::GetWritableSize function, and usually it will be less than that value.
To write the size of data returned by the SFXRingBuffer::GetWritableSize function, it is necessary to call both SFXRingBuffer::GetWritableBuffer and SFXRingBuffer::SeekWrite function several times.
Read from the input stream, and write all the data onto the ring buffer.
Void WriteRingBufferFromFile(SFXBinaryStreamReaderRef stream, SFXRingBufferRef ring)
{
UInt32 totalSize; // total size of data that can be written onto the ring buffer
UInt32 availSize; // total size of data that can be read from the file
totalSize = ring.GetWritableSize();
availSize = stream.GetReadableSize();
while (totalSize > 0 && availSize > 0) {
UInt32 writableSize;
VoidPtr writePtr;
UInt32 writtenSize; // data size to write actually
writePtr = ring.GetWritableBuffer(&writableSize);
{
writtenSize = (availSize < writableSize) ? availSize : writableSize;
stream.Read(writePtr, writtenSize);
}
ring.SeekRead(readPtr, readableSize);
totalSize -= writtenSize;
availSize -= writtenSize;
}
}
SFXRingBuffer::GetWritableBuffer | SFXRingBuffer::GetWritableSize | SFXRingBuffer::SeekWrite
[ public, const ] UInt32 GetWritableSize(Void);
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
}
}
}
[ public, const ] SInt32 LastIndexOf( SFXBufferConstRef buffer // buffer object to match with SInt32 index = SINT32_MAXIMUM // starting index to search from );
[ public, const ] SInt32 LastIndexOf( VoidConstPtr buffer // buffer data to match with UInt32 buffer // size of buffer data to match with SInt32 index = SINT32_MAXIMUM // starting index to search from );
[ public, const ] SInt32 LastIndexOf( Byte byte // byte data to match with SInt32 index = SINT32_MAXIMUM // starting index to search from );
The index value of read pointer is regarded as "0".
To search from other than the end, specify the starting index to match with. (The origin index is 0.)
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) {
// write 8 byte of data
if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {
// get the last index of SFXRingBuffer object to match with 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
}
}
[ public ] SFCError Read( VoidPtr buffer // buffer for reading data UInt32 size // data size to read );
[ public ] SFCError Read( SFXBufferPtr buffer // buffer for reading data );
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) {
// write 8 byte of data
if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {
// allocate buffer for reading
buffer.SetSize(4);
// read 4 byte of data
if (ring.Read(&buffer) == SFERR_NO_ERROR) {
// display the content of ring buffer
TRACE("%c, %c, %c, %c", buffer[0], buffer[1], buffer[2], buffer[3]); // 1, 2, 3, 4
}
}
}
[ public ] Void Reset(Void);
Just after calling the SFXRingBuffer::Reset function, the size of area to be read will be "0" and the size of area to be written will equal that of ring buffer.
The SFXRingBuffer::SeekRead sets forward the read pointer by the value of "size" parameter.
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) {
// write 8 byte of data
if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {
// allocate buffer for reading
buffer.SetSize(4);
// set forward the read pointer
ring.SeekRead(2);
// read 4 byte of data
if (ring.Read(&buffer) == SFERR_NO_ERROR) {
// display the content of ring buffer
TRACE("%c, %c, %c, %c", buffer[0], buffer[1], buffer[2], buffer[3]); // 3, 4, 5, 6
}
}
}
The SFXRingBuffer::SeekWrite sets forward the write pointer by the value of "size" parameter.
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) {
// set forward the write pointer
ring.SeekWrite(2);
// write 8 byte of data
if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {
// allocate buffer for reading
buffer.SetSize(4);
// read 4 byte of data
if (ring.Read(&buffer) == SFERR_NO_ERROR) {
// display the content of ring buffer
TRACE("%c, %c, %c, %c", buffer[0], buffer[1], buffer[2], buffer[3]); // , , 1, 2
}
}
}
[ public ] SFCError Write( VoidConstPtr buffer // buffer for writing data UInt32 size // data size to write );
[ public ] SFCError Write( SFXBufferConstRef buffer // buffer for writing data );
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) {
// write 8 byte of data
if (ring.Write(data, sizeof(data)) == SFERR_NO_ERROR) {
// allocate buffer for reading
buffer.SetSize(4);
// read 4 byte of data
if (ring.Read(&buffer) == SFERR_NO_ERROR) {
// display the content of ring buffer
TRACE("%c, %c, %c, %c", buffer[0], buffer[1], buffer[2], buffer[3]); // 1, 2, 3, 4
}
}
}
|
Copyright (C) 2002 - 2009 Sophia Cradle, Inc. All Rights Reserved. |
![]() ![]() ![]()
|