stream_helper
is a helper object that provides the mwx::stream
interface. It generates a helper object that references a data class, and performs data input/output through the helper object.Below, a helper object bs
is generated from the array b
of smplbuf, and data input is performed using the mwx::stream::operator <<()
operator.
smplbuf_u8<32> b;
auto&& bs = b.get_stream_helper(); // Helper object
// Generate data sequence
uint8_t FOURCHARS[]={'A', 'B', 'C', 'D'};
bs << FOURCHARS;
bs << ';';
bs << uint32_t(0x30313233); // "0123"
bs << format(";%d", 99);
Serial << b << crlf; // Output to Serial is via smplbuf_u8<32> class
// Result: ABCD;0123;99
Overview
stream_helper
behaves as if the data array is a stream.
Internally, it keeps track of the read/write position within the data array. It behaves as follows:
- When reading or writing, the read/write position moves to the next position.
- After reading the last data or appending data to the end, the read/write position becomes the end position.
- When the read/write position is at the end,
available()
returnsfalse
.- Reading is not possible.
- Writing appends data if within writable range.
Creating stream_helper
stream_helper
is created from member functions of data classes (smplbuf, EEPROM).
auto&& obj_helper = obj.get_stream_helper()
// obj is an object of a data class, and obj_helper's type is long, so it is received with auto&&.
Methods
rewind()
void rewind()
Moves the read/write position to the beginning.
seek()
int seek(int offset, int whence = MWX_SEEK_SET)
Sets the read/write position.
whence | Position set |
---|---|
MWX_SEEK_SET | Sets from the beginning. offset of 0 means the same as rewind() . |
MWX_SEEK_CUR | Moves by offset from the current position. |
MWX_SEEK_END | Sets to the end position. offset of 0 sets to the end. -1 moves to the last character. |
tell()
int tell()
Returns the read/write position. Returns -1
if at the end position.
available()
int available()
Returns 0
if the read/write position is at the end. Otherwise, returns a non-zero value.