smplbuf
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.
Declaration, Initialize
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).
List of initializers
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)
Methods
append(), push_back(), pop_back()
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(), size(), capacity()
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(), reserve_head(), redim()
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.
operator []
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.
Output to mwx::stream
Array objects of type uint8_t
(smplbuf<uint8_t, *>
) can be output as is to derived objects of mwx::stream
.
<< operator
Outputs a sequence of bytes for mwx::stream
derived objects such as Serial
.
to_stream()
Used for output to stream purposes. Used to implement the << operator.
Data generation with `mwx::stream
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
.
最終更新