libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
iodevice.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <cstdint>
14#include <promeki/namespace.h>
15#include <promeki/objectbase.h>
16#include <promeki/error.h>
17#include <promeki/result.h>
18#include <promeki/uniqueptr.h>
19#include <promeki/sharedptr.h>
20
21PROMEKI_NAMESPACE_BEGIN
22
39class IODevice : public ObjectBase {
40 PROMEKI_OBJECT(IODevice, ObjectBase)
41 public:
43 using UPtr = UniquePtr<IODevice>;
44
55 using Shared = SharedPtr<IODevice, false>;
56
58 enum OpenMode {
59 NotOpen = 0x00,
60 ReadOnly = 0x01,
61 WriteOnly = 0x02,
62 ReadWrite = ReadOnly | WriteOnly,
63 Append = 0x04 | WriteOnly
64 };
65
70 IODevice(ObjectBase *parent = nullptr) : ObjectBase(parent) {}
71
73 virtual ~IODevice();
74
80 virtual Error open(OpenMode mode) = 0;
81
86 virtual Error close() = 0;
87
92 virtual bool isOpen() const = 0;
93
100 virtual int64_t read(void *data, int64_t maxSize) = 0;
101
108 virtual int64_t write(const void *data, int64_t maxSize) = 0;
109
117 virtual void flush();
118
125 virtual int64_t bytesAvailable() const;
126
132 virtual bool waitForReadyRead(unsigned int timeoutMs = 0);
133
139 virtual bool waitForBytesWritten(unsigned int timeoutMs = 0);
140
148 virtual bool isSequential() const;
149
155 virtual Error seek(int64_t pos);
156
163 virtual int64_t pos() const;
164
171 virtual Result<int64_t> size() const;
172
179 virtual bool atEnd() const;
180
185 OpenMode openMode() const { return _openMode; }
186
191 bool isReadable() const { return _openMode & ReadOnly; }
192
197 bool isWritable() const { return _openMode & WriteOnly; }
198
203 Error error() const { return _error; }
204
208 void clearError() {
209 _error = Error();
210 return;
211 }
212
214 PROMEKI_SIGNAL(readyRead);
215
217 PROMEKI_SIGNAL(bytesWritten, int64_t);
218
220 PROMEKI_SIGNAL(errorOccurred, Error);
221
223 PROMEKI_SIGNAL(aboutToClose);
224
225 protected:
232 void setOpenMode(OpenMode mode) {
233 _openMode = mode;
234 return;
235 }
236
241 void setError(const Error &err) {
242 _error = err;
243 if (err.isError()) errorOccurredSignal.emit(err);
244 return;
245 }
246
247 private:
248 OpenMode _openMode = NotOpen;
249 Error _error;
250};
251
252PROMEKI_NAMESPACE_END
253
254#endif // PROMEKI_ENABLE_CORE