Duck typing for byte-stream buffers

Introducing the BufferChunk object

The BufferChunk is the interface for dealing streaming buffers in Pothos. Its nothing more than a memory address, length in bytes, and a reference counted container to underlying memory, which could be a malloc slab, doubly mmapped buffer, DMA buffer... But none of that tells the user whats in the buffer. To get the data type of the buffer, take a look at the port it came from: Every port on a block has an associated data type set by the constructor.

Liberating the buffer from the stream

Now suppose that we decided that somewhere in the stream a particular selection of memory was of interest (recognized a preamble). We grab the buffer object, set its offset to the start of the preamble, and pass it downstream as a Pothos::Packet message object. We just threw out the data type! The downstream block has to make an assumption about the type of data held by the packet message.

Associating the data type with the buffer

The BufferChunk object now has an associated data type object. The Pothos::DType, which represents primitive data types, was re-written to be lightweight -- no strings, just easy to copy numbers representing the primitive data type, and the dimensionality (multiple primitives per type). So a buffer lifted from an input stream automatically carries with it the necessary information about its contents.

Unspecified ports and duck-typing blocks

When the port's data type is not specified at construction time, the data type remains "unspecified" (previously, the default port type was "byte"). With the type unspecified, the port can accept buffers of any data type, and the work() implementation can inspect the buffer at runtime for data type. This technique allows for creation of blocks that are agnostic to the data type.

Example: dynamic plotting widget

How about a plotter widget that can handle any buffer of any type on any port? We dont have to specify any data type parameter. The work() implementation can grab the buffer from an incoming stream or as a packet message, convert the buffer knowing its type to raster data, and plot it. Channel 0 could be a stream of complex floats, and channel 1 could be a packet message of doubles.

Last edited: Fri, Sep 26 2014 - 07:18AM