|
| 1 | +## Span |
| 2 | + |
| 3 | +A Span is a nonowning view to a sequence of contiguous elements. |
| 4 | + |
| 5 | +It can replace the traditional pair of pointer and size arguments passed as |
| 6 | +array definitions in function calls. |
| 7 | + |
| 8 | +### Construction |
| 9 | + |
| 10 | +Span objects can be constructed from a reference to a C++ array, a pointer to the |
| 11 | +sequence viewed and its size or the range of the sequence viewed: |
| 12 | + |
| 13 | +``` |
| 14 | +const uint8_t str[] = "Hello mbed!"; |
| 15 | +
|
| 16 | +Span<const uint8_t> span_from_array(str); |
| 17 | +Span<const uint8_t> span_from_ptr_and_size(str, sizeof(str)); |
| 18 | +Span<const uint8_t> span_from_range(str, str + sizeof(str)); |
| 19 | +``` |
| 20 | + |
| 21 | +### Operations |
| 22 | + |
| 23 | +Span objects can be copied and assigned like regular value types with the help |
| 24 | +of the copy constructor or the copy assignment (=) operator. |
| 25 | + |
| 26 | +``` |
| 27 | +const uint8_t str[] = "Hello mbed!"; |
| 28 | +
|
| 29 | +Span<uint8_t> str_span(hello_mbed); |
| 30 | +Span<uint8_t> copy_constructed_span(str_span); |
| 31 | +Span<uint8_t> copy_assigned_span; |
| 32 | +
|
| 33 | +copy_assigned_span = str_span; |
| 34 | +``` |
| 35 | + |
| 36 | +You can retrieve elements of the object with the subscript ([]) operator. You |
| 37 | +can access the pointer to the first element of the sequence viewed with `data()`. |
| 38 | +The function `size()` returns the number of elements in the sequence, and |
| 39 | +`empty()` informs whether there is any element in the sequence. |
| 40 | + |
| 41 | +``` |
| 42 | +void process_unit(uint8_t); |
| 43 | +
|
| 44 | +void process(const Span<uint8_t> &data) |
| 45 | +{ |
| 46 | + if (data.empty()) { |
| 47 | + // nothing to process |
| 48 | + return; |
| 49 | + } |
| 50 | +
|
| 51 | + for (ptrdiff_t i = 0; i < data.size(); ++i) { |
| 52 | + process_unit(data[i]); |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +You can slice Span from the beginning of the sequence (`first()`), from the end |
| 58 | +of the sequence (`last()`) or from an arbitrary point in the sequence (`subspan()`). |
| 59 | + |
| 60 | +``` |
| 61 | +const uint8_t str[] = "Hello mbed!"; |
| 62 | +
|
| 63 | +Span<uint8_t> str_span(hello_mbed); |
| 64 | +
|
| 65 | +ptrdiff_t half_size = str_span.size() / 2; |
| 66 | +
|
| 67 | +Span<uint8_t> first_half = str_span.first(half_size); |
| 68 | +Span<uint8_t> second_half = str_span.last(half_size); |
| 69 | +Span<uint8_t> middle_half = str_span.subspan(/* offset */ half_size / 2, half_size); |
| 70 | +``` |
| 71 | + |
| 72 | +### Size encoding |
| 73 | + |
| 74 | +The size of the sequence can be encoded in the type itself or in the value of |
| 75 | +the instance with the help of the template parameter Extent: |
| 76 | + - `Span<uint8_t, 6>`: Span over a sequence of 6 `uint8_t`. |
| 77 | + - `Span<uint8_t>`: Span over an arbitrary long sequence of `uint8_t`. |
| 78 | + |
| 79 | +When the size is encoded in the type itself, it is guaranteed that the Span |
| 80 | +view is a valid sequence (not empty() and not NULL) - unless Extent equals 0. |
| 81 | +The type system also prevents automatic conversion from Span of different |
| 82 | +sizes. Finally, the Span object is internally represented as a single pointer. |
| 83 | + |
| 84 | +``` |
| 85 | +Span<uint8_t> long_span; |
| 86 | +
|
| 87 | +// illegal |
| 88 | +Span<uint8_t, 6> span_mac_address; |
| 89 | +Span<uint8_t, 6> from_long_span(long_span); |
| 90 | +
|
| 91 | +// legal |
| 92 | +uint8_t mac_address[6] = { }; |
| 93 | +Span<uint8_t, 6> span_mac_address(mac_address); |
| 94 | +long_span = span_mac_address; |
| 95 | +``` |
| 96 | + |
| 97 | +When the size of the sequence viewed is encoded in the Span value, Span |
| 98 | +instances can view an empty sequence. The function empty() helps client code |
| 99 | +decide whether Span is viewing valid content or not. |
| 100 | + |
| 101 | +### Span class reference |
| 102 | + |
| 103 | +[](https://os-doc-builder.test.mbed.com/docs/development/mbed-os-api-doxy/class_span.html) |
| 104 | + |
0 commit comments