|
| 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 array definitions in function calls. |
| 6 | + |
| 7 | +### Construction |
| 8 | + |
| 9 | +Span objects can be constructed from a reference to a C++ array, a pointer to the sequence viewed and its size or the range of the sequence viewed: |
| 10 | + |
| 11 | +``` |
| 12 | +const uint8_t str[] = "Hello mbed!"; |
| 13 | +
|
| 14 | +Span<const uint8_t> span_from_array(str); |
| 15 | +Span<const uint8_t> span_from_ptr_and_size(str, sizeof(str)); |
| 16 | +Span<const uint8_t> span_from_range(str, str + sizeof(str)); |
| 17 | +``` |
| 18 | + |
| 19 | +### Operations |
| 20 | + |
| 21 | +You can copy and assign Span objects like regular value types with the help of the copy constructor or the copy assignment (=) operator. |
| 22 | + |
| 23 | +``` |
| 24 | +const uint8_t str[] = "Hello mbed!"; |
| 25 | +
|
| 26 | +Span<uint8_t> str_span(hello_mbed); |
| 27 | +Span<uint8_t> copy_constructed_span(str_span); |
| 28 | +Span<uint8_t> copy_assigned_span; |
| 29 | +
|
| 30 | +copy_assigned_span = str_span; |
| 31 | +``` |
| 32 | + |
| 33 | +You can retrieve elements of the object with the subscript ([]) operator. You can access the pointer to the first element of the sequence viewed with `data()`. The function `size()` returns the number of elements in the sequence, and `empty()` informs whether there is any element in the sequence. |
| 34 | + |
| 35 | +``` |
| 36 | +void process_unit(uint8_t); |
| 37 | +
|
| 38 | +void process(const Span<uint8_t> &data) |
| 39 | +{ |
| 40 | + if (data.empty()) { |
| 41 | + // nothing to process |
| 42 | + return; |
| 43 | + } |
| 44 | +
|
| 45 | + for (ptrdiff_t i = 0; i < data.size(); ++i) { |
| 46 | + process_unit(data[i]); |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +You can slice Span from the beginning of the sequence (`first()`), from the end of the sequence (`last()`) or from an arbitrary point in the sequence (`subspan()`). |
| 52 | + |
| 53 | +``` |
| 54 | +const uint8_t str[] = "Hello mbed!"; |
| 55 | +
|
| 56 | +Span<uint8_t> str_span(hello_mbed); |
| 57 | +
|
| 58 | +ptrdiff_t half_size = str_span.size() / 2; |
| 59 | +
|
| 60 | +Span<uint8_t> lower_half = str_span.first(half_size); |
| 61 | +Span<uint8_t> upper_half = str_span.last(half_size); |
| 62 | +Span<uint8_t> interquartile_range = str_span.subspan(/* offset */ half_size / 2, half_size); |
| 63 | +``` |
| 64 | + |
| 65 | +### Size encoding |
| 66 | + |
| 67 | +You can encode the size of the sequence in the type itself or in the value of the instance with the help of the template parameter Extent: |
| 68 | + |
| 69 | + - `Span<uint8_t, 6>`: Span over a sequence of 6 `uint8_t`. |
| 70 | + - `Span<uint8_t>`: Span over an arbitrary long sequence of `uint8_t`. |
| 71 | + |
| 72 | +When you encode the size in the type itself, the Span view is guaranteed to be a valid sequence (not `empty()` and not NULL) - unless `Extent` equals 0. The type system also prevents automatic conversion from Span of different sizes. Finally, a single pointer internally represents the Span object. |
| 73 | + |
| 74 | +``` |
| 75 | +Span<uint8_t> long_span; |
| 76 | +
|
| 77 | +// illegal |
| 78 | +Span<uint8_t, 6> span_mac_address; |
| 79 | +Span<uint8_t, 6> from_long_span(long_span); |
| 80 | +
|
| 81 | +// legal |
| 82 | +uint8_t mac_address[6] = { }; |
| 83 | +Span<uint8_t, 6> span_mac_address(mac_address); |
| 84 | +long_span = span_mac_address; |
| 85 | +``` |
| 86 | + |
| 87 | +When you encode the size of the sequence viewed in the Span value, Span instances can view an empty sequence. The function `empty()` helps client code decide whether Span is viewing valid content or not. |
| 88 | + |
| 89 | +### Span class reference |
| 90 | + |
| 91 | +[](https://os-doc-builder.test.mbed.com/docs/development/mbed-os-api-doxy/class_span.html) |
| 92 | + |
| 93 | + |
| 94 | +### Span example |
| 95 | + |
| 96 | +``` |
| 97 | +template<typename T> |
| 98 | +Span<const T> split(Span<const T> &range, const T& separator) { |
| 99 | + const ptrdiff_t out_of_range = range.size(); |
| 100 | +
|
| 101 | + ptrdiff_t start; |
| 102 | + for (start = 0; start != out_of_range && range[start] == separator; ++start) { } |
| 103 | +
|
| 104 | + ptrdiff_t last; |
| 105 | + for (last = start; last != out_of_range && range[last] != separator; ++last) { } |
| 106 | +
|
| 107 | + Span<const T> result = range.subspan(start, last - start); |
| 108 | + range = range.subspan(last); |
| 109 | + return result; |
| 110 | +} |
| 111 | +
|
| 112 | +Span<const char> buffer("Hello World! Hello mbed-os!"); |
| 113 | +while(buffer.empty() == false) { |
| 114 | + Span<const char> token = split(buffer, ' '); |
| 115 | + printf("token: %.*s\r\n", token.size(), token.data()); |
| 116 | +} |
| 117 | +
|
| 118 | +//------------------------------------------------------------------------------ |
| 119 | +// Equivalent C-like code |
| 120 | +
|
| 121 | +template<typename T> |
| 122 | +void split(const T** in_ptr, ptrdiff_t* in_size, const T** token_ptr, ptrdiff_t* token_size, const T& separator) { |
| 123 | + const ptrdiff_t out_of_range = *in_size; |
| 124 | +
|
| 125 | + ptrdiff_t start; |
| 126 | + for (start = 0; start != out_of_range && (*in_ptr)[start] == separator; ++start) { } |
| 127 | +
|
| 128 | + ptrdiff_t last; |
| 129 | + for (last = start; last != out_of_range && (*in_ptr)[last] != separator; ++last) { } |
| 130 | +
|
| 131 | + *token_ptr = *in_ptr + start; |
| 132 | + *token_size = last - start; |
| 133 | +
|
| 134 | + *in_size = *in_size - last; |
| 135 | + *in_ptr = *in_ptr + last; |
| 136 | +} |
| 137 | +
|
| 138 | +const char* buffer_ptr = str; |
| 139 | +ptrdiff_t buffer_size = sizeof(str); |
| 140 | +while (buffer_size) { |
| 141 | + const char* token_ptr = NULL; |
| 142 | + ptrdiff_t token_size = 0; |
| 143 | + split(&buffer_ptr, &buffer_size, &token_ptr, &token_size, ' '); |
| 144 | + printf("token: %.*s\r\n", token_size, token_ptr); |
| 145 | +} |
| 146 | +``` |
0 commit comments