libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
BufferIODevice Class Reference

IODevice backed by an in-memory Buffer. More...

#include <bufferiodevice.h>

Inheritance diagram for BufferIODevice:
Collaboration diagram for BufferIODevice:

Public Member Functions

 BufferIODevice (Buffer *buffer, ObjectBase *parent=nullptr)
 Constructs a BufferIODevice with an external Buffer.
 
 BufferIODevice (ObjectBase *parent=nullptr)
 Constructs a BufferIODevice with no buffer.
 
 ~BufferIODevice () override
 Destructor.
 
void setBuffer (Buffer *buffer)
 Sets the underlying Buffer.
 
Bufferbuffer () const
 Returns the underlying Buffer, or nullptr.
 
Error open (OpenMode mode) override
 Opens the device with the specified mode.
 
Error close () override
 Closes the 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.
 
int64_t bytesAvailable () const override
 Returns the number of bytes available for reading.
 
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.
 
Result< int64_tsize () const override
 Returns the total size of the device in bytes.
 
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 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.
 
ObjectBaseparent () 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 ObjectBaseListchildList () 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.
 
EventLoopeventLoop () 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)
 

Additional Inherited Members

- 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.
 
- Static Public Member Functions inherited from ObjectBase
static const MetaInfometaInfo ()
 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)
 
- Static Public Attributes inherited from ObjectBase
static constexpr const charaboutToDestroySignalName = 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
ObjectBasesignalSender ()
 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.
 

Detailed Description

IODevice backed by an in-memory Buffer.

BufferIODevice provides seekable, random-access I/O over a promeki Buffer. It is useful wherever an IODevice interface is expected but the data lives in memory — for example, unit-testing code that operates on IODevice, or wiring a DataStream to a memory buffer.

Ownership
BufferIODevice does not own the Buffer; the caller must ensure the Buffer outlives the device. If no external Buffer is provided, the device uses an internal default-constructed Buffer (invalid/empty) and all operations will fail until a valid Buffer is set.
Write growth
When opened for writing, writes past the current logical size() succeed as long as space remains in availSize(). The device advances size() to track the high-water mark. Writes that would exceed availSize() return an error.

Constructor & Destructor Documentation

◆ BufferIODevice() [1/2]

BufferIODevice::BufferIODevice ( Buffer buffer,
ObjectBase parent = nullptr 
)
explicit

Constructs a BufferIODevice with an external Buffer.

Parameters
bufferPointer to the Buffer to use. Must remain valid for the lifetime of this device.
parentThe parent object, or nullptr.

◆ BufferIODevice() [2/2]

BufferIODevice::BufferIODevice ( ObjectBase parent = nullptr)
explicit

Constructs a BufferIODevice with no buffer.

A buffer must be set via setBuffer() before the device can be opened.

Parameters
parentThe parent object, or nullptr.

Member Function Documentation

◆ atEnd()

bool BufferIODevice::atEnd ( ) const
overridevirtual

Returns true if the current position is at the end.

The default implementation returns true if pos() >= size().

Returns
true if at end of device.

Reimplemented from IODevice.

◆ buffer()

Buffer * BufferIODevice::buffer ( ) const
inline

Returns the underlying Buffer, or nullptr.

Returns
The buffer pointer.

◆ bytesAvailable()

int64_t BufferIODevice::bytesAvailable ( ) const
overridevirtual

Returns the number of bytes available for reading.

The default implementation returns 0.

Returns
The number of bytes available.

Reimplemented from IODevice.

◆ close()

Error BufferIODevice::close ( )
overridevirtual

Closes the device.

Returns
Error::Ok on success, or an error on failure.

Implements IODevice.

◆ isOpen()

bool BufferIODevice::isOpen ( ) const
overridevirtual

Returns true if the device is open.

Returns
true if the device is currently open.

Implements IODevice.

◆ isSequential()

bool BufferIODevice::isSequential ( ) const
overridevirtual

Returns true if the device is sequential (non-seekable).

Sequential devices (pipes, sockets) cannot be seeked. The default implementation returns false.

Returns
true if the device is sequential.

Reimplemented from IODevice.

◆ open()

Error BufferIODevice::open ( OpenMode  mode)
overridevirtual

Opens the device with the specified mode.

Parameters
modeThe open mode (ReadOnly, WriteOnly, ReadWrite, or Append).
Returns
Error::Ok on success, or an error on failure.

Implements IODevice.

◆ pos()

int64_t BufferIODevice::pos ( ) const
overridevirtual

Returns the current read/write position.

The default implementation returns 0.

Returns
The current position in bytes.

Reimplemented from IODevice.

◆ read()

int64_t BufferIODevice::read ( void data,
int64_t  maxSize 
)
overridevirtual

Reads up to maxSize bytes into data.

Parameters
dataPointer to the buffer to read into.
maxSizeMaximum number of bytes to read.
Returns
The number of bytes read, or -1 on error.

Implements IODevice.

◆ seek()

Error BufferIODevice::seek ( int64_t  pos)
overridevirtual

Seeks to the given byte offset from the beginning.

Parameters
posThe byte offset to seek to.
Returns
Error::Ok on success, or an error on failure.

Reimplemented from IODevice.

◆ setBuffer()

void BufferIODevice::setBuffer ( Buffer buffer)

Sets the underlying Buffer.

The device must not be open when calling this.

Parameters
bufferPointer to the Buffer to use.

◆ size()

Result< int64_t > BufferIODevice::size ( ) const
overridevirtual

Returns the total size of the device in bytes.

The default implementation returns 0.

Returns
A Result containing the device size, or an error.

Reimplemented from IODevice.

◆ write()

int64_t BufferIODevice::write ( const void data,
int64_t  maxSize 
)
overridevirtual

Writes up to maxSize bytes from data.

Parameters
dataPointer to the data to write.
maxSizeNumber of bytes to write.
Returns
The number of bytes written, or -1 on error.

Implements IODevice.


The documentation for this class was generated from the following file: