IODevice Class Referenceabstract

#include <Pt/System/IODevice.h>

Endpoint for I/O operations. More...

Inherits Selectable.

Inherited by TcpSocket, UdpSocket, FileDevice, and SerialDevice.

Public Member Functions

virtual ~IODevice ()
void beginRead (char *buffer, std::size_t n)
 Begins to read data.
void beginWrite (const char *buffer, std::size_t n)
 Begins to write data.
void cancel ()
 Cancels all operations.
void close ()
 Closes the device.
void detach ()
 Remove from event loop and cancels outstanding operations.
std::size_t endRead ()
 Ends reading data.
std::size_t endWrite ()
 Ends writing data.
Signal< IODevice & > & inputReady ()
 Notifies about available data. More...
bool isEof () const
 Returns if the device has reached EOF.
bool isReading () const
 Returns true if the device is reading.
bool isWriting () const
 Returns true if the device is writing.
EventLooploop () const
 Returns the used event loop.
Signal< IODevice & > & outputReady ()
 Notifies when data can be written. More...
std::size_t peek (char *buffer, std::size_t n)
 Peek data from I/O device without consuming them. More...
pos_type position ()
 Returns the current I/O position. More...
std::size_t read (char *buffer, std::size_t n)
 Read data from I/O device. More...
bool run ()
 Run operation if it is ready.
pos_type seek (off_type offset, seekdir sd)
 Moves the read position to the given offset. More...
bool seekable () const
 Returns true if device is seekable.
void setActive (EventLoop &parent)
 Sets the parent loop, so that operations can be run.
void setTimeout (std::size_t timeout)
 Sets the timeout for blocking I/O in milliseconds.
void sync ()
 Synchronize device. More...
std::size_t write (const char *buffer, std::size_t n)
 Write data to I/O device. More...

Protected Member Functions

 IODevice ()
 Default Constructor.
virtual void onAttach (EventLoop &loop)
 Attached to loop.
virtual void onCancel ()
 Blocks until operation has cancelled.
virtual void onDetach (EventLoop &loop)
 Detached from loop.
virtual bool onRun ()=0
 Check if ready and run.

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 Pt 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. An EventLoop can be used to wait on activity on an IODevice. The signals inputReady or outputReady of the IODevice indicate that an I/O operation has finished.

Member Function Documentation

std::size_t read ( char *  buffer,
std::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.

bufferbuffer where to place the data to be read.
nnumber of bytes to read
number of bytes read, which may be less than requested.
std::size_t write ( const char *  buffer,
std::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. In case of EOF the IODevice is set to eof.

pos_type seek ( off_type  offset,
seekdir  sd 
std::size_t peek ( char *  buffer,
std::size_t  n 
void sync ( )

Commits written data to physical device.

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 seekable().

Signal<IODevice&>& inputReady ( )

This signal is send when the IODevice is monitored in an EventLoop and data becomes available.

Signal<IODevice&>& outputReady ( )

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