Endpoint for I/O operations. More...

#include <cxxtools/iodevice.h>

Inheritance diagram for cxxtools::IODevice:
cxxtools::Selectable cxxtools::NonCopyable cxxtools::FileDevice cxxtools::net::TcpSocket tnt::GnuTlsStream tnt::OpensslStream tnt::GnuTls_iostream tnt::openssl_iostream

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 Types inherited from cxxtools::Selectable
enum  State { Disabled = 0, Idle = 1, Busy = 2, Avail = 3 }

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.
size_t 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.
void cancel ()
 Cancels asynchronous reading and writing.
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
size_t ravail () const
const char * wbuf () const
size_t wbuflen () const
size_t wavail () const
- Public Member Functions inherited from cxxtools::Selectable
virtual ~Selectable ()
 Destructor.
void setSelector (SelectorBase *parent)
SelectorBaseselector ()
const SelectorBaseselector () const
void close ()
 Closes the I/O device.
bool wait (std::size_t msecs=WaitInfinite)
bool enabled () const
 Test if the I/O device object is enabled.
bool idle () const
bool busy () const
bool avail () const
virtual SelectableImpl & simpl ()=0

Public Attributes

Signal< IODevice & > inputReady
 Notifies about available data.
Signal< IODevice & > outputReady
 Notifies when data can be written.

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
- Static Public Attributes inherited from cxxtools::Selectable
static const std::size_t WaitInfinite = Selector::WaitInfinite

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 void onCancel ()=0
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 Member Functions inherited from cxxtools::Selectable
 Selectable ()
 Default Constructor.
void setEnabled (bool isEnabled)
 Sets or unsets the device enabled.
void setState (State state)
virtual void onClose ()=0
 Closes the Selector.
virtual bool onWait (std::size_t msecs)=0
virtual void onAttach (SelectorBase &)=0
virtual void onDetach (SelectorBase &)=0
- Protected Member Functions inherited from cxxtools::NonCopyable
 NonCopyable ()

Protected Attributes

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

Detailed Description

Endpoint for I/O operations.

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 Signal inputReady or outputReady of the IODevice that is ready to perform I/O.

Member Typedef Documentation

typedef std::char_traits<char>::off_type cxxtools::IODevice::off_type
typedef std::char_traits<char>::pos_type cxxtools::IODevice::pos_type
typedef std::ios_base::seekdir cxxtools::IODevice::SeekDir

Constructor & Destructor Documentation

virtual cxxtools::IODevice::~IODevice ( )
virtual

Destructor.

cxxtools::IODevice::IODevice ( )
protected

Default Constructor.

Member Function Documentation

bool cxxtools::IODevice::async ( ) const

Returns true if the device operates in asynchronous mode.

void cxxtools::IODevice::beginRead ( char *  buffer,
size_t  n 
)
size_t cxxtools::IODevice::beginWrite ( const char *  buffer,
size_t  n 
)
void cxxtools::IODevice::cancel ( )

Cancels asynchronous reading and writing.

size_t cxxtools::IODevice::endRead ( )
size_t cxxtools::IODevice::endWrite ( )
bool cxxtools::IODevice::eof ( ) const

Returns if the device has reached EOF.

Test if the I/O device has reached eof.

Returns
true if the I/O device has reached eof, false otherwise.
virtual IODeviceImpl& cxxtools::IODevice::ioimpl ( )
pure virtual

Implemented in cxxtools::net::TcpSocket.

virtual size_t cxxtools::IODevice::onBeginRead ( char *  buffer,
size_t  n,
bool &  eof 
)
protectedpure virtual
virtual size_t cxxtools::IODevice::onBeginWrite ( const char *  buffer,
size_t  n 
)
protectedpure virtual
virtual void cxxtools::IODevice::onCancel ( )
protectedpure virtual
virtual size_t cxxtools::IODevice::onEndRead ( bool &  eof)
protectedpure virtual
virtual size_t cxxtools::IODevice::onEndWrite ( )
protectedpure virtual
virtual size_t cxxtools::IODevice::onPeek ( char *  ,
size_t   
)
inlineprotectedvirtual

Read data from I/O device without consuming them.

Reimplemented in cxxtools::FileDevice.

virtual size_t cxxtools::IODevice::onRead ( char *  buffer,
size_t  count,
bool &  eof 
)
protectedpure virtual

Read bytes from device.

