ucommon
|
A thread-safe buffer for serializing and streaming class data. More...
#include <containers.h>
Public Member Functions | |
Buffer (size_t size, size_t count) | |
Create a buffer to hold a series of objects. | |
void | copy (void *data) |
Copy the next object from the buffer. | |
bool | copy (void *data, timeout_t timeout) |
Copy the next object from the buffer. | |
void * | get (timeout_t timeout) |
Get the next object from the buffer. | |
void * | get (void) |
Get the next object from the buffer. | |
unsigned | getCount (void) |
Get the number of objects in the buffer currently. | |
unsigned | getSize (void) |
Get the size of the buffer. | |
operator bool () | |
Test if there is data waiting in the buffer. | |
bool | operator! () |
Test if the buffer is empty. | |
void * | peek (unsigned item) |
Peek at pending data in buffer. | |
void | put (void *data) |
Put (copy) an object into the buffer. | |
bool | put (void *data, timeout_t timeout) |
Put (copy) an object into the buffer. | |
void | release (void) |
Release must be called when we get an object from the buffer. | |
virtual | ~Buffer () |
Deallocate buffer and unblock any waiting threads. |
A thread-safe buffer for serializing and streaming class data.
While the queue and stack operate by managing lists of reference pointers to objects of various mixed kind, the buffer holds physical copies of objects that being passed through it, and all must be the same size. The buffer class can be used stand-alone or with the typed bufferof template. The buffer is accessed in fifo order.
Definition at line 96 of file containers.h.
ucommon::Buffer::Buffer | ( | size_t | size, |
size_t | count | ||
) |
Create a buffer to hold a series of objects.
size | of each object in buffer. |
count | of objects in the buffer. |
void ucommon::Buffer::copy | ( | void * | data | ) |
Copy the next object from the buffer.
This blocks until an object becomes available. Buffer is auto-released.
data | pointer to copy into. |
bool ucommon::Buffer::copy | ( | void * | data, |
timeout_t | timeout | ||
) |
Copy the next object from the buffer.
Buffer is auto-released.
data | pointer to copy into. |
timeout | to wait when buffer is empty in milliseconds. |
void* ucommon::Buffer::get | ( | timeout_t | timeout | ) |
Get the next object from the buffer.
timeout | to wait when buffer is empty in milliseconds. |
Reimplemented in ucommon::bufferof< T >.
void* ucommon::Buffer::get | ( | void | ) |
Get the next object from the buffer.
This blocks until an object becomes available.
Reimplemented in ucommon::bufferof< T >.
unsigned ucommon::Buffer::getCount | ( | void | ) |
Get the number of objects in the buffer currently.
unsigned ucommon::Buffer::getSize | ( | void | ) |
Get the size of the buffer.
ucommon::Buffer::operator bool | ( | ) |
Test if there is data waiting in the buffer.
bool ucommon::Buffer::operator! | ( | ) |
Test if the buffer is empty.
void* ucommon::Buffer::peek | ( | unsigned | item | ) |
Peek at pending data in buffer.
This returns a pointer to objects relative to the head. In effect it is the same as get() for item = 0.
item | to examine in buffer. |
Reimplemented in ucommon::bufferof< T >.
void ucommon::Buffer::put | ( | void * | data | ) |
Put (copy) an object into the buffer.
This blocks while the buffer is full.
data | to copy into the buffer. |
bool ucommon::Buffer::put | ( | void * | data, |
timeout_t | timeout | ||
) |
Put (copy) an object into the buffer.
data | to copy into the buffer. |
timeout | to wait if buffer is full. |
void ucommon::Buffer::release | ( | void | ) |
Release must be called when we get an object from the buffer.
This is because the pointer we return is a physical pointer to memory that is part of the buffer. The object we get cannot be removed or the memory modified while the object is being used.