Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

IODevice Class Reference

Endpoint for I/O operations. More...

#include <cxxtools/iodevice.h>

List of all members.

Public Types

typedef std::char_traits<
char >::pos_type 
pos_type
typedef std::char_traits<
char >::off_type 
off_type
typedef std::ios_base::seekdir SeekDir
typedef IOS_OpenMode OpenMode

Public Member Functions

virtual ~IODevice ()
 Destructor.
void beginRead (char *buffer, size_t n)
size_t endRead ()
size_t read (char *buffer, size_t n)
 Read data from I/O device.
void beginWrite (const char *buffer, size_t n)
size_t endWrite ()
size_t write (const char *buffer, size_t n)
 Write data to I/O device.
bool seekable () const
 Returns true if device is seekable.
pos_type seek (off_type offset, std::ios::seekdir sd)
 Move the next read position to the given offset.
size_t peek (char *buffer, size_t n)
 Read data from I/O device without consuming them.
void sync ()
 Synchronize device.
pos_type position ()
 Returns the current I/O position.
bool eof () const
 Returns if the device has reached EOF.
bool async () const
 Returns true if the device operates in asynchronous mode.
virtual IODeviceImpl & ioimpl ()=0
bool reading () const
bool writing () const
char * rbuf () const
size_t rbuflen () const
const char * wbuf () const
size_t wbuflen () const

Public Attributes

Signal< IODevice & > inputReady
 Notifies about availavle data.
Signal< IODevice & > outputReady
 Notifies when data can be written.
Signal< IODevice & > errorOccured
 Notifies when an error occured.

Static Public Attributes

static const OpenMode Sync = IOS_Sync
static const OpenMode Async = IOS_Async
static const OpenMode Read = IOS_Read
static const OpenMode Write = IOS_Write
static const OpenMode AtEnd = IOS_AtEnd
static const OpenMode Append = IOS_Append
static const OpenMode Trunc = IOS_Trunc

Protected Member Functions

 IODevice ()
 Default Constructor.
virtual size_t onBeginRead (char *buffer, size_t n, bool &eof)=0
virtual size_t onEndRead (bool &eof)=0
virtual size_t onRead (char *buffer, size_t count, bool &eof)=0
 Read bytes from device.
virtual size_t onBeginWrite (const char *buffer, size_t n)=0
virtual size_t onEndWrite ()=0
virtual size_t onWrite (const char *buffer, size_t count)=0
 Write bytes to device.
virtual size_t onPeek (char *, size_t)
 Read data from I/O device without consuming them.
virtual bool onSeekable () const
 Returns true if device is seekable.
virtual pos_type onSeek (off_type, std::ios::seekdir)
 Move the next read position to the given offset.
virtual void onSync () const
 Synchronize device.
virtual size_t onSize () const
 Returns the size of the device.
void setEof (bool eof)
 Sets or unsets the device to eof.
void setAsync (bool async)
 Sets or unsets the device to async.

Protected Attributes

char * _rbuf
size_t _rbuflen
size_t _ravail
const char * _wbuf
size_t _wbuflen
size_t _wavail
void * _reserved


Detailed Description

This class serves as the base class for all kinds of I/O devices. The interface supports synchronous and asynchronous I/O operations, peeking and seeking. I/O buffers and I/O streams within the cxxtools framework use IODevices as endpoints and therefore fully feaured standard C++ compliant IOStreams can be constructed at runtime. Examples of IODevices are the SerialDevice, the endpoints of a Pipe or the FileDevice. A Selector can be used to wait on activity on an IODevice, which will send the Singal inputReady or outputReady of the IODevice that is ready to perform I/O.


Member Function Documentation

size_t read ( char *  buffer,
size_t  n 
)

Reads up to n bytes and stores them in buffer. Returns the number of bytes read, which may be less than requested and even 0 if the device operates in asynchronous (non-blocking) mode. In case of EOF the IODevice is set to eof.

Parameters:
buffer buffer where to place the data to be read.
n number of bytes to read
Returns:
number of bytes read, which may be less than requested.
Exceptions:
IOError 

size_t write ( const char *  buffer,
size_t  n 
)

Writes n bytes from buffer to this I/O device. Returns the number of bytes written, which may be less than requested and even 0 if the device operates in asynchronous (non-blocking) mode. In case of EOF the IODevice is set to eof.

Parameters:
buffer buffer containing the data to be written.
n number of bytes that should be written.
Returns:
number of bytes written, which may be less than requested.
Exceptions:
IOError 

bool seekable (  )  const

Tests if the device is seekable.

Returns:
true if the device is seekable, false otherwise.

pos_type seek ( off_type  offset,
std::ios::seekdir  sd 
)

Tries to move the current read position to the given offset. SeekMode determines the relative starting point of offset.

Parameters:
offset the offset the pointer should be moved by.
mode determines the relative starting offset.
Returns:
the new abosulte read positing.
Exceptions:
IOError 

size_t peek ( char *  buffer,
size_t  n 
)

Tries to extract up to n bytes from this object without consuming them. The bytes are stored in buffer, and the number of bytes peeked is returned.

Parameters:
buffer buffer where to place the data to be read.
n number of bytes to peek
Returns:
number of bytes peek.
Exceptions:
IOError 

void sync (  ) 

Commits written data to physical device.

Exceptions:
IOError 

pos_type position (  ) 

The current I/O position is returned or an IOError is thrown if the device is not seekable. Seekability can be tested with BasicIODevice::seekable().

Exceptions:
IOError 

bool eof (  )  const

Test if the I/O device has reached eof.

Returns:
true if the I/O device is usable, false otherwise.


Member Data Documentation

Signal<IODevice&> inputReady

This signal is send when the IODevice is monitored in a Selector or EventLoop and data becomes available.

This signal is send when the IODevice is monitored in a Selector or EventLoop and the device is ready to write data.

This signal is send when the device is monitored in a Selector or EventLoop and an error occured on the device.

Copyright © 2008 The Tntnet Development Team
Tntnet 1.6