Operators and methods by mwx::stream
via a stream_helper helper object that references a smplbuf array of type uint8_t
.
Helper object type names are resolved by auto&&
because they are long. The interfaces defined in mwx::stream
, such as <<
operator, can be used for this object.
The generated helper object bs
starts reading/writing from the beginning of the main array b
when it is created. If it is at the end of the array, data is added by append()
. Each time a read/write operation is performed, the position is moved to the next one.
Helper functions can use the >>
operator for reading.
The uint8_t
type smplbuf_strm_u8??
is also available in the stream(stream) interface, so several methods for streams can be used.
Example
This is a container class with an array structure inside. The maximum size of the buffer is determined at initialization, and it behaves as a variable-length array up to that maximum size.
smplbuf
is a container class that provides array operations on memory areas specified by element type T
and memory allocation method alloc
. Since the specification of alloc
is complicated, alias definitions using using
are used.
Here is an example of object declaration. Immediately after the declaration, a method call is made for initialization. The maximum size of each object is 128 bytes immediately after initialization, and the size is 0. Use while extending the size as needed.
Alias classes are defined for uint8_t
type.
Elements can be accessed like normal arrays, using the [] operator, etc., and iterators can also be used to access elements.
The class also has push_back()
method, which enables some type of algorithm from standard C++ library.
Declares a container of type T
and size N
. After the declaration, call the initialization method.
smplbuf_local
allocates an area by a fixed array inside. It can also be initialized by the constructor.
In smplbuf_attach
, specify the first pointer T* buf
of the buffer to be used, the initial size size
and maximum size N
of the array. Initialization by the constructor is also possible.
The smplbuf_heap
allocates memory in the HEAP area (a memory area that cannot be released but can be allocated at any time). Once allocated, this area cannot be released, so it is usually defined as a global area. Allocation is done by init_heap()
. Memory allocation by the constructor is not allowed. Please call init_heap()
to use this function.
When creating a global object, initialization by the constructor is not possible. Please call initialization functions init_local()
,attach()
,init_heap()
at the beginning of execution (setup()
is recommended).
Initializer list { ... }
can be used to initialize members. Except for use in the constructor in a local declaration of smplbuf_local
, it is valid after calling the initialization method.
right side value of assignment operator (smplbuf_local
, smplbuf_attach
, smplbuf_heap
)
constructor (local declaration of smplbuf_local
, global declaration is not allowed)
Add a member c
at the end. The return value of append()
is bool
, which returns false
if the buffer is full and cannot be added.
The pop_back()
deletes the trailing entry. However, it does not clear the entry.
empty()
returns true
when no elements are stored in the array. is_end()
returns true
when the array is full.
size()
returns the number of array elements.
capacity()
returns the maximum number of elements in the array.
reserve()
extends the size of the array. The area where the array is not stored is initialized by default.
reserve_hear()
allocates an area of the specified size at the top of the array. This area cannot be referenced by the container object. For example, it can be used as a container to access a sub-array of a packet payload whose header part is skipped. To return the container to the allocated area so that all of it can be accessed again, give it the same negative value as when it was allocated.
redim()
resizes the allocated area. Unlike reserve()
, it does not initialize the unused area.
element.
A negative value for i
makes the element from the end of the buffer. For -1
, the element is the last element, and for -2
, it is one before the end.
Array objects of type uint8_t
(smplbuf<uint8_t, *>
) can be output as is to derived objects of mwx::stream
.
Outputs a sequence of bytes for mwx::stream
derived objects such as Serial
.
Used for output to stream purposes. Used to implement the << operator.
mwx::streamdefines functions and operators to output bytes to a stream, such as
<<operator and
printfmt()method. You can use the stream output procedure with a smplbuf array of type
uint8_t` as the output destination.
There are two methods.
Using helper object generated by .get_stream_helper()
.
Use the smplbuf class that inherits mwx::stream
.