IODevice wrapping a C stdio FILE pointer. More...
#include <fileiodevice.h>


Public Types | |
| enum | Flag { NoFlags = 0x00 , OwnsFile = 0x01 } |
| Flags for the FILE* constructor. More... | |
Public Types inherited from IODevice | |
| enum | OpenMode { NotOpen = 0x00 , ReadOnly = 0x01 , WriteOnly = 0x02 , ReadWrite = ReadOnly | WriteOnly , Append = 0x04 | WriteOnly } |
| Mode flags controlling how a device is opened. More... | |
Public Types inherited from ObjectBase | |
| using | SlotVariantFunc = std::function< void(const VariantList &)> |
| Function type for invoking a slot with a list of Variants. | |
Public Member Functions | |
| FileIODevice (FILE *file, OpenMode mode, int flags=NoFlags, ObjectBase *parent=nullptr) | |
| Constructs a FileIODevice wrapping an external FILE. | |
| FileIODevice (const String &filename, ObjectBase *parent=nullptr) | |
| Constructs a FileIODevice with a filename. | |
| FileIODevice (ObjectBase *parent=nullptr) | |
| Constructs a FileIODevice with no FILE or filename. | |
| ~FileIODevice () override | |
| Destructor. Closes and fclose's if owned. | |
| FileIODevice (const FileIODevice &)=delete | |
| Deleted copy constructor (non-copyable). | |
| FileIODevice & | operator= (const FileIODevice &)=delete |
| Deleted copy assignment (non-copyable). | |
| void | setFilename (const String &filename) |
| Sets the filename for fopen-based opening. | |
| const String & | filename () const |
| Returns the filename, or an empty string if none was set. | |
| FILE * | file () const |
| Returns the underlying FILE pointer, or nullptr. | |
| bool | ownsFile () const |
| Returns true if the device owns the FILE pointer. | |
| FILE * | takeFile () |
| Transfers ownership of the FILE pointer to the caller. | |
| Error | open (OpenMode mode) override |
| Opens the device. | |
| Error | close () override |
| Closes the device. | |
| void | flush () override |
| Flushes any buffered output data to the underlying device. | |
| bool | isOpen () const override |
| Returns true if the device is open. | |
| int64_t | read (void *data, int64_t maxSize) override |
| Reads up to maxSize bytes into data. | |
| int64_t | write (const void *data, int64_t maxSize) override |
| Writes up to maxSize bytes from data. | |
| bool | isSequential () const override |
| Returns true if the device is sequential (non-seekable). | |
| Error | seek (int64_t pos) override |
| Seeks to the given byte offset from the beginning. | |
| int64_t | pos () const override |
| Returns the current read/write position. | |
| bool | atEnd () const override |
| Returns true if the current position is at the end. | |
Public Member Functions inherited from IODevice | |
| IODevice (ObjectBase *parent=nullptr) | |
| Constructs an IODevice. | |
| virtual | ~IODevice () |
| Destructor. | |
| virtual int64_t | bytesAvailable () const |
| Returns the number of bytes available for reading. | |
| virtual bool | waitForReadyRead (unsigned int timeoutMs=0) |
| Waits until data is available for reading or timeout. | |
| virtual bool | waitForBytesWritten (unsigned int timeoutMs=0) |
| Waits until all pending data has been written or timeout. | |
| virtual Result< int64_t > | size () const |
| Returns the total size of the device in bytes. | |
| OpenMode | openMode () const |
| Returns the current open mode. | |
| bool | isReadable () const |
| Returns true if the device is readable. | |
| bool | isWritable () const |
| Returns true if the device is writable. | |
| Error | error () const |
| Returns the current error state. | |
| void | clearError () |
| Clears the error state to Ok. | |
| PROMEKI_SIGNAL (readyRead) | |
| Emitted when data is available for reading. | |
| PROMEKI_SIGNAL (bytesWritten, int64_t) | |
| Emitted when bytes have been written. | |
| PROMEKI_SIGNAL (errorOccurred, Error) | |
| Emitted when an error occurs. | |
| PROMEKI_SIGNAL (aboutToClose) | |
| Emitted just before the device is closed. | |
Public Member Functions inherited from ObjectBase | |
| ObjectBase (ObjectBase *p=nullptr) | |
| Default ObjectBase constructor. | |
| virtual | ~ObjectBase () |
| Destructor. Emits aboutToDestroy, detaches from parent, and destroys children. | |
| ObjectBase * | parent () const |
| Returns the parent object, if one. nullptr if none. | |
| void | setParent (ObjectBase *p) |
| Sets the parent of this object. If the object already has a parent, it will be removed as a child from the old parent and added as a child to the new one. | |
| const ObjectBaseList & | childList () const |
| Returns a list of children of this object. | |
| template<typename... Args> | |
| int | registerSlot (Slot< Args... > *slot) |
| Registers a slot with this object and assigns it an ID. | |
| EventLoop * | eventLoop () const |
| Returns the EventLoop this object is affiliated with. | |
| void | moveToThread (EventLoop *loop) |
| Changes the EventLoop affinity of this object. | |
| int | startTimer (unsigned int intervalMs, bool singleShot=false) |
| Starts a timer on this object's EventLoop. | |
| void | stopTimer (int timerId) |
| Stops a timer previously started with startTimer(). | |
| template<typename... Args> | |
| PROMEKI_NAMESPACE_BEGIN void | connect (Signal< Args... > *signal, Slot< Args... > *slot) |
Static Public Member Functions | |
| static FileIODevice * | stdinDevice () |
| Returns a singleton FileIODevice wrapping C stdin. | |
| static FileIODevice * | stdoutDevice () |
| Returns a singleton FileIODevice wrapping C stdout. | |
| static FileIODevice * | stderrDevice () |
| Returns a singleton FileIODevice wrapping C stderr. | |
Static Public Member Functions inherited from ObjectBase | |
| static const MetaInfo & | metaInfo () |
| Returns the MetaInfo for the ObjectBase class. | |
| template<typename... Args> | |
| static void | connect (Signal< Args... > *signal, Slot< Args... > *slot) |
| connects a signal and slot together. This function assumes both the signal and slot exist in a ObjectBase or derived object | |
Additional Inherited Members | |
Public Attributes inherited from ObjectBase | |
| Signal< ObjectBase * > | aboutToDestroySignal = Signal< ObjectBase * >(this, aboutToDestroySignalName) |
Static Public Attributes inherited from ObjectBase | |
| static constexpr const char * | aboutToDestroySignalName = PROMEKI_STRINGIFY( aboutToDestroy ) "(" PROMEKI_STRINGIFY_ARGS( ObjectBase * ) ")" |
| static SignalMeta | aboutToDestroySignalMeta = SignalMeta(metaInfo(), aboutToDestroySignalName) |
Protected Member Functions inherited from IODevice | |
| void | setOpenMode (OpenMode mode) |
| Sets the open mode. | |
| void | setError (const Error &err) |
| Sets the error state and emits errorOccurred. | |
Protected Member Functions inherited from ObjectBase | |
| ObjectBase * | signalSender () |
| Returns the ObjectBase that emitted the signal currently being handled. | |
| virtual void | event (Event *e) |
| Called by EventLoop to deliver events to this object. | |
| virtual void | timerEvent (TimerEvent *e) |
| Called when a timer fires for this object. | |
IODevice wrapping a C stdio FILE pointer.
FileIODevice adapts a FILE* to the IODevice interface. It supports two usage patterns:
| FileIODevice::FileIODevice | ( | FILE * | file, |
| OpenMode | mode, | ||
| int | flags = NoFlags, |
||
| ObjectBase * | parent = nullptr |
||
| ) |
Constructs a FileIODevice wrapping an external FILE.
The device is immediately open with the given mode. If OwnsFile is set, the FILE will be fclose'd on close() or destruction.
| file | The FILE pointer to wrap. |
| mode | The open mode (ReadOnly, WriteOnly, or ReadWrite). |
| flags | Ownership flags. |
| parent | The parent object, or nullptr. |
|
explicit |
Constructs a FileIODevice with a filename.
The device is not open until open() is called. The FILE is created via fopen and owned by the device.
| filename | The path to the file. |
| parent | The parent object, or nullptr. |
|
explicit |
Constructs a FileIODevice with no FILE or filename.
Set a filename via setFilename() or provide a FILE via the other constructor before opening.
| parent | The parent object, or nullptr. |
|
overridevirtual |
|
overridevirtual |
|
inline |
Returns the underlying FILE pointer, or nullptr.
Returns the filename, or an empty string if none was set.
|
overridevirtual |
Flushes any buffered output data to the underlying device.
The default implementation is a no-op, suitable for unbuffered devices such as in-memory buffers. Subclasses that wrap buffered I/O (e.g. stdio FILE*) should override this.
Reimplemented from IODevice.
|
overridevirtual |
Returns true if the device is open.
Implements IODevice.
|
overridevirtual |
Returns true if the device is sequential (non-seekable).
Sequential devices (pipes, sockets) cannot be seeked. The default implementation returns false.
Reimplemented from IODevice.
|
inline |
Returns true if the device owns the FILE pointer.
|
overridevirtual |
Returns the current read/write position.
The default implementation returns 0.
Reimplemented from IODevice.
Reads up to maxSize bytes into data.
| data | Pointer to the buffer to read into. |
| maxSize | Maximum number of bytes to read. |
Implements IODevice.
Sets the filename for fopen-based opening.
The device must not be open when calling this.
| filename | The path to the file. |
|
static |
Returns a singleton FileIODevice wrapping C stderr.
The device is a lazy-initialized static local, guaranteed to outlive any other static object that calls this function. The FILE pointer is not owned (not fclose'd).
|
static |
Returns a singleton FileIODevice wrapping C stdin.
The device is a lazy-initialized static local, guaranteed to outlive any other static object that calls this function. The FILE pointer is not owned (not fclose'd).
|
static |
Returns a singleton FileIODevice wrapping C stdout.
The device is a lazy-initialized static local, guaranteed to outlive any other static object that calls this function. The FILE pointer is not owned (not fclose'd).
| FILE * FileIODevice::takeFile | ( | ) |
Transfers ownership of the FILE pointer to the caller.
After this call, the device is closed and the FILE is no longer managed by the device. The caller is responsible for fclose'ing the returned FILE.
Writes up to maxSize bytes from data.
| data | Pointer to the data to write. |
| maxSize | Number of bytes to write. |
Implements IODevice.