File I/O device with buffered reading. More...
#include <file.h>


Public Types | |
| enum | Flags { NoFlags = 0x00 , Create = 0x01 , Append = 0x02 , Truncate = 0x04 , Exclusive = 0x08 } |
| File-specific open flags. More... | |
| using | FileHandle = int |
| Platform-specific file handle type (POSIX). | |
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 | |
| File (const File &)=delete | |
| Deleted copy constructor (non-copyable). | |
| File & | operator= (const File &)=delete |
| Deleted copy assignment operator (non-copyable). | |
| File (ObjectBase *parent=nullptr) | |
| Default constructor. Creates a File with no filename. | |
| File (const String &fn, ObjectBase *parent=nullptr) | |
| Constructs a File with the given filename. | |
| File (const char *fn, ObjectBase *parent=nullptr) | |
| Constructs a File with the given C string filename. | |
| File (const FilePath &fp, ObjectBase *parent=nullptr) | |
| Constructs a File with the given file path. | |
| ~File () | |
| Destructor. Closes the file if it is open. | |
| const String & | filename () const |
| Returns the filename associated with this File. | |
| void | setFilename (const String &fn) |
| Sets the filename. | |
| int | flags () const |
| Returns the file-specific flags used at open time. | |
| FileHandle | handle () const |
| Returns the native file handle. | |
| Error | open (OpenMode mode) override |
| Opens the file with the given mode and no extra flags. | |
| Error | open (OpenMode mode, int fileFlags) |
| Opens the file with the given mode and file-specific flags. | |
| Error | close () override |
| Closes the file if it is open. | |
| bool | isOpen () const override |
| Returns true if the file is currently open. | |
| int64_t | write (const void *data, int64_t maxSize) override |
| Writes data to the file at the current position. | |
| bool | isSequential () const override |
| Returns false (files are seekable). | |
| Error | seek (int64_t offset) override |
| Seeks to an absolute byte offset. | |
| int64_t | pos () const override |
| Returns the current read/write position. | |
| Result< int64_t > | size () const override |
| Returns the total file size in bytes. | |
| bool | atEnd () const override |
| Returns true if the position is at or past the end. | |
| Result< int64_t > | seekFromCurrent (int64_t offset) const |
| Seeks relative to the current file position. | |
| Result< int64_t > | seekFromEnd (int64_t offset) const |
| Seeks relative to the end of the file. | |
| Error | truncate (int64_t offset) const |
| Truncates the file to the specified length. | |
| Result< size_t > | directIOAlignment () const |
| Returns the direct I/O alignment requirement for this file. | |
| Error | readBulk (Buffer &buf, int64_t size) |
| Reads bulk data from the current file position using direct I/O. | |
| Error | setDirectIO (bool enable) |
| Enables or disables direct I/O (bypass OS page cache). | |
| bool | isDirectIO () const |
| Returns true if direct I/O is enabled. | |
| Error | setSynchronous (bool enable) |
| Enables or disables synchronous writes. | |
| bool | isSynchronous () const |
| Returns true if synchronous writes are enabled. | |
| Error | setNonBlocking (bool enable) |
| Enables or disables non-blocking mode. | |
| bool | isNonBlocking () const |
| Returns true if non-blocking mode is enabled. | |
Public Member Functions inherited from BufferedIODevice | |
| BufferedIODevice (ObjectBase *parent=nullptr) | |
| Constructs a BufferedIODevice. | |
| virtual | ~BufferedIODevice () |
| Destructor. | |
| int64_t | read (void *data, int64_t maxSize) override |
| Reads up to maxSize bytes into data. | |
| int64_t | bytesAvailable () const override |
| Returns the number of bytes available for reading. | |
| Error | setReadBuffer (Buffer &&buf) |
| Replaces the internal read buffer. | |
| const Buffer & | readBuffer () const |
| Returns a const reference to the current read buffer. | |
| size_t | readBufferSize () const |
| Returns the capacity of the read buffer. | |
| Buffer | readLine (size_t maxLength=0) |
| Reads a line of text up to a newline character. | |
| Buffer | readAll () |
| Reads all available data from the device. | |
| Buffer | readBytes (size_t maxBytes) |
| Reads up to maxBytes bytes and returns as a Buffer. | |
| bool | canReadLine () const |
| Returns true if a complete line is available for reading. | |
| int64_t | peek (void *buf, size_t maxBytes) const |
| Reads up to maxBytes without consuming them. | |
| Buffer | peek (size_t maxBytes) const |
| Reads up to maxBytes without consuming them. | |
| void | setUnbuffered (bool enable) |
| Enables or disables unbuffered mode. | |
| bool | isUnbuffered () const |
| Returns true if unbuffered mode is enabled. | |
Public Member Functions inherited from IODevice | |
| IODevice (ObjectBase *parent=nullptr) | |
| Constructs an IODevice. | |
| virtual | ~IODevice () |
| Destructor. | |
| virtual void | flush () |
| Flushes any buffered output data to the underlying device. | |
| 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. | |
| 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 Attributes | |
| static constexpr FileHandle | FileHandleClosedValue = -1 |
| Sentinel value representing a closed file handle (POSIX). | |
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 | |
| int64_t | readFromDevice (void *data, int64_t maxSize) override |
| Reads raw data from the file descriptor. | |
| int64_t | deviceBytesAvailable () const override |
| Returns bytes available from the device (file). | |
Protected Member Functions inherited from BufferedIODevice | |
| size_t | bufferedBytesUnconsumed () const |
| Returns the number of buffered bytes not yet consumed. | |
| void | ensureReadBuffer () |
| Ensures the read buffer is allocated. | |
| void | resetReadBuffer () |
| Resets the read buffer cursors. | |
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. | |
Additional Inherited Members | |
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 | |
Public Attributes inherited from ObjectBase | |
| Signal< ObjectBase * > | aboutToDestroySignal = Signal< ObjectBase * >(this, aboutToDestroySignalName) |
File I/O device with buffered reading.
Derives from BufferedIODevice, providing a full IODevice interface for file I/O. Supports direct I/O, synchronous writes, and non-blocking mode via setDirectIO(), setSynchronous(), and setNonBlocking().
File-specific open flags (Create, Append, Truncate, Exclusive) are passed via the open(OpenMode, int) overload. The IODevice open(OpenMode) overload opens with no extra flags.
When direct I/O is enabled, unbuffered mode is automatically forced. The previous unbuffered state is saved and restored when direct I/O is disabled.
File-specific open flags.
These extend the IODevice OpenMode and are passed to open(OpenMode, int).
| File::File | ( | ObjectBase * | parent = nullptr | ) |
Default constructor. Creates a File with no filename.
| parent | The parent object, or nullptr. |
| File::File | ( | const String & | fn, |
| ObjectBase * | parent = nullptr |
||
| ) |
Constructs a File with the given filename.
| fn | The path to the file. |
| parent | The parent object, or nullptr. |
| File::File | ( | const char * | fn, |
| ObjectBase * | parent = nullptr |
||
| ) |
Constructs a File with the given C string filename.
| fn | The path to the file. |
| parent | The parent object, or nullptr. |
| File::File | ( | const FilePath & | fp, |
| ObjectBase * | parent = nullptr |
||
| ) |
Constructs a File with the given file path.
| fp | The path to the file. |
| parent | The parent object, or nullptr. |
|
overridevirtual |
Returns true if the position is at or past the end.
Reimplemented from IODevice.
|
overridevirtual |
|
overrideprotectedvirtual |
Returns bytes available from the device (file).
Reimplemented from BufferedIODevice.
Returns the direct I/O alignment requirement for this file.
When the file is open, returns the filesystem block size via fstat. This is the alignment required for file offsets, buffer addresses, and transfer sizes when direct I/O is enabled.
Returns the filename associated with this File.
|
inline |
Returns the file-specific flags used at open time.
|
inline |
Returns the native file handle.
|
inline |
Returns true if direct I/O is enabled.
|
inline |
Returns true if non-blocking mode is enabled.
|
overridevirtual |
Returns true if the file is currently open.
Implements IODevice.
|
overridevirtual |
Returns false (files are seekable).
Reimplemented from IODevice.
|
inline |
Returns true if synchronous writes are enabled.
Opens the file with the given mode and file-specific flags.
| mode | The open mode (ReadOnly, WriteOnly, or ReadWrite). |
| fileFlags | A bitmask of Flags (Create, Append, Truncate, Exclusive). |
|
overridevirtual |
Returns the current read/write position.
Reimplemented from IODevice.
Reads bulk data from the current file position using direct I/O.
Reads size bytes from the current file position into buf, using direct I/O for the aligned interior of the region and normal I/O for any unaligned head and tail bytes. This is the optimal strategy for reading large media payloads (image data, audio samples) from container files, bypassing the OS page cache for the bulk transfer while handling misaligned boundaries gracefully.
This method calls shiftData() on buf so that the direct I/O portion lands on an aligned destination address. After a successful call, buf.data() points to the first byte of the requested payload. The caller only needs to ensure the buffer is allocated with at least directIOAlignment() alignment and that allocSize() is at least size + directIOAlignment():
The buffer must be host-accessible and allocSize() must be large enough to accommodate the shift plus size bytes.
If the region is smaller than one alignment block, or if the alignment cannot be determined, a normal (non-DIO) read is used for the entire region. If direct I/O fails at runtime (e.g. the filesystem does not support O_DIRECT), the method falls back to normal I/O for that portion automatically.
If fewer than size bytes remain in the file, the read succeeds and buf.size() reflects the actual number of bytes read (which will be less than size).
| buf | Destination buffer (must be host-accessible). |
| size | Number of bytes to read. |
Reads raw data from the file descriptor.
Implements BufferedIODevice.
Seeks relative to the current file position.
| offset | The byte offset relative to the current position. |
Seeks relative to the end of the file.
| offset | The byte offset relative to the end (typically negative or 0). |
Enables or disables direct I/O (bypass OS page cache).
When enabled, unbuffered mode is automatically forced on. When disabled, the previous unbuffered state is restored. If the file is open, the O_DIRECT flag is toggled via fcntl.
| enable | true to enable direct I/O. |
Sets the filename.
Only allowed when the file is not open.
| fn | The new filename. |
Enables or disables non-blocking mode.
If the file is open, the O_NONBLOCK flag is toggled via fcntl.
| enable | true to enable non-blocking operations. |
Enables or disables synchronous writes.
If the file is open, the O_SYNC flag is toggled via fcntl.
| enable | true to enable synchronous writes. |
Returns the total file size in bytes.
Reimplemented from IODevice.
Truncates the file to the specified length.
| offset | The new file size in bytes. |
Writes data to the file at the current position.
| data | Pointer to the data to write. |
| maxSize | Number of bytes to write. |
Implements IODevice.