#include <Iguana/Utilities/classlib/iotools/ReadWriteBuffer.h>
Public Types | |
enum | { DEFAULT_SIZE = 4096 } |
Public Member Functions | |
unsigned char * | buffer (void) const |
void | bump (IOSize n) |
IOSize | capacity (void) const |
unsigned char * | current (void) const |
unsigned char * | dirty (void) const |
unsigned char * | end (void) const |
void | filled (IOSize n) |
void | get (void *data, IOSize n) |
IOSize | left (void) const |
unsigned char * | limit (void) const |
ptrdiff_t | offset (void) const |
ReadWriteBuffer & | operator= (const ReadWriteBuffer &x) |
unsigned char * | pback (void) const |
void | put (const void *data, IOSize n) |
ReadWriteBuffer (const ReadWriteBuffer &x) | |
ReadWriteBuffer (IOSize busize=DEFAULT_SIZE) | |
unsigned char * | reference (void) const |
void | reset (void) |
void | skip (IOSize n) |
void | taken (IOSize n) |
void | unget (const void *data, IOSize n) |
~ReadWriteBuffer (void) | |
Private Attributes | |
unsigned char * | m_buffer |
unsigned char * | m_current |
unsigned char * | m_dirty |
unsigned char * | m_end |
unsigned char * | m_limit |
unsigned char * | m_pback |
unsigned char * | m_reference |
This class makes it easier to buffer input and output to seekable stores such as Storage objects. The object maintains a reference location and a single combined buffer for both reads and writes. The buffer can be thought of as a cache window of data surrounding the reference location, the file position of the underlying store. The "real" file position is the current buffer position, which is usually at an offset from the store's file position: negative after buffered reads, positive after buffered writes.
Clients should use get() for buffered reads from the store. If left() returns zero to indicate the buffer is exhausted, clients should copy at most capacity() bytes from the external source into end(). They should then call filled() to tell the buffer how much was added, the method updates the other pointers and moves the reference location to the end of the newly added data. This matches normal reads: the store file pointer remains at the end of what has just been read. The "real" file pointer is the one maintained by the buffer at the beginning of the data. It is possible to add more data even if left() does not return zero provided capacity() returns non-zero and is not dirty().
Clients should use put() for buffered writes to the store. If capacity() returns zero to indicate the buffer is full, clients should write the section dirty() .. current () to the store. offset() indicates the distance from the reference location at which the write should occur, current() - dirty() is the number of bytes to write. Once the data has been written, the client should call taken() to move the pointers and the reference location to the end of the just written data. This matches normal writes; the store file pointer remains at the end of what was just written.
Writing to the buffer causes it to discard data already buffered for reads with filled(). More specifically, left() will return zero after a put() as long as there is dirty data in the buffer; all of it must be extracted with taken() before read can proceed. Thus, if left() returns zero, clients should notice the buffer is dirty() and synchronise with the external store before trying to read more input. This behaviour implies that some already data may be lost if the underlying input is unseekable (stream-like). Use separate read and write buffers for streaming.
The buffer also maintains a push-back area. Bytes already read from the buffer can be "unread", or new data can be pushed back to be accessed on the next get(). Arbitrary amounts of data can be pushed back. That data is however considered "virtual" as it may not correspond to the data in the backing store. Thus a put() discards all pushed-back data, and then writes at the last "real" position (remembered by pback()).
Definition at line 68 of file ReadWriteBuffer.h.
anonymous enum |
Definition at line 71 of file ReadWriteBuffer.h.
00071 { DEFAULT_SIZE = 4096 }; //< Default buffer size
lat::ReadWriteBuffer::ReadWriteBuffer | ( | IOSize | busize = DEFAULT_SIZE |
) |
lat::ReadWriteBuffer::ReadWriteBuffer | ( | const ReadWriteBuffer & | x | ) |
lat::ReadWriteBuffer::~ReadWriteBuffer | ( | void | ) |
unsigned char* lat::ReadWriteBuffer::buffer | ( | void | ) | const |
unsigned char* lat::ReadWriteBuffer::current | ( | void | ) | const |
unsigned char* lat::ReadWriteBuffer::dirty | ( | void | ) | const |
unsigned char* lat::ReadWriteBuffer::end | ( | void | ) | const |
unsigned char* lat::ReadWriteBuffer::limit | ( | void | ) | const |
ptrdiff_t lat::ReadWriteBuffer::offset | ( | void | ) | const |
ReadWriteBuffer& lat::ReadWriteBuffer::operator= | ( | const ReadWriteBuffer & | x | ) |
unsigned char* lat::ReadWriteBuffer::pback | ( | void | ) | const |
unsigned char* lat::ReadWriteBuffer::reference | ( | void | ) | const |
unsigned char* lat::ReadWriteBuffer::m_buffer [private] |
Definition at line 103 of file ReadWriteBuffer.h.
unsigned char* lat::ReadWriteBuffer::m_current [private] |
Definition at line 104 of file ReadWriteBuffer.h.
unsigned char* lat::ReadWriteBuffer::m_dirty [private] |
Definition at line 109 of file ReadWriteBuffer.h.
unsigned char* lat::ReadWriteBuffer::m_end [private] |
Definition at line 105 of file ReadWriteBuffer.h.
unsigned char* lat::ReadWriteBuffer::m_limit [private] |
Definition at line 106 of file ReadWriteBuffer.h.
unsigned char* lat::ReadWriteBuffer::m_pback [private] |
Definition at line 108 of file ReadWriteBuffer.h.
unsigned char* lat::ReadWriteBuffer::m_reference [private] |
Definition at line 107 of file ReadWriteBuffer.h.