UCommon
Public Member Functions | Protected Member Functions
ucommon::Buffer Class Reference

A thread-safe buffer for serializing and streaming class data. More...

#include <containers.h>

Inheritance diagram for ucommon::Buffer:
Inheritance graph
[legend]
Collaboration diagram for ucommon::Buffer:
Collaboration graph
[legend]

Public Member Functions

unsigned count (void)
 Get the number of objects in the buffer currently. More...
 
 operator bool ()
 Test if there is data waiting in the buffer. More...
 
bool operator! ()
 Test if the buffer is empty. More...
 
unsigned size (void)
 Get the size of the buffer. More...
 

Protected Member Functions

 Buffer (size_t typesize, size_t count)
 Create a buffer to hold a series of objects. More...
 
void copy (void *data)
 Copy the next object from the buffer. More...
 
bool copy (void *data, timeout_t timeout)
 Copy the next object from the buffer. More...
 
void * get (timeout_t timeout)
 Get the next object from the buffer. More...
 
void * get (void)
 Get the next object from the buffer. More...
 
virtual void * invalid (void) const
 
void * peek (unsigned item)
 Peek at pending data in buffer. More...
 
void put (void *data)
 Put (copy) an object into the buffer. More...
 
bool put (void *data, timeout_t timeout)
 Put (copy) an object into the buffer. More...
 
void release (void)
 Release must be called when we get an object from the buffer. More...
 
virtual ~Buffer ()
 Deallocate buffer and unblock any waiting threads.
 
- Protected Member Functions inherited from ucommon::Conditional
void broadcast (void)
 Signal the conditional to release all waiting threads.
 
 Conditional ()
 Initialize and construct conditional.
 
void lock (void)
 Lock the conditional's supporting mutex.
 
void signal (void)
 Signal the conditional to release one waiting thread.
 
void unlock (void)
 Unlock the conditional's supporting mutex.
 
bool wait (timeout_t timeout)
 Conditional wait for signal on millisecond timeout. More...
 
bool wait (struct timespec *timeout)
 Conditional wait for signal on timespec timeout. More...
 
void wait (void)
 Wait (block) until signalled.
 
 ~Conditional ()
 Destroy conditional, release any blocked threads.
 

Additional Inherited Members

- Static Protected Member Functions inherited from ucommon::Conditional
static pthread_condattr_t * initializer (void)
 Support function for getting conditional attributes for realtime scheduling. More...
 
static void set (struct timespec *hires, timeout_t timeout)
 Convert a millisecond timeout into use for high resolution conditional timers. More...
 

Detailed Description

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. For this reason the buffer is normally used through the bufferof<type> template rather than stand-alone. The buffer is accessed in fifo order.

Author
David Sugar dyfet.nosp@m.@gnu.nosp@m.telep.nosp@m.hony.nosp@m..org

Definition at line 96 of file containers.h.

Constructor & Destructor Documentation

ucommon::Buffer::Buffer ( size_t  typesize,
size_t  count 
)
protected

Create a buffer to hold a series of objects.

Parameters
sizeof each object in buffer.
countof objects in the buffer.

Member Function Documentation

void ucommon::Buffer::copy ( void *  data)
protected

Copy the next object from the buffer.

This blocks until an object becomes available. Buffer is auto-released.

Parameters
datapointer to copy into.
bool ucommon::Buffer::copy ( void *  data,
timeout_t  timeout 
)
protected

Copy the next object from the buffer.

Buffer is auto-released.

Parameters
datapointer to copy into.
timeoutto wait when buffer is empty in milliseconds.
Returns
true if object copied, or false if timed out.
unsigned ucommon::Buffer::count ( void  )

Get the number of objects in the buffer currently.

Returns
number of objects buffered.
void* ucommon::Buffer::get ( timeout_t  timeout)
protected

Get the next object from the buffer.

Parameters
timeoutto wait when buffer is empty in milliseconds.
Returns
pointer to next object in the buffer or NULL if timed out.
void* ucommon::Buffer::get ( void  )
protected

Get the next object from the buffer.

This blocks until an object becomes available.

Returns
pointer to next object from buffer.
ucommon::Buffer::operator bool ( )

Test if there is data waiting in the buffer.

Returns
true if buffer has data.
bool ucommon::Buffer::operator! ( )

Test if the buffer is empty.

Returns
true if the buffer is empty.
void* ucommon::Buffer::peek ( unsigned  item)
protected

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.

Parameters
itemto examine in buffer.
Returns
pointer to item or NULL if invalid item number.
void ucommon::Buffer::put ( void *  data)
protected

Put (copy) an object into the buffer.

This blocks while the buffer is full.

Parameters
datato copy into the buffer.
bool ucommon::Buffer::put ( void *  data,
timeout_t  timeout 
)
protected

Put (copy) an object into the buffer.

Parameters
datato copy into the buffer.
timeoutto wait if buffer is full.
Returns
true if copied, false if timed out while full.
void ucommon::Buffer::release ( void  )
protected

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.

unsigned ucommon::Buffer::size ( void  )

Get the size of the buffer.

Returns
size of the buffer.

The documentation for this class was generated from the following file: