29 #ifndef Pt_System_IODevice_h
30 #define Pt_System_IODevice_h
32 #include <Pt/System/Api.h>
33 #include <Pt/System/IOError.h>
34 #include <Pt/System/Selectable.h>
36 #include <Pt/Signal.h>
58 typedef std::char_traits<char>::pos_type pos_type;
59 typedef std::char_traits<char>::off_type off_type;
60 typedef std::ios_base::seekdir seekdir;
94 std::size_t
read(
char* buffer, std::size_t n);
112 std::size_t
write(
const char* buffer, std::size_t n);
122 pos_type
seek(off_type offset, seekdir sd);
129 std::size_t
peek(
char* buffer, std::size_t n);
159 {
return _inputReady; }
168 {
return _outputReady; }
173 {
return _rbuf != 0; }
178 {
return _wbuf != 0; }
183 std::size_t rbuflen()
const
186 std::size_t ravail()
const
189 const char* wbuf()
const
192 std::size_t wbuflen()
const
195 std::size_t wavail()
const
207 virtual void onClose() = 0;
209 virtual void onSetTimeout(std::size_t timeout) = 0;
211 virtual std::size_t onBeginRead(
EventLoop& loop,
char* buffer, std::size_t n,
bool& eof) = 0;
213 virtual std::size_t onEndRead(
EventLoop& loop,
char* buffer, std::size_t n,
bool& eof) = 0;
215 virtual std::size_t onRead(
char* buffer, std::size_t count,
bool& eof) = 0;
217 virtual std::size_t onBeginWrite(
EventLoop& loop,
const char* buffer, std::size_t n) = 0;
219 virtual std::size_t onEndWrite(
EventLoop& loop,
const char* buffer, std::size_t n) = 0;
221 virtual std::size_t onWrite(
const char* buffer, std::size_t count) = 0;
223 virtual std::size_t onPeek(
char*, std::size_t)
226 virtual bool onSeekable()
const
229 virtual pos_type onSeek(off_type, std::ios::seekdir)
230 {
throw IOError(
"Could not seek on device"); }
232 virtual void onSync()
const
235 void setEof(
bool eof);
246 std::size_t _rbuflen;
249 std::size_t _wbuflen;
253 Pt::varint_t _reserved;
263 #endif // Pt_System_IODevice_h
Core module.
Definition: Allocator.h:33
void beginWrite(const char *buffer, std::size_t n)
Begins to write data.
bool isEof() const
Returns if the device has reached EOF.
void sync()
Synchronize device.
std::size_t endRead()
Ends reading data.
virtual ~IODevice()
Destructor.
Multicast Signal to call multiple slots.
Definition: Api-Signal.h:111
std::size_t write(const char *buffer, std::size_t n)
Write data to I/O device.
bool seekable() const
Returns true if device is seekable.
Signal< IODevice & > & inputReady()
Notifies about available data.
Definition: IODevice.h:158
IODevice()
Default Constructor.
EventLoop * loop() const
Returns the used event loop.
Definition: IODevice.h:200
pos_type seek(off_type offset, seekdir sd)
Moves the read position to the given offset.
void setTimeout(std::size_t timeout)
Sets the timeout for blocking I/O in milliseconds.
Signal< IODevice & > & outputReady()
Notifies when data can be written.
Definition: IODevice.h:167
virtual void onCancel()
Blocks until operation has cancelled.
void beginRead(char *buffer, std::size_t n)
Begins to read data.
void close()
Closes the device.
std::size_t read(char *buffer, std::size_t n)
Read data from I/O device.
Dispatches operations through an event loop.
Definition: Selectable.h:45
I/O error.
Definition: IOError.h:42
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:74
Endpoint for I/O operations.
Definition: IODevice.h:56
std::size_t peek(char *buffer, std::size_t n)
Peek data from I/O device without consuming them.
pos_type position()
Returns the current I/O position.
bool isReading() const
Returns true if the device is reading.
Definition: IODevice.h:172
virtual void onAttach(EventLoop &loop)
Attached to loop.
bool isWriting() const
Returns true if the device is writing.
Definition: IODevice.h:177
virtual void onDetach(EventLoop &loop)
Detached from loop.
std::size_t endWrite()
Ends writing data.