Implemented in cxxtools::net::TcpSocket, and cxxtools::FileDevice.

virtual pos_type cxxtools::IODevice::onSeek ( off_type  ,
std::ios::seekdir   
)
inlineprotectedvirtual

Move the next read position to the given offset.

Reimplemented in cxxtools::FileDevice.

virtual bool cxxtools::IODevice::onSeekable ( ) const
inlineprotectedvirtual

Returns true if device is seekable.

Reimplemented in cxxtools::FileDevice.

virtual size_t cxxtools::IODevice::onSize ( ) const
inlineprotectedvirtual

Returns the size of the device.

virtual void cxxtools::IODevice::onSync ( ) const
inlineprotectedvirtual

Synchronize device.

Reimplemented in cxxtools::FileDevice.

virtual size_t cxxtools::IODevice::onWrite ( const char *  buffer,
size_t  count 
)
protectedpure virtual

Write bytes to device.

Implemented in cxxtools::net::TcpSocket, and cxxtools::FileDevice.

size_t cxxtools::IODevice::peek ( char *  buffer,
size_t  n 
)

Read data from I/O device without consuming them.

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
bufferbuffer where to place the data to be read.
nnumber of bytes to peek
Returns
number of bytes peek.
Exceptions
IOError
pos_type cxxtools::IODevice::position ( )

Returns the current I/O 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
size_t cxxtools::IODevice::ravail ( ) const
inline
char* cxxtools::IODevice::rbuf ( ) const
inline
size_t cxxtools::IODevice::rbuflen ( ) const
inline
size_t cxxtools::IODevice::read ( char *  buffer,
size_t  n 
)

Read data from I/O device.

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
bufferbuffer where to place the data to be read.
nnumber of bytes to read
Returns
number of bytes read, which may be less than requested.
Exceptions
IOError
bool cxxtools::IODevice::reading ( ) const
inline
pos_type cxxtools::IODevice::seek ( off_type  offset,
std::ios::seekdir  sd 
)

Move the next read position to the given offset.

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

Parameters
offsetthe offset the pointer should be moved by.
modedetermines the relative starting offset.
Returns
the new abosulte read positing.
Exceptions
IOError
bool cxxtools::IODevice::seekable ( ) const

Returns true if device is seekable.

Tests if the device is seekable.

Returns
true if the device is seekable, false otherwise.
void cxxtools::IODevice::setAsync ( bool  async)
protected

Sets or unsets the device to async.

void cxxtools::IODevice::setEof ( bool  eof)
protected

Sets or unsets the device to eof.

void cxxtools::IODevice::sync ( )

Synchronize device.

Commits written data to physical device.

Exceptions
IOError
size_t cxxtools::IODevice::wavail ( ) const
inline
const char* cxxtools::IODevice::wbuf ( ) const
inline
size_t cxxtools::IODevice::wbuflen ( ) const
inline
size_t cxxtools::IODevice::write ( const char *  buffer,
size_t  n 
)

Write data to I/O device.

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
bufferbuffer containing the data to be written.
nnumber of bytes that should be written.
Returns
number of bytes written, which may be less than requested.
Exceptions
IOError
bool cxxtools::IODevice::writing ( ) const
inline

Member Data Documentation

size_t cxxtools::IODevice::_ravail
protected
char* cxxtools::IODevice::_rbuf
protected
size_t cxxtools::IODevice::_rbuflen
protected
void* cxxtools::IODevice::_reserved
protected
size_t cxxtools::IODevice::_wavail
protected
const char* cxxtools::IODevice::_wbuf
protected
size_t cxxtools::IODevice::_wbuflen
protected
const OpenMode cxxtools::IODevice::Append = IOS_Append
static
const OpenMode cxxtools::IODevice::Async = IOS_Async
static
const OpenMode cxxtools::IODevice::AtEnd = IOS_AtEnd
static
Signal<IODevice&> cxxtools::IODevice::inputReady

Notifies about available data.

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

Signal<IODevice&> cxxtools::IODevice::outputReady

Notifies when data can be written.

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

const OpenMode cxxtools::IODevice::Read = IOS_Read
static
const OpenMode cxxtools::IODevice::Sync = IOS_Sync
static
const OpenMode cxxtools::IODevice::Trunc = IOS_Trunc
static
const OpenMode cxxtools::IODevice::Write = IOS_Write
static

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