libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
iodevice.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
13#include <promeki/core/error.h>
14#include <promeki/core/result.h>
15
17
29class IODevice : public ObjectBase {
30 PROMEKI_OBJECT(IODevice, ObjectBase)
31 public:
33 enum OpenMode {
34 NotOpen = 0x00,
35 ReadOnly = 0x01,
36 WriteOnly = 0x02,
38 Append = 0x04 | WriteOnly
39 };
40
46
48 virtual ~IODevice();
49
55 virtual Error open(OpenMode mode) = 0;
56
61 virtual Error close() = 0;
62
67 virtual bool isOpen() const = 0;
68
75 virtual int64_t read(void *data, int64_t maxSize) = 0;
76
83 virtual int64_t write(const void *data, int64_t maxSize) = 0;
84
92 virtual void flush();
93
100 virtual int64_t bytesAvailable() const;
101
107 virtual bool waitForReadyRead(unsigned int timeoutMs = 0);
108
114 virtual bool waitForBytesWritten(unsigned int timeoutMs = 0);
115
123 virtual bool isSequential() const;
124
131
138 virtual int64_t pos() const;
139
146 virtual Result<int64_t> size() const;
147
154 virtual bool atEnd() const;
155
161 return _openMode;
162 }
163
168 bool isReadable() const {
169 return _openMode & ReadOnly;
170 }
171
176 bool isWritable() const {
177 return _openMode & WriteOnly;
178 }
179
184 Error error() const {
185 return _error;
186 }
187
191 void clearError() {
192 _error = Error();
193 return;
194 }
195
198
201
204
207
208 protected:
216 _openMode = mode;
217 return;
218 }
219
224 void setError(const Error &err) {
225 _error = err;
226 if(err.isError()) errorOccurredSignal.emit(err);
227 return;
228 }
229
230 private:
231 OpenMode _openMode = NotOpen;
232 Error _error;
233};
234
Lightweight error code wrapper for the promeki library.
Definition error.h:39
Abstract base class for all I/O devices.
Definition iodevice.h:29
bool isReadable() const
Returns true if the device is readable.
Definition iodevice.h:168
OpenMode openMode() const
Returns the current open mode.
Definition iodevice.h:160
IODevice(ObjectBase *parent=nullptr)
Constructs an IODevice.
Definition iodevice.h:45
PROMEKI_SIGNAL(errorOccurred, Error)
Emitted when an error occurs.
virtual Error seek(int64_t pos)
Seeks to the given byte offset from the beginning.
virtual bool waitForBytesWritten(unsigned int timeoutMs=0)
Waits until all pending data has been written or timeout.
virtual void flush()
Flushes any buffered output data to the underlying device.
PROMEKI_SIGNAL(aboutToClose)
Emitted just before the device is closed.
virtual ~IODevice()
Destructor.
virtual int64_t pos() const
Returns the current read/write position.
virtual bool isOpen() const =0
Returns true if the device is open.
void clearError()
Clears the error state to Ok.
Definition iodevice.h:191
virtual bool isSequential() const
Returns true if the device is sequential (non-seekable).
void setError(const Error &err)
Sets the error state and emits errorOccurred.
Definition iodevice.h:224
bool isWritable() const
Returns true if the device is writable.
Definition iodevice.h:176
virtual Error open(OpenMode mode)=0
Opens the device with the specified mode.
virtual int64_t bytesAvailable() const
Returns the number of bytes available for reading.
virtual bool atEnd() const
Returns true if the current position is at the end.
virtual int64_t read(void *data, int64_t maxSize)=0
Reads up to maxSize bytes into data.
Error error() const
Returns the current error state.
Definition iodevice.h:184
virtual Error close()=0
Closes the device.
virtual int64_t write(const void *data, int64_t maxSize)=0
Writes up to maxSize bytes from data.
OpenMode
Mode flags controlling how a device is opened.
Definition iodevice.h:33
@ WriteOnly
Open for writing only.
Definition iodevice.h:36
@ ReadOnly
Open for reading only.
Definition iodevice.h:35
@ NotOpen
Device is not open.
Definition iodevice.h:34
@ ReadWrite
Open for reading and writing.
Definition iodevice.h:37
@ Append
Open for appending (implies WriteOnly).
Definition iodevice.h:38
PROMEKI_SIGNAL(readyRead)
Emitted when data is available for reading.
virtual Result< int64_t > size() const
Returns the total size of the device in bytes.
virtual bool waitForReadyRead(unsigned int timeoutMs=0)
Waits until data is available for reading or timeout.
void setOpenMode(OpenMode mode)
Sets the open mode.
Definition iodevice.h:215
PROMEKI_SIGNAL(bytesWritten, int64_t)
Emitted when bytes have been written.
Dynamic array container wrapping std::vector.
Definition list.h:40
Base object for promeki.
Definition objectbase.h:129
ObjectBase * parent() const
Returns the parent object, if one. nullptr if none.
Definition objectbase.h:258
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19