io::ForwardingReader
The ForwardingReader
is a helper class implementing io::IReader, allowing to read
data from a source instance of a reader while forwarding a copy of the data to a destination
io::IWriter. This is useful to fork a input data stream e.g. to a debug writer while
consuming it. In contrast to io::SplitWriter, which splits a data stream at the writer
side, the ForwardingReader
allows forking a data stream at the reader side of a channel.
Properties
Memory consumption:
sizeof(IReader&) + sizeof(IWriter&) + (sizeof(::estd::slice<uint8_t>) * 2)
Public API
The public API of ForwardingReader
consists of a constructor and the inherited io::IReader
API:
/**
* Constructs a ForwardingReader connecting a given source and destination.
*/
ForwardingReader(IReader& source, IWriter& destination);
/**
* Returns source's maxSize().
*/
size_t maxSize() const override;
/**
* Calling peek() will call peek() on the source reader and if data is available allocate
* the data on the destination writer.
*/
::estd::slice<uint8_t> peek() const override;
/**
* Calling release() will copy the data to the destination and commit it. After that,
* release() is called on the source to free the input data.
*/
void release() override;
Usage Example
The following example shows a simplified usage of ForwardingReader
:
1
2/**
3 * Function placeholder for consuming data from a given reader.
4 */
5void consumeData(::io::IReader& reader);
6
7/**
8 * This usage example demonstrates the usage of ::io::ForwardingReader. It's very high level but
9 * gives an idea about possible use cases.
10 */
11TEST(ForwardingReader, UsageExample)
12{
13 using Queue = ::io::MemoryQueue<1024, 16>;
14 // Create one input queue.
15 Queue input;
16 // Create one queue to mirror read data to a debug device.
17 Queue debugOutput;
18 io::MemoryQueueReader<Queue> inputReader{input};
19 io::MemoryQueueWriter<Queue> debugWriter{debugOutput};
20 // Create the ForwardingReader.
21 ::io::ForwardingReader r{inputReader, debugWriter};
22
23 // This function can be called cyclically to process data from the input queue. The consumed
24 // data is also copied to the debugOutput queue.
25 consumeData(r);
26}
27