blob: 63d44ba76fcfe17eb2f2d0abcce1aa591981473e [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_span:
Wyatt Hepleree3e02f2019-12-05 10:52:31 -08002
3-------
4pw_span
5-------
Armando Montanez0054a9b2020-03-13 13:06:24 -07006The ``pw_span`` module provides an implementation of C++20's
Wyatt Hepleree3e02f2019-12-05 10:52:31 -08007`std::span <https://en.cppreference.com/w/cpp/container/span>`_, which is a
Wyatt Hepler69a51902020-06-22 10:42:53 -07008non-owning view of an array of values. The intent is for this implementation of
9``std::span`` is to exactly match the C++20 standard.
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080010
Wyatt Hepler9f392342020-07-08 10:11:33 -070011The only header provided by the ``pw_span`` module is ``<span>``. It is included
12as if it were coming from the C++ Standard Library. If the C++ library provides
13``<span>``, the library's version of ``std::span`` is used in place of
Wyatt Hepler69a51902020-06-22 10:42:53 -070014``pw_span``'s.
15
16``pw_span`` requires two include paths -- ``public/`` and ``public_overrides/``.
17The internal implementation header is in ``public/``, and the ``<span>`` header
18that mimics the C++ Standard Library is in ``public_overrides/``.
19
Wyatt Hepler69a51902020-06-22 10:42:53 -070020Using std::span
21===============
22``std::span`` is a convenient abstraction that wraps a pointer and a size.
23``std::span`` is especially useful in APIs. Spans support implicit conversions
Alexei Frolov44d54732020-01-10 14:45:43 -080024from C arrays, ``std::array``, or any STL-style container, such as
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080025``std::string_view``.
26
27Functions operating on an array of bytes typically accept pointer and size
28arguments:
29
30.. code-block:: cpp
31
32 bool ProcessBuffer(char* buffer, size_t buffer_size);
33
34 bool DoStuff() {
35 ProcessBuffer(c_array, sizeof(c_array));
36 ProcessBuffer(array_object.data(), array_object.size());
37 ProcessBuffer(data_pointer, data_size);
38 }
39
Wyatt Hepler69a51902020-06-22 10:42:53 -070040Pointer and size arguments can be replaced with a ``std::span``:
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080041
42.. code-block:: cpp
43
Wyatt Hepler69a51902020-06-22 10:42:53 -070044 #include <span>
45
46 // With std::span, the buffer is passed as a single argument.
47 bool ProcessBuffer(std::span<uint8_t> buffer);
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080048
49 bool DoStuff() {
50 ProcessBuffer(c_array);
51 ProcessBuffer(array_object);
Wyatt Hepler69a51902020-06-22 10:42:53 -070052 ProcessBuffer(std::span(data_pointer, data_size));
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080053 }
54
55.. tip::
Wyatt Hepler69a51902020-06-22 10:42:53 -070056 Use ``std::span<std::byte>`` or ``std::span<const std::byte>`` to represent
57 spans of binary data. Use ``std::as_bytes`` or ``std::as_writeable_bytes``
Wyatt Hepler867d42d2019-12-09 18:54:31 -080058 to convert any span to a byte span.
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080059
60 .. code-block:: cpp
61
Wyatt Hepler69a51902020-06-22 10:42:53 -070062 void ProcessData(std::span<const std::byte> data);
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080063
64 void DoStuff() {
65 std::array<AnyType, 7> data = { ... };
Wyatt Hepler69a51902020-06-22 10:42:53 -070066 ProcessData(std::as_bytes(std::span(data)));
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080067 }
68
69Compatibility
70=============
Wyatt Hepler0132da52021-10-11 16:20:11 -070071Works with C++14, but some features require C++17.
Yuval Peressb8f3ad22021-10-26 22:55:27 -060072
73Zephyr
74======
75To enable ``pw_span`` for Zephyr add ``CONFIG_PIGWEED_SPAN=y`` to the project's
76configuration.