RTC auf true
platform/Span.h@2:7aab896b1a3b, 2019-03-13 (annotated)
- Committer:
- kevman
- Date:
- Wed Mar 13 11:03:24 2019 +0000
- Revision:
- 2:7aab896b1a3b
- Parent:
- 0:38ceb79fef03
2019-03-13
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | /* mbed Microcontroller Library |
kevman | 0:38ceb79fef03 | 2 | * Copyright (c) 2018-2018 ARM Limited |
kevman | 0:38ceb79fef03 | 3 | * |
kevman | 0:38ceb79fef03 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kevman | 0:38ceb79fef03 | 5 | * you may not use this file except in compliance with the License. |
kevman | 0:38ceb79fef03 | 6 | * You may obtain a copy of the License at |
kevman | 0:38ceb79fef03 | 7 | * |
kevman | 0:38ceb79fef03 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kevman | 0:38ceb79fef03 | 9 | * |
kevman | 0:38ceb79fef03 | 10 | * Unless required by applicable law or agreed to in writing, software |
kevman | 0:38ceb79fef03 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kevman | 0:38ceb79fef03 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kevman | 0:38ceb79fef03 | 13 | * See the License for the specific language governing permissions and |
kevman | 0:38ceb79fef03 | 14 | * limitations under the License. |
kevman | 0:38ceb79fef03 | 15 | */ |
kevman | 0:38ceb79fef03 | 16 | |
kevman | 0:38ceb79fef03 | 17 | #ifndef MBED_PLATFORM_SPAN_H_ |
kevman | 0:38ceb79fef03 | 18 | #define MBED_PLATFORM_SPAN_H_ |
kevman | 0:38ceb79fef03 | 19 | |
kevman | 0:38ceb79fef03 | 20 | #include <algorithm> |
kevman | 0:38ceb79fef03 | 21 | #include <stddef.h> |
kevman | 0:38ceb79fef03 | 22 | #include <stdint.h> |
kevman | 0:38ceb79fef03 | 23 | |
kevman | 0:38ceb79fef03 | 24 | #include "platform/mbed_assert.h" |
kevman | 0:38ceb79fef03 | 25 | |
kevman | 0:38ceb79fef03 | 26 | namespace mbed { |
kevman | 0:38ceb79fef03 | 27 | |
kevman | 0:38ceb79fef03 | 28 | /** \addtogroup platform */ |
kevman | 0:38ceb79fef03 | 29 | /** @{*/ |
kevman | 0:38ceb79fef03 | 30 | /** |
kevman | 0:38ceb79fef03 | 31 | * \defgroup platform_Span Span class |
kevman | 0:38ceb79fef03 | 32 | * @{ |
kevman | 0:38ceb79fef03 | 33 | */ |
kevman | 0:38ceb79fef03 | 34 | |
kevman | 0:38ceb79fef03 | 35 | // Internal details of Span |
kevman | 0:38ceb79fef03 | 36 | // It is used construct Span from Span of convertible types (non const -> const) |
kevman | 0:38ceb79fef03 | 37 | namespace span_detail { |
kevman | 0:38ceb79fef03 | 38 | |
kevman | 0:38ceb79fef03 | 39 | // If From type is convertible to To type, then the compilation constant value is |
kevman | 0:38ceb79fef03 | 40 | // true; otherwise, it is false. |
kevman | 0:38ceb79fef03 | 41 | template<typename From, typename To> |
kevman | 0:38ceb79fef03 | 42 | class is_convertible |
kevman | 0:38ceb79fef03 | 43 | { |
kevman | 0:38ceb79fef03 | 44 | struct true_type { char x[512]; }; |
kevman | 0:38ceb79fef03 | 45 | struct false_type { }; |
kevman | 0:38ceb79fef03 | 46 | |
kevman | 0:38ceb79fef03 | 47 | static const From& generator(); |
kevman | 0:38ceb79fef03 | 48 | static true_type sink(const To &); |
kevman | 0:38ceb79fef03 | 49 | static false_type sink(...); |
kevman | 0:38ceb79fef03 | 50 | |
kevman | 0:38ceb79fef03 | 51 | public: |
kevman | 0:38ceb79fef03 | 52 | static const bool value = sizeof(true_type) == sizeof(sink(generator())); |
kevman | 0:38ceb79fef03 | 53 | }; |
kevman | 0:38ceb79fef03 | 54 | |
kevman | 0:38ceb79fef03 | 55 | } |
kevman | 0:38ceb79fef03 | 56 | |
kevman | 0:38ceb79fef03 | 57 | #if defined(DOXYGEN_ONLY) |
kevman | 0:38ceb79fef03 | 58 | /** |
kevman | 0:38ceb79fef03 | 59 | * Special value for the Extent parameter of Span. |
kevman | 0:38ceb79fef03 | 60 | * If the type uses this value, then the size of the array is stored in the object |
kevman | 0:38ceb79fef03 | 61 | * at runtime. |
kevman | 0:38ceb79fef03 | 62 | * |
kevman | 0:38ceb79fef03 | 63 | * @relates Span |
kevman | 0:38ceb79fef03 | 64 | */ |
kevman | 0:38ceb79fef03 | 65 | const ptrdiff_t SPAN_DYNAMIC_EXTENT = -1; |
kevman | 0:38ceb79fef03 | 66 | #else |
kevman | 0:38ceb79fef03 | 67 | #define SPAN_DYNAMIC_EXTENT -1 |
kevman | 0:38ceb79fef03 | 68 | #endif |
kevman | 0:38ceb79fef03 | 69 | |
kevman | 0:38ceb79fef03 | 70 | /** |
kevman | 0:38ceb79fef03 | 71 | * Nonowning view to a sequence of contiguous elements. |
kevman | 0:38ceb79fef03 | 72 | * |
kevman | 0:38ceb79fef03 | 73 | * Spans encapsulate a pointer to a sequence of contiguous elements and its size |
kevman | 0:38ceb79fef03 | 74 | * into a single object. Span can replace the traditional pair of pointer and |
kevman | 0:38ceb79fef03 | 75 | * size arguments passed as array definitions in function calls. |
kevman | 0:38ceb79fef03 | 76 | * |
kevman | 0:38ceb79fef03 | 77 | * @par Operations |
kevman | 0:38ceb79fef03 | 78 | * |
kevman | 0:38ceb79fef03 | 79 | * Span objects can be copied and assigned like regular value types with the help |
kevman | 0:38ceb79fef03 | 80 | * of the copy constructor or the copy assignment (=) operator. |
kevman | 0:38ceb79fef03 | 81 | * |
kevman | 0:38ceb79fef03 | 82 | * You can retrieve elements of the object with the subscript ([]) operator. You can access the |
kevman | 0:38ceb79fef03 | 83 | * pointer to the first element of the sequence viewed with data(). |
kevman | 0:38ceb79fef03 | 84 | * The function size() returns the number of elements in the sequence, and |
kevman | 0:38ceb79fef03 | 85 | * empty() informs whether there is any element in the sequence. |
kevman | 0:38ceb79fef03 | 86 | * |
kevman | 0:38ceb79fef03 | 87 | * You can slice Span from the beginning of the sequence (first()), from the end |
kevman | 0:38ceb79fef03 | 88 | * of the sequence (last()) or from an arbitrary point of the sequence (subspan()). |
kevman | 0:38ceb79fef03 | 89 | * |
kevman | 0:38ceb79fef03 | 90 | * @par Size encoding |
kevman | 0:38ceb79fef03 | 91 | * |
kevman | 0:38ceb79fef03 | 92 | * The size of the sequence can be encoded in the type itself or in the value of |
kevman | 0:38ceb79fef03 | 93 | * the instance with the help of the template parameter Extent: |
kevman | 0:38ceb79fef03 | 94 | * |
kevman | 0:38ceb79fef03 | 95 | * - Span<uint8_t, 6>: Span over a sequence of 6 elements. |
kevman | 0:38ceb79fef03 | 96 | * - Span<uint8_t>: Span over an arbitrary long sequence. |
kevman | 0:38ceb79fef03 | 97 | * |
kevman | 0:38ceb79fef03 | 98 | * When the size is encoded in the type itself, it is guaranteed that the Span |
kevman | 0:38ceb79fef03 | 99 | * view is a valid sequence (not empty() and not NULL) - unless Extent equals 0. |
kevman | 0:38ceb79fef03 | 100 | * The type system also prevents automatic conversion from Span of different |
kevman | 0:38ceb79fef03 | 101 | * sizes. Finally, the Span object is internally represented as a single pointer. |
kevman | 0:38ceb79fef03 | 102 | * |
kevman | 0:38ceb79fef03 | 103 | * When the size of the sequence viewed is encoded in the Span value, Span |
kevman | 0:38ceb79fef03 | 104 | * instances can view an empty sequence. The function empty() helps client code |
kevman | 0:38ceb79fef03 | 105 | * decide whether Span is viewing valid content or not. |
kevman | 0:38ceb79fef03 | 106 | * |
kevman | 0:38ceb79fef03 | 107 | * @par Example |
kevman | 0:38ceb79fef03 | 108 | * |
kevman | 0:38ceb79fef03 | 109 | * - Encoding fixed size array: Array values in parameter decays automatically |
kevman | 0:38ceb79fef03 | 110 | * to pointer, which leaves room for subtitle bugs: |
kevman | 0:38ceb79fef03 | 111 | * |
kevman | 0:38ceb79fef03 | 112 | * @code |
kevman | 0:38ceb79fef03 | 113 | typedef uint8_t mac_address_t[6]; |
kevman | 0:38ceb79fef03 | 114 | void process_mac(mac_address_t); |
kevman | 0:38ceb79fef03 | 115 | |
kevman | 0:38ceb79fef03 | 116 | // compile just fine |
kevman | 0:38ceb79fef03 | 117 | uint8_t *invalid_value = NULL; |
kevman | 0:38ceb79fef03 | 118 | process_mac(invalid_value); |
kevman | 0:38ceb79fef03 | 119 | |
kevman | 0:38ceb79fef03 | 120 | |
kevman | 0:38ceb79fef03 | 121 | // correct way |
kevman | 0:38ceb79fef03 | 122 | typedef Span<uint8_t, 6> mac_address_t; |
kevman | 0:38ceb79fef03 | 123 | void process_mac(mac_address_t); |
kevman | 0:38ceb79fef03 | 124 | |
kevman | 0:38ceb79fef03 | 125 | // compilation error |
kevman | 0:38ceb79fef03 | 126 | uint8_t *invalid_value = NULL; |
kevman | 0:38ceb79fef03 | 127 | process_mac(invalid_value); |
kevman | 0:38ceb79fef03 | 128 | |
kevman | 0:38ceb79fef03 | 129 | // compilation ok |
kevman | 0:38ceb79fef03 | 130 | uint8_t valid_value[6]; |
kevman | 0:38ceb79fef03 | 131 | process_mac(valid_value); |
kevman | 0:38ceb79fef03 | 132 | * @endcode |
kevman | 0:38ceb79fef03 | 133 | * |
kevman | 0:38ceb79fef03 | 134 | * - Arbitrary buffer: When dealing with multiple buffers, it becomes painful to |
kevman | 0:38ceb79fef03 | 135 | * keep track of every buffer size and pointer. |
kevman | 0:38ceb79fef03 | 136 | * |
kevman | 0:38ceb79fef03 | 137 | * @code |
kevman | 0:38ceb79fef03 | 138 | const uint8_t options_tag[OPTIONS_TAG_SIZE]; |
kevman | 0:38ceb79fef03 | 139 | |
kevman | 0:38ceb79fef03 | 140 | struct parsed_value_t { |
kevman | 0:38ceb79fef03 | 141 | uint8_t *header; |
kevman | 0:38ceb79fef03 | 142 | uint8_t *options; |
kevman | 0:38ceb79fef03 | 143 | uint8_t *payload; |
kevman | 0:38ceb79fef03 | 144 | size_t payload_size; |
kevman | 0:38ceb79fef03 | 145 | } |
kevman | 0:38ceb79fef03 | 146 | |
kevman | 0:38ceb79fef03 | 147 | parsed_value_t parse(uint8_t *buffer, size_t buffer_size) |
kevman | 0:38ceb79fef03 | 148 | { |
kevman | 0:38ceb79fef03 | 149 | parsed_value_t parsed_value { 0 }; |
kevman | 0:38ceb79fef03 | 150 | |
kevman | 0:38ceb79fef03 | 151 | if (buffer != NULL && buffer_size <= MINIMAL_BUFFER_SIZE) { |
kevman | 0:38ceb79fef03 | 152 | return parsed_value; |
kevman | 0:38ceb79fef03 | 153 | } |
kevman | 0:38ceb79fef03 | 154 | |
kevman | 0:38ceb79fef03 | 155 | parsed_value.header = buffer; |
kevman | 0:38ceb79fef03 | 156 | parsed_value.header_size = BUFFER_HEADER_SIZE; |
kevman | 0:38ceb79fef03 | 157 | |
kevman | 0:38ceb79fef03 | 158 | if (memcmp(buffer + HEADER_OPTIONS_INDEX, options_tag, sizeof(options_tag)) == 0) { |
kevman | 0:38ceb79fef03 | 159 | options = buffer + BUFFER_HEADER_SIZE; |
kevman | 0:38ceb79fef03 | 160 | payload = buffer + BUFFER_HEADER_SIZE + OPTIONS_SIZE; |
kevman | 0:38ceb79fef03 | 161 | payload_size = buffer_size - BUFFER_HEADER_SIZE + OPTIONS_SIZE; |
kevman | 0:38ceb79fef03 | 162 | } else { |
kevman | 0:38ceb79fef03 | 163 | payload = buffer + BUFFER_HEADER_SIZE; |
kevman | 0:38ceb79fef03 | 164 | payload_size = buffer_size - BUFFER_HEADER_SIZE; |
kevman | 0:38ceb79fef03 | 165 | } |
kevman | 0:38ceb79fef03 | 166 | |
kevman | 0:38ceb79fef03 | 167 | return parsed_value; |
kevman | 0:38ceb79fef03 | 168 | } |
kevman | 0:38ceb79fef03 | 169 | |
kevman | 0:38ceb79fef03 | 170 | |
kevman | 0:38ceb79fef03 | 171 | //with Span |
kevman | 0:38ceb79fef03 | 172 | struct parsed_value_t { |
kevman | 0:38ceb79fef03 | 173 | Span<uint8_t> header; |
kevman | 0:38ceb79fef03 | 174 | Span<uint8_t> options; |
kevman | 0:38ceb79fef03 | 175 | Span<uint8_t> payload; |
kevman | 0:38ceb79fef03 | 176 | } |
kevman | 0:38ceb79fef03 | 177 | |
kevman | 0:38ceb79fef03 | 178 | parsed_value_t parse(const Span<uint8_t> &buffer) |
kevman | 0:38ceb79fef03 | 179 | { |
kevman | 0:38ceb79fef03 | 180 | parsed_value_t parsed_value; |
kevman | 0:38ceb79fef03 | 181 | |
kevman | 0:38ceb79fef03 | 182 | if (buffer.size() <= MINIMAL_BUFFER_SIZE) { |
kevman | 0:38ceb79fef03 | 183 | return parsed_value; |
kevman | 0:38ceb79fef03 | 184 | } |
kevman | 0:38ceb79fef03 | 185 | |
kevman | 0:38ceb79fef03 | 186 | parsed_value.header = buffer.first(BUFFER_HEADER_SIZE); |
kevman | 0:38ceb79fef03 | 187 | |
kevman | 0:38ceb79fef03 | 188 | if (buffer.subspan<HEADER_OPTIONS_INDEX, sizeof(options_tag)>() == option_tag) { |
kevman | 0:38ceb79fef03 | 189 | options = buffer.supspan(parsed_value.header.size(), OPTIONS_SIZE); |
kevman | 0:38ceb79fef03 | 190 | } |
kevman | 0:38ceb79fef03 | 191 | |
kevman | 0:38ceb79fef03 | 192 | payload = buffer.subspan(parsed_value.header.size() + parsed_value.options.size()); |
kevman | 0:38ceb79fef03 | 193 | |
kevman | 0:38ceb79fef03 | 194 | return parsed_value; |
kevman | 0:38ceb79fef03 | 195 | } |
kevman | 0:38ceb79fef03 | 196 | * @endcode |
kevman | 0:38ceb79fef03 | 197 | * |
kevman | 0:38ceb79fef03 | 198 | * @note You can create Span instances with the help of the function template |
kevman | 0:38ceb79fef03 | 199 | * make_Span() and make_const_Span(). |
kevman | 0:38ceb79fef03 | 200 | * |
kevman | 0:38ceb79fef03 | 201 | * @note Span<T, Extent> objects can be implicitly converted to Span<T> objects |
kevman | 0:38ceb79fef03 | 202 | * where required. |
kevman | 0:38ceb79fef03 | 203 | * |
kevman | 0:38ceb79fef03 | 204 | * @tparam ElementType type of objects the Span views. |
kevman | 0:38ceb79fef03 | 205 | * |
kevman | 0:38ceb79fef03 | 206 | * @tparam Extent The size of the contiguous sequence viewed. The default value |
kevman | 0:38ceb79fef03 | 207 | * SPAN_DYNAMIC_SIZE is special because it allows construction of Span objects of |
kevman | 0:38ceb79fef03 | 208 | * any size (set at runtime). |
kevman | 0:38ceb79fef03 | 209 | */ |
kevman | 0:38ceb79fef03 | 210 | template<typename ElementType, ptrdiff_t Extent = SPAN_DYNAMIC_EXTENT> |
kevman | 0:38ceb79fef03 | 211 | struct Span { |
kevman | 0:38ceb79fef03 | 212 | |
kevman | 0:38ceb79fef03 | 213 | /** |
kevman | 0:38ceb79fef03 | 214 | * Type of the element contained |
kevman | 0:38ceb79fef03 | 215 | */ |
kevman | 0:38ceb79fef03 | 216 | typedef ElementType element_type; |
kevman | 0:38ceb79fef03 | 217 | |
kevman | 0:38ceb79fef03 | 218 | /** |
kevman | 0:38ceb79fef03 | 219 | * Type of the index. |
kevman | 0:38ceb79fef03 | 220 | */ |
kevman | 0:38ceb79fef03 | 221 | typedef ptrdiff_t index_type; |
kevman | 0:38ceb79fef03 | 222 | |
kevman | 0:38ceb79fef03 | 223 | /** |
kevman | 0:38ceb79fef03 | 224 | * Pointer to an ElementType |
kevman | 0:38ceb79fef03 | 225 | */ |
kevman | 0:38ceb79fef03 | 226 | typedef element_type *pointer; |
kevman | 0:38ceb79fef03 | 227 | |
kevman | 0:38ceb79fef03 | 228 | /** |
kevman | 0:38ceb79fef03 | 229 | * Reference to an ElementType |
kevman | 0:38ceb79fef03 | 230 | */ |
kevman | 0:38ceb79fef03 | 231 | typedef element_type &reference; |
kevman | 0:38ceb79fef03 | 232 | |
kevman | 0:38ceb79fef03 | 233 | /** |
kevman | 0:38ceb79fef03 | 234 | * Size of the Extent; -1 if dynamic. |
kevman | 0:38ceb79fef03 | 235 | */ |
kevman | 0:38ceb79fef03 | 236 | static const index_type extent = Extent; |
kevman | 0:38ceb79fef03 | 237 | |
kevman | 0:38ceb79fef03 | 238 | MBED_STATIC_ASSERT(Extent >= 0, "Invalid extent for a Span"); |
kevman | 0:38ceb79fef03 | 239 | |
kevman | 0:38ceb79fef03 | 240 | /** |
kevman | 0:38ceb79fef03 | 241 | * Construct an empty Span. |
kevman | 0:38ceb79fef03 | 242 | * |
kevman | 0:38ceb79fef03 | 243 | * @post a call to size() returns 0, and data() returns NULL. |
kevman | 0:38ceb79fef03 | 244 | * |
kevman | 0:38ceb79fef03 | 245 | * @note This function is not accessible if Extent != SPAN_DYNAMIC_EXTENT or |
kevman | 0:38ceb79fef03 | 246 | * Extent != 0 . |
kevman | 0:38ceb79fef03 | 247 | */ |
kevman | 0:38ceb79fef03 | 248 | Span() : |
kevman | 0:38ceb79fef03 | 249 | _data(NULL) |
kevman | 0:38ceb79fef03 | 250 | { |
kevman | 0:38ceb79fef03 | 251 | MBED_STATIC_ASSERT( |
kevman | 0:38ceb79fef03 | 252 | Extent == 0, |
kevman | 0:38ceb79fef03 | 253 | "Cannot default construct a static-extent Span (unless Extent is 0)" |
kevman | 0:38ceb79fef03 | 254 | ); |
kevman | 0:38ceb79fef03 | 255 | } |
kevman | 0:38ceb79fef03 | 256 | |
kevman | 0:38ceb79fef03 | 257 | /** |
kevman | 0:38ceb79fef03 | 258 | * Construct a Span from a pointer to a buffer and its size. |
kevman | 0:38ceb79fef03 | 259 | * |
kevman | 0:38ceb79fef03 | 260 | * @param ptr Pointer to the beginning of the data viewed. |
kevman | 0:38ceb79fef03 | 261 | * |
kevman | 0:38ceb79fef03 | 262 | * @param count Number of elements viewed. |
kevman | 0:38ceb79fef03 | 263 | * |
kevman | 0:38ceb79fef03 | 264 | * @pre [ptr, ptr + count) must be be a valid range. |
kevman | 0:38ceb79fef03 | 265 | * @pre count must be equal to Extent. |
kevman | 0:38ceb79fef03 | 266 | * |
kevman | 0:38ceb79fef03 | 267 | * @post a call to size() returns Extent, and data() returns @p ptr. |
kevman | 0:38ceb79fef03 | 268 | */ |
kevman | 0:38ceb79fef03 | 269 | Span(pointer ptr, index_type count) : |
kevman | 0:38ceb79fef03 | 270 | _data(ptr) |
kevman | 0:38ceb79fef03 | 271 | { |
kevman | 0:38ceb79fef03 | 272 | MBED_ASSERT(count == Extent); |
kevman | 0:38ceb79fef03 | 273 | MBED_ASSERT(Extent == 0 || ptr != NULL); |
kevman | 0:38ceb79fef03 | 274 | } |
kevman | 0:38ceb79fef03 | 275 | |
kevman | 0:38ceb79fef03 | 276 | /** |
kevman | 0:38ceb79fef03 | 277 | * Construct a Span from the range [first, last). |
kevman | 0:38ceb79fef03 | 278 | * |
kevman | 0:38ceb79fef03 | 279 | * @param first Pointer to the beginning of the data viewed. |
kevman | 0:38ceb79fef03 | 280 | * @param last End of the range (element after the last element). |
kevman | 0:38ceb79fef03 | 281 | * |
kevman | 0:38ceb79fef03 | 282 | * @pre [first, last) must be be a valid range. |
kevman | 0:38ceb79fef03 | 283 | * @pre first <= last. |
kevman | 0:38ceb79fef03 | 284 | * @pre last - first must be equal to Extent. |
kevman | 0:38ceb79fef03 | 285 | * |
kevman | 0:38ceb79fef03 | 286 | * @post a call to size() returns Extent, and data() returns @p first. |
kevman | 0:38ceb79fef03 | 287 | */ |
kevman | 0:38ceb79fef03 | 288 | Span(pointer first, pointer last) : |
kevman | 0:38ceb79fef03 | 289 | _data(first) |
kevman | 0:38ceb79fef03 | 290 | { |
kevman | 0:38ceb79fef03 | 291 | MBED_ASSERT(first <= last); |
kevman | 0:38ceb79fef03 | 292 | MBED_ASSERT((last - first) == Extent); |
kevman | 0:38ceb79fef03 | 293 | MBED_ASSERT(Extent == 0 || first != NULL); |
kevman | 0:38ceb79fef03 | 294 | } |
kevman | 0:38ceb79fef03 | 295 | |
kevman | 0:38ceb79fef03 | 296 | /** |
kevman | 0:38ceb79fef03 | 297 | * Construct a Span from the reference to an array. |
kevman | 0:38ceb79fef03 | 298 | * |
kevman | 0:38ceb79fef03 | 299 | * @param elements Reference to the array viewed. |
kevman | 0:38ceb79fef03 | 300 | * |
kevman | 0:38ceb79fef03 | 301 | * @post a call to size() returns Extent, and data() returns a |
kevman | 0:38ceb79fef03 | 302 | * pointer to elements. |
kevman | 0:38ceb79fef03 | 303 | */ |
kevman | 0:38ceb79fef03 | 304 | Span(element_type (&elements)[Extent]): |
kevman | 0:38ceb79fef03 | 305 | _data(elements) { } |
kevman | 0:38ceb79fef03 | 306 | |
kevman | 0:38ceb79fef03 | 307 | /** |
kevman | 0:38ceb79fef03 | 308 | * Construct a Span object from another Span of the same size. |
kevman | 0:38ceb79fef03 | 309 | * |
kevman | 0:38ceb79fef03 | 310 | * @param other The Span object used to construct this. |
kevman | 0:38ceb79fef03 | 311 | * |
kevman | 0:38ceb79fef03 | 312 | * @note For Span with a positive extent, this function is not accessible. |
kevman | 0:38ceb79fef03 | 313 | * |
kevman | 0:38ceb79fef03 | 314 | * @note OtherElementType(*)[] must be convertible to ElementType(*)[]. |
kevman | 0:38ceb79fef03 | 315 | */ |
kevman | 0:38ceb79fef03 | 316 | template<typename OtherElementType> |
kevman | 0:38ceb79fef03 | 317 | Span(const Span<OtherElementType, Extent> &other): |
kevman | 0:38ceb79fef03 | 318 | _data(other.data()) |
kevman | 0:38ceb79fef03 | 319 | { |
kevman | 0:38ceb79fef03 | 320 | MBED_STATIC_ASSERT( |
kevman | 0:38ceb79fef03 | 321 | (span_detail::is_convertible<OtherElementType (*)[1], ElementType (*)[1]>::value), |
kevman | 0:38ceb79fef03 | 322 | "OtherElementType(*)[] should be convertible to ElementType (*)[]" |
kevman | 0:38ceb79fef03 | 323 | ); |
kevman | 0:38ceb79fef03 | 324 | } |
kevman | 0:38ceb79fef03 | 325 | |
kevman | 0:38ceb79fef03 | 326 | /** |
kevman | 0:38ceb79fef03 | 327 | * Return the size of the sequence viewed. |
kevman | 0:38ceb79fef03 | 328 | * |
kevman | 0:38ceb79fef03 | 329 | * @return The size of the sequence viewed. |
kevman | 0:38ceb79fef03 | 330 | */ |
kevman | 0:38ceb79fef03 | 331 | index_type size() const |
kevman | 0:38ceb79fef03 | 332 | { |
kevman | 0:38ceb79fef03 | 333 | return Extent; |
kevman | 0:38ceb79fef03 | 334 | } |
kevman | 0:38ceb79fef03 | 335 | |
kevman | 0:38ceb79fef03 | 336 | /** |
kevman | 0:38ceb79fef03 | 337 | * Return if the sequence is empty or not. |
kevman | 0:38ceb79fef03 | 338 | * |
kevman | 0:38ceb79fef03 | 339 | * @return true if the sequence is empty and false otherwise. |
kevman | 0:38ceb79fef03 | 340 | */ |
kevman | 0:38ceb79fef03 | 341 | bool empty() const |
kevman | 0:38ceb79fef03 | 342 | { |
kevman | 0:38ceb79fef03 | 343 | return size() == 0; |
kevman | 0:38ceb79fef03 | 344 | } |
kevman | 0:38ceb79fef03 | 345 | |
kevman | 0:38ceb79fef03 | 346 | /** |
kevman | 0:38ceb79fef03 | 347 | * Returns a reference to the element at position @p index. |
kevman | 0:38ceb79fef03 | 348 | * |
kevman | 0:38ceb79fef03 | 349 | * @param index Index of the element to access. |
kevman | 0:38ceb79fef03 | 350 | * |
kevman | 0:38ceb79fef03 | 351 | * @return A reference to the element at the index specified in input. |
kevman | 0:38ceb79fef03 | 352 | * |
kevman | 0:38ceb79fef03 | 353 | * @pre 0 <= index < Extent. |
kevman | 0:38ceb79fef03 | 354 | */ |
kevman | 0:38ceb79fef03 | 355 | reference operator[](index_type index) const |
kevman | 0:38ceb79fef03 | 356 | { |
kevman | 0:38ceb79fef03 | 357 | #ifdef MBED_DEBUG |
kevman | 0:38ceb79fef03 | 358 | MBED_ASSERT(0 <= index && index < Extent); |
kevman | 0:38ceb79fef03 | 359 | #endif |
kevman | 0:38ceb79fef03 | 360 | return _data[index]; |
kevman | 0:38ceb79fef03 | 361 | } |
kevman | 0:38ceb79fef03 | 362 | |
kevman | 0:38ceb79fef03 | 363 | /** |
kevman | 0:38ceb79fef03 | 364 | * Return a pointer to the first element of the sequence or NULL if the Span |
kevman | 0:38ceb79fef03 | 365 | * is empty(). |
kevman | 0:38ceb79fef03 | 366 | * |
kevman | 0:38ceb79fef03 | 367 | * @return The pointer to the first element of the Span. |
kevman | 0:38ceb79fef03 | 368 | */ |
kevman | 0:38ceb79fef03 | 369 | pointer data() const |
kevman | 0:38ceb79fef03 | 370 | { |
kevman | 0:38ceb79fef03 | 371 | return _data; |
kevman | 0:38ceb79fef03 | 372 | } |
kevman | 0:38ceb79fef03 | 373 | |
kevman | 0:38ceb79fef03 | 374 | /** |
kevman | 0:38ceb79fef03 | 375 | * Create a new Span over the first @p Count elements of the existing view. |
kevman | 0:38ceb79fef03 | 376 | * |
kevman | 0:38ceb79fef03 | 377 | * @tparam Count The number of element viewed by the new Span |
kevman | 0:38ceb79fef03 | 378 | * |
kevman | 0:38ceb79fef03 | 379 | * @return A new Span over the first @p Count elements. |
kevman | 0:38ceb79fef03 | 380 | * |
kevman | 0:38ceb79fef03 | 381 | * @pre Count >= 0 && Count <= size(). |
kevman | 0:38ceb79fef03 | 382 | */ |
kevman | 0:38ceb79fef03 | 383 | template<ptrdiff_t Count> |
kevman | 0:38ceb79fef03 | 384 | Span<element_type, Count> first() const |
kevman | 0:38ceb79fef03 | 385 | { |
kevman | 0:38ceb79fef03 | 386 | MBED_STATIC_ASSERT( |
kevman | 0:38ceb79fef03 | 387 | (0 <= Count) && (Count <= Extent), |
kevman | 0:38ceb79fef03 | 388 | "Invalid subspan extent" |
kevman | 0:38ceb79fef03 | 389 | ); |
kevman | 0:38ceb79fef03 | 390 | return Span<element_type, Count>(_data, Count); |
kevman | 0:38ceb79fef03 | 391 | } |
kevman | 0:38ceb79fef03 | 392 | |
kevman | 0:38ceb79fef03 | 393 | /** |
kevman | 0:38ceb79fef03 | 394 | * Create a new Span over the last @p Count elements of the existing view. |
kevman | 0:38ceb79fef03 | 395 | * |
kevman | 0:38ceb79fef03 | 396 | * @tparam Count The number of element viewed by the new Span. |
kevman | 0:38ceb79fef03 | 397 | * |
kevman | 0:38ceb79fef03 | 398 | * @return A new Span over the last @p Count elements. |
kevman | 0:38ceb79fef03 | 399 | * |
kevman | 0:38ceb79fef03 | 400 | * @pre Count >= 0 && Count <= size(). |
kevman | 0:38ceb79fef03 | 401 | */ |
kevman | 0:38ceb79fef03 | 402 | template<ptrdiff_t Count> |
kevman | 0:38ceb79fef03 | 403 | Span<element_type, Count> last() const |
kevman | 0:38ceb79fef03 | 404 | { |
kevman | 0:38ceb79fef03 | 405 | MBED_STATIC_ASSERT( |
kevman | 0:38ceb79fef03 | 406 | (0 <= Count) && (Count <= Extent), |
kevman | 0:38ceb79fef03 | 407 | "Invalid subspan extent" |
kevman | 0:38ceb79fef03 | 408 | ); |
kevman | 0:38ceb79fef03 | 409 | return Span<element_type, Count>(_data + (Extent - Count), Count); |
kevman | 0:38ceb79fef03 | 410 | } |
kevman | 0:38ceb79fef03 | 411 | |
kevman | 0:38ceb79fef03 | 412 | /** |
kevman | 0:38ceb79fef03 | 413 | * Create a subspan that is a view of other Count elements; the view starts at |
kevman | 0:38ceb79fef03 | 414 | * element Offset. |
kevman | 0:38ceb79fef03 | 415 | * |
kevman | 0:38ceb79fef03 | 416 | * @tparam Offset The offset of the first element viewed by the subspan. |
kevman | 0:38ceb79fef03 | 417 | * |
kevman | 0:38ceb79fef03 | 418 | * @tparam Count The number of elements present in the subspan. If Count |
kevman | 0:38ceb79fef03 | 419 | * is equal to SPAN_DYNAMIC_EXTENT, then a Span starting at offset and |
kevman | 0:38ceb79fef03 | 420 | * containing the rest of the elements is returned. |
kevman | 0:38ceb79fef03 | 421 | * |
kevman | 0:38ceb79fef03 | 422 | * @return A subspan of this starting at Offset and Count long. |
kevman | 0:38ceb79fef03 | 423 | */ |
kevman | 0:38ceb79fef03 | 424 | template<std::ptrdiff_t Offset, std::ptrdiff_t Count> |
kevman | 0:38ceb79fef03 | 425 | Span<element_type, Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count> |
kevman | 0:38ceb79fef03 | 426 | subspan() const |
kevman | 0:38ceb79fef03 | 427 | { |
kevman | 0:38ceb79fef03 | 428 | MBED_STATIC_ASSERT( |
kevman | 0:38ceb79fef03 | 429 | 0 <= Offset && Offset <= Extent, |
kevman | 0:38ceb79fef03 | 430 | "Invalid subspan offset" |
kevman | 0:38ceb79fef03 | 431 | ); |
kevman | 0:38ceb79fef03 | 432 | MBED_STATIC_ASSERT( |
kevman | 0:38ceb79fef03 | 433 | (Count == SPAN_DYNAMIC_EXTENT) || |
kevman | 0:38ceb79fef03 | 434 | (0 <= Count && (Count + Offset) <= Extent), |
kevman | 0:38ceb79fef03 | 435 | "Invalid subspan count" |
kevman | 0:38ceb79fef03 | 436 | ); |
kevman | 0:38ceb79fef03 | 437 | return Span<element_type, Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count>( |
kevman | 0:38ceb79fef03 | 438 | _data + Offset, |
kevman | 0:38ceb79fef03 | 439 | Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count |
kevman | 0:38ceb79fef03 | 440 | ); |
kevman | 0:38ceb79fef03 | 441 | } |
kevman | 0:38ceb79fef03 | 442 | |
kevman | 0:38ceb79fef03 | 443 | /** |
kevman | 0:38ceb79fef03 | 444 | * Create a new Span over the first @p count elements of the existing view. |
kevman | 0:38ceb79fef03 | 445 | * |
kevman | 0:38ceb79fef03 | 446 | * @param count The number of element viewed by the new Span. |
kevman | 0:38ceb79fef03 | 447 | * |
kevman | 0:38ceb79fef03 | 448 | * @return A new Span over the first @p count elements. |
kevman | 0:38ceb79fef03 | 449 | */ |
kevman | 0:38ceb79fef03 | 450 | Span<element_type, SPAN_DYNAMIC_EXTENT> first(index_type count) const |
kevman | 0:38ceb79fef03 | 451 | { |
kevman | 0:38ceb79fef03 | 452 | MBED_ASSERT(0 <= count && count <= Extent); |
kevman | 0:38ceb79fef03 | 453 | return Span<element_type, SPAN_DYNAMIC_EXTENT>(_data, count); |
kevman | 0:38ceb79fef03 | 454 | } |
kevman | 0:38ceb79fef03 | 455 | |
kevman | 0:38ceb79fef03 | 456 | /** |
kevman | 0:38ceb79fef03 | 457 | * Create a new Span over the last @p count elements of the existing view. |
kevman | 0:38ceb79fef03 | 458 | * |
kevman | 0:38ceb79fef03 | 459 | * @param count The number of elements viewed by the new Span. |
kevman | 0:38ceb79fef03 | 460 | * |
kevman | 0:38ceb79fef03 | 461 | * @return A new Span over the last @p count elements. |
kevman | 0:38ceb79fef03 | 462 | */ |
kevman | 0:38ceb79fef03 | 463 | Span<element_type, SPAN_DYNAMIC_EXTENT> last(index_type count) const |
kevman | 0:38ceb79fef03 | 464 | { |
kevman | 0:38ceb79fef03 | 465 | MBED_ASSERT(0 <= count && count <= Extent); |
kevman | 0:38ceb79fef03 | 466 | return Span<element_type, SPAN_DYNAMIC_EXTENT>( |
kevman | 0:38ceb79fef03 | 467 | _data + (Extent - count), |
kevman | 0:38ceb79fef03 | 468 | count |
kevman | 0:38ceb79fef03 | 469 | ); |
kevman | 0:38ceb79fef03 | 470 | } |
kevman | 0:38ceb79fef03 | 471 | |
kevman | 0:38ceb79fef03 | 472 | /** |
kevman | 0:38ceb79fef03 | 473 | * Create a subspan that is a view of other count elements; the view starts at |
kevman | 0:38ceb79fef03 | 474 | * element offset. |
kevman | 0:38ceb79fef03 | 475 | * |
kevman | 0:38ceb79fef03 | 476 | * @param offset The offset of the first element viewed by the subspan. |
kevman | 0:38ceb79fef03 | 477 | * |
kevman | 0:38ceb79fef03 | 478 | * @param count The number of elements present in the subspan. If Count |
kevman | 0:38ceb79fef03 | 479 | * is equal to SPAN_DYNAMIC_EXTENT, then a span starting at offset and |
kevman | 0:38ceb79fef03 | 480 | * containing the rest of the elements is returned. |
kevman | 0:38ceb79fef03 | 481 | * |
kevman | 0:38ceb79fef03 | 482 | * @return |
kevman | 0:38ceb79fef03 | 483 | */ |
kevman | 0:38ceb79fef03 | 484 | Span<element_type, SPAN_DYNAMIC_EXTENT> subspan( |
kevman | 0:38ceb79fef03 | 485 | index_type offset, index_type count = SPAN_DYNAMIC_EXTENT |
kevman | 0:38ceb79fef03 | 486 | ) const |
kevman | 0:38ceb79fef03 | 487 | { |
kevman | 0:38ceb79fef03 | 488 | MBED_ASSERT(0 <= offset && offset <= Extent); |
kevman | 0:38ceb79fef03 | 489 | MBED_ASSERT( |
kevman | 0:38ceb79fef03 | 490 | (count == SPAN_DYNAMIC_EXTENT) || |
kevman | 0:38ceb79fef03 | 491 | (0 <= count && (count + offset) <= Extent) |
kevman | 0:38ceb79fef03 | 492 | ); |
kevman | 0:38ceb79fef03 | 493 | return Span<element_type, SPAN_DYNAMIC_EXTENT>( |
kevman | 0:38ceb79fef03 | 494 | _data + offset, |
kevman | 0:38ceb79fef03 | 495 | count == SPAN_DYNAMIC_EXTENT ? Extent - offset : count |
kevman | 0:38ceb79fef03 | 496 | ); |
kevman | 0:38ceb79fef03 | 497 | } |
kevman | 0:38ceb79fef03 | 498 | |
kevman | 0:38ceb79fef03 | 499 | private: |
kevman | 0:38ceb79fef03 | 500 | pointer _data; |
kevman | 0:38ceb79fef03 | 501 | }; |
kevman | 0:38ceb79fef03 | 502 | |
kevman | 0:38ceb79fef03 | 503 | /** |
kevman | 0:38ceb79fef03 | 504 | * Span specialization that handle dynamic size. |
kevman | 0:38ceb79fef03 | 505 | */ |
kevman | 0:38ceb79fef03 | 506 | template<typename ElementType> |
kevman | 0:38ceb79fef03 | 507 | struct Span<ElementType, SPAN_DYNAMIC_EXTENT> { |
kevman | 0:38ceb79fef03 | 508 | /** |
kevman | 0:38ceb79fef03 | 509 | * Type of the element contained. |
kevman | 0:38ceb79fef03 | 510 | */ |
kevman | 0:38ceb79fef03 | 511 | typedef ElementType element_type; |
kevman | 0:38ceb79fef03 | 512 | |
kevman | 0:38ceb79fef03 | 513 | /** |
kevman | 0:38ceb79fef03 | 514 | * Type of the index. |
kevman | 0:38ceb79fef03 | 515 | */ |
kevman | 0:38ceb79fef03 | 516 | typedef ptrdiff_t index_type; |
kevman | 0:38ceb79fef03 | 517 | |
kevman | 0:38ceb79fef03 | 518 | /** |
kevman | 0:38ceb79fef03 | 519 | * Pointer to an ElementType. |
kevman | 0:38ceb79fef03 | 520 | */ |
kevman | 0:38ceb79fef03 | 521 | typedef element_type *pointer; |
kevman | 0:38ceb79fef03 | 522 | |
kevman | 0:38ceb79fef03 | 523 | /** |
kevman | 0:38ceb79fef03 | 524 | * Reference to an ElementType. |
kevman | 0:38ceb79fef03 | 525 | */ |
kevman | 0:38ceb79fef03 | 526 | typedef element_type &reference; |
kevman | 0:38ceb79fef03 | 527 | |
kevman | 0:38ceb79fef03 | 528 | /** |
kevman | 0:38ceb79fef03 | 529 | * Size of the Extent; -1 if dynamic. |
kevman | 0:38ceb79fef03 | 530 | */ |
kevman | 0:38ceb79fef03 | 531 | static const index_type extent = SPAN_DYNAMIC_EXTENT; |
kevman | 0:38ceb79fef03 | 532 | |
kevman | 0:38ceb79fef03 | 533 | /** |
kevman | 0:38ceb79fef03 | 534 | * Construct an empty Span. |
kevman | 0:38ceb79fef03 | 535 | * |
kevman | 0:38ceb79fef03 | 536 | * @post a call to size() returns 0, and data() returns NULL. |
kevman | 0:38ceb79fef03 | 537 | * |
kevman | 0:38ceb79fef03 | 538 | * @note This function is not accessible if Extent != SPAN_DYNAMIC_EXTENT or |
kevman | 0:38ceb79fef03 | 539 | * Extent != 0 . |
kevman | 0:38ceb79fef03 | 540 | */ |
kevman | 0:38ceb79fef03 | 541 | Span() : |
kevman | 0:38ceb79fef03 | 542 | _data(NULL), _size(0) { } |
kevman | 0:38ceb79fef03 | 543 | |
kevman | 0:38ceb79fef03 | 544 | /** |
kevman | 0:38ceb79fef03 | 545 | * Construct a Span from a pointer to a buffer and its size. |
kevman | 0:38ceb79fef03 | 546 | * |
kevman | 0:38ceb79fef03 | 547 | * @param ptr Pointer to the beginning of the data viewed. |
kevman | 0:38ceb79fef03 | 548 | * |
kevman | 0:38ceb79fef03 | 549 | * @param count Number of elements viewed. |
kevman | 0:38ceb79fef03 | 550 | * |
kevman | 0:38ceb79fef03 | 551 | * @pre [ptr, ptr + count) must be be a valid range. |
kevman | 0:38ceb79fef03 | 552 | * @pre count must be equal to extent. |
kevman | 0:38ceb79fef03 | 553 | * |
kevman | 0:38ceb79fef03 | 554 | * @post a call to size() returns count, and data() returns @p ptr. |
kevman | 0:38ceb79fef03 | 555 | */ |
kevman | 0:38ceb79fef03 | 556 | Span(pointer ptr, index_type count) : |
kevman | 0:38ceb79fef03 | 557 | _data(ptr), _size(count) |
kevman | 0:38ceb79fef03 | 558 | { |
kevman | 0:38ceb79fef03 | 559 | MBED_ASSERT(count >= 0); |
kevman | 0:38ceb79fef03 | 560 | MBED_ASSERT(ptr != NULL || count == 0); |
kevman | 0:38ceb79fef03 | 561 | } |
kevman | 0:38ceb79fef03 | 562 | |
kevman | 0:38ceb79fef03 | 563 | /** |
kevman | 0:38ceb79fef03 | 564 | * Construct a Span from the range [first, last). |
kevman | 0:38ceb79fef03 | 565 | * |
kevman | 0:38ceb79fef03 | 566 | * @param first Pointer to the beginning of the data viewed. |
kevman | 0:38ceb79fef03 | 567 | * @param last End of the range (element after the last element). |
kevman | 0:38ceb79fef03 | 568 | * |
kevman | 0:38ceb79fef03 | 569 | * @pre [first, last) must be be a valid range. |
kevman | 0:38ceb79fef03 | 570 | * @pre first <= last. |
kevman | 0:38ceb79fef03 | 571 | * |
kevman | 0:38ceb79fef03 | 572 | * @post a call to size() returns the result of (last - first), and |
kevman | 0:38ceb79fef03 | 573 | * data() returns @p first. |
kevman | 0:38ceb79fef03 | 574 | */ |
kevman | 0:38ceb79fef03 | 575 | Span(pointer first, pointer last) : |
kevman | 0:38ceb79fef03 | 576 | _data(first), _size(last - first) |
kevman | 0:38ceb79fef03 | 577 | { |
kevman | 0:38ceb79fef03 | 578 | MBED_ASSERT(first <= last); |
kevman | 0:38ceb79fef03 | 579 | MBED_ASSERT(first != NULL || (last - first) == 0); |
kevman | 0:38ceb79fef03 | 580 | } |
kevman | 0:38ceb79fef03 | 581 | |
kevman | 0:38ceb79fef03 | 582 | /** |
kevman | 0:38ceb79fef03 | 583 | * Construct a Span from the reference to an array. |
kevman | 0:38ceb79fef03 | 584 | * |
kevman | 0:38ceb79fef03 | 585 | * @param elements Reference to the array viewed. |
kevman | 0:38ceb79fef03 | 586 | * |
kevman | 0:38ceb79fef03 | 587 | * @tparam Count Number of elements of T presents in the array. |
kevman | 0:38ceb79fef03 | 588 | * |
kevman | 0:38ceb79fef03 | 589 | * @post a call to size() returns Count, and data() returns a |
kevman | 0:38ceb79fef03 | 590 | * pointer to elements. |
kevman | 0:38ceb79fef03 | 591 | */ |
kevman | 0:38ceb79fef03 | 592 | template<size_t Count> |
kevman | 0:38ceb79fef03 | 593 | Span(element_type (&elements)[Count]): |
kevman | 0:38ceb79fef03 | 594 | _data(elements), _size(Count) { } |
kevman | 0:38ceb79fef03 | 595 | |
kevman | 0:38ceb79fef03 | 596 | /** |
kevman | 0:38ceb79fef03 | 597 | * Construct a Span object from another Span. |
kevman | 0:38ceb79fef03 | 598 | * |
kevman | 0:38ceb79fef03 | 599 | * @param other The Span object used to construct this. |
kevman | 0:38ceb79fef03 | 600 | * |
kevman | 0:38ceb79fef03 | 601 | * @note For Span with a positive extent, this function is not accessible. |
kevman | 0:38ceb79fef03 | 602 | * |
kevman | 0:38ceb79fef03 | 603 | * @note OtherElementType(*)[] must be convertible to ElementType(*)[]. |
kevman | 0:38ceb79fef03 | 604 | */ |
kevman | 0:38ceb79fef03 | 605 | template<typename OtherElementType, ptrdiff_t OtherExtent> |
kevman | 0:38ceb79fef03 | 606 | Span(const Span<OtherElementType, OtherExtent> &other): |
kevman | 0:38ceb79fef03 | 607 | _data(other.data()), _size(other.size()) |
kevman | 0:38ceb79fef03 | 608 | { |
kevman | 0:38ceb79fef03 | 609 | MBED_STATIC_ASSERT( |
kevman | 0:38ceb79fef03 | 610 | (span_detail::is_convertible<OtherElementType (*)[1], ElementType (*)[1]>::value), |
kevman | 0:38ceb79fef03 | 611 | "OtherElementType(*)[] should be convertible to ElementType (*)[]" |
kevman | 0:38ceb79fef03 | 612 | ); |
kevman | 0:38ceb79fef03 | 613 | } |
kevman | 0:38ceb79fef03 | 614 | |
kevman | 0:38ceb79fef03 | 615 | /** |
kevman | 0:38ceb79fef03 | 616 | * Return the size of the array viewed. |
kevman | 0:38ceb79fef03 | 617 | * |
kevman | 0:38ceb79fef03 | 618 | * @return The number of elements present in the array viewed. |
kevman | 0:38ceb79fef03 | 619 | */ |
kevman | 0:38ceb79fef03 | 620 | index_type size() const |
kevman | 0:38ceb79fef03 | 621 | { |
kevman | 0:38ceb79fef03 | 622 | return _size; |
kevman | 0:38ceb79fef03 | 623 | } |
kevman | 0:38ceb79fef03 | 624 | |
kevman | 0:38ceb79fef03 | 625 | /** |
kevman | 0:38ceb79fef03 | 626 | * Return if the sequence viewed is empty or not. |
kevman | 0:38ceb79fef03 | 627 | * |
kevman | 0:38ceb79fef03 | 628 | * @return true if the sequence is empty and false otherwise. |
kevman | 0:38ceb79fef03 | 629 | */ |
kevman | 0:38ceb79fef03 | 630 | bool empty() const |
kevman | 0:38ceb79fef03 | 631 | { |
kevman | 0:38ceb79fef03 | 632 | return size() == 0; |
kevman | 0:38ceb79fef03 | 633 | } |
kevman | 0:38ceb79fef03 | 634 | |
kevman | 0:38ceb79fef03 | 635 | /** |
kevman | 0:38ceb79fef03 | 636 | * Access to an element of the sequence. |
kevman | 0:38ceb79fef03 | 637 | * |
kevman | 0:38ceb79fef03 | 638 | * @param index Element index to access. |
kevman | 0:38ceb79fef03 | 639 | * |
kevman | 0:38ceb79fef03 | 640 | * @return A reference to the element at the index specified in input. |
kevman | 0:38ceb79fef03 | 641 | * |
kevman | 0:38ceb79fef03 | 642 | * @pre index is less than size(). |
kevman | 0:38ceb79fef03 | 643 | */ |
kevman | 0:38ceb79fef03 | 644 | reference operator[](index_type index) const |
kevman | 0:38ceb79fef03 | 645 | { |
kevman | 0:38ceb79fef03 | 646 | #ifdef MBED_DEBUG |
kevman | 0:38ceb79fef03 | 647 | MBED_ASSERT(0 <= index && index < _size); |
kevman | 0:38ceb79fef03 | 648 | #endif |
kevman | 0:38ceb79fef03 | 649 | return _data[index]; |
kevman | 0:38ceb79fef03 | 650 | } |
kevman | 0:38ceb79fef03 | 651 | |
kevman | 0:38ceb79fef03 | 652 | /** |
kevman | 0:38ceb79fef03 | 653 | * Get the raw pointer to the sequence viewed. |
kevman | 0:38ceb79fef03 | 654 | * |
kevman | 0:38ceb79fef03 | 655 | * @return The raw pointer to the first element viewed. |
kevman | 0:38ceb79fef03 | 656 | */ |
kevman | 0:38ceb79fef03 | 657 | pointer data() const |
kevman | 0:38ceb79fef03 | 658 | { |
kevman | 0:38ceb79fef03 | 659 | return _data; |
kevman | 0:38ceb79fef03 | 660 | } |
kevman | 0:38ceb79fef03 | 661 | |
kevman | 0:38ceb79fef03 | 662 | /** |
kevman | 0:38ceb79fef03 | 663 | * Create a new Span over the first @p Count elements of the existing view. |
kevman | 0:38ceb79fef03 | 664 | * |
kevman | 0:38ceb79fef03 | 665 | * @tparam Count The number of elements viewed by the new Span. |
kevman | 0:38ceb79fef03 | 666 | * |
kevman | 0:38ceb79fef03 | 667 | * @return A new Span over the first @p Count elements. |
kevman | 0:38ceb79fef03 | 668 | * |
kevman | 0:38ceb79fef03 | 669 | * @pre Count >= 0 && Count <= size(). |
kevman | 0:38ceb79fef03 | 670 | */ |
kevman | 0:38ceb79fef03 | 671 | template<ptrdiff_t Count> |
kevman | 0:38ceb79fef03 | 672 | Span<element_type, Count> first() const |
kevman | 0:38ceb79fef03 | 673 | { |
kevman | 0:38ceb79fef03 | 674 | MBED_ASSERT((Count >= 0) && (Count <= _size)); |
kevman | 0:38ceb79fef03 | 675 | return Span<element_type, Count>(_data, Count); |
kevman | 0:38ceb79fef03 | 676 | } |
kevman | 0:38ceb79fef03 | 677 | |
kevman | 0:38ceb79fef03 | 678 | /** |
kevman | 0:38ceb79fef03 | 679 | * Create a new Span over the last @p Count elements of the existing view. |
kevman | 0:38ceb79fef03 | 680 | * |
kevman | 0:38ceb79fef03 | 681 | * @tparam Count The number of elements viewed by the new Span. |
kevman | 0:38ceb79fef03 | 682 | * |
kevman | 0:38ceb79fef03 | 683 | * @return A new Span over the last @p Count elements. |
kevman | 0:38ceb79fef03 | 684 | * |
kevman | 0:38ceb79fef03 | 685 | * @pre Count >= 0 && Count <= size(). |
kevman | 0:38ceb79fef03 | 686 | */ |
kevman | 0:38ceb79fef03 | 687 | template<ptrdiff_t Count> |
kevman | 0:38ceb79fef03 | 688 | Span<element_type, Count> last() const |
kevman | 0:38ceb79fef03 | 689 | { |
kevman | 0:38ceb79fef03 | 690 | MBED_ASSERT((0 <= Count) && (Count <= _size)); |
kevman | 0:38ceb79fef03 | 691 | return Span<element_type, Count>(_data + (_size - Count), Count); |
kevman | 0:38ceb79fef03 | 692 | } |
kevman | 0:38ceb79fef03 | 693 | |
kevman | 0:38ceb79fef03 | 694 | /** |
kevman | 0:38ceb79fef03 | 695 | * Create a subspan that is a view other Count elements; the view starts at |
kevman | 0:38ceb79fef03 | 696 | * element Offset. |
kevman | 0:38ceb79fef03 | 697 | * |
kevman | 0:38ceb79fef03 | 698 | * @tparam Offset The offset of the first element viewed by the subspan. |
kevman | 0:38ceb79fef03 | 699 | * |
kevman | 0:38ceb79fef03 | 700 | * @tparam Count The number of elements present in the subspan. If Count |
kevman | 0:38ceb79fef03 | 701 | * is equal to SPAN_DYNAMIC_EXTENT, then a Span starting at offset and |
kevman | 0:38ceb79fef03 | 702 | * containing the rest of the elements is returned. |
kevman | 0:38ceb79fef03 | 703 | * |
kevman | 0:38ceb79fef03 | 704 | * @return A subspan of this starting at Offset and Count long. |
kevman | 0:38ceb79fef03 | 705 | */ |
kevman | 0:38ceb79fef03 | 706 | template<std::ptrdiff_t Offset, std::ptrdiff_t Count> |
kevman | 0:38ceb79fef03 | 707 | Span<element_type, Count> |
kevman | 0:38ceb79fef03 | 708 | subspan() const |
kevman | 0:38ceb79fef03 | 709 | { |
kevman | 0:38ceb79fef03 | 710 | MBED_ASSERT(0 <= Offset && Offset <= _size); |
kevman | 0:38ceb79fef03 | 711 | MBED_ASSERT( |
kevman | 0:38ceb79fef03 | 712 | (Count == SPAN_DYNAMIC_EXTENT) || |
kevman | 0:38ceb79fef03 | 713 | (0 <= Count && (Count + Offset) <= _size) |
kevman | 0:38ceb79fef03 | 714 | ); |
kevman | 0:38ceb79fef03 | 715 | return Span<element_type, Count>( |
kevman | 0:38ceb79fef03 | 716 | _data + Offset, |
kevman | 0:38ceb79fef03 | 717 | Count == SPAN_DYNAMIC_EXTENT ? _size - Offset : Count |
kevman | 0:38ceb79fef03 | 718 | ); |
kevman | 0:38ceb79fef03 | 719 | } |
kevman | 0:38ceb79fef03 | 720 | |
kevman | 0:38ceb79fef03 | 721 | /** |
kevman | 0:38ceb79fef03 | 722 | * Create a new Span over the first @p count elements of the existing view. |
kevman | 0:38ceb79fef03 | 723 | * |
kevman | 0:38ceb79fef03 | 724 | * @param count The number of elements viewed by the new Span. |
kevman | 0:38ceb79fef03 | 725 | * |
kevman | 0:38ceb79fef03 | 726 | * @return A new Span over the first @p count elements. |
kevman | 0:38ceb79fef03 | 727 | */ |
kevman | 0:38ceb79fef03 | 728 | Span<element_type, SPAN_DYNAMIC_EXTENT> first(index_type count) const |
kevman | 0:38ceb79fef03 | 729 | { |
kevman | 0:38ceb79fef03 | 730 | MBED_ASSERT(0 <= count && count <= _size); |
kevman | 0:38ceb79fef03 | 731 | return Span<element_type, SPAN_DYNAMIC_EXTENT>(_data, count); |
kevman | 0:38ceb79fef03 | 732 | } |
kevman | 0:38ceb79fef03 | 733 | |
kevman | 0:38ceb79fef03 | 734 | /** |
kevman | 0:38ceb79fef03 | 735 | * Create a new Span over the last @p count elements of the existing view. |
kevman | 0:38ceb79fef03 | 736 | * |
kevman | 0:38ceb79fef03 | 737 | * @param count The number of elements viewed by the new Span. |
kevman | 0:38ceb79fef03 | 738 | * |
kevman | 0:38ceb79fef03 | 739 | * @return A new Span over the last @p count elements. |
kevman | 0:38ceb79fef03 | 740 | */ |
kevman | 0:38ceb79fef03 | 741 | Span<element_type, SPAN_DYNAMIC_EXTENT> last(index_type count) const |
kevman | 0:38ceb79fef03 | 742 | { |
kevman | 0:38ceb79fef03 | 743 | MBED_ASSERT(0 <= count && count <= _size); |
kevman | 0:38ceb79fef03 | 744 | return Span<element_type, SPAN_DYNAMIC_EXTENT>( |
kevman | 0:38ceb79fef03 | 745 | _data + (_size - count), |
kevman | 0:38ceb79fef03 | 746 | count |
kevman | 0:38ceb79fef03 | 747 | ); |
kevman | 0:38ceb79fef03 | 748 | } |
kevman | 0:38ceb79fef03 | 749 | |
kevman | 0:38ceb79fef03 | 750 | /** |
kevman | 0:38ceb79fef03 | 751 | * Create a subspan that is a view of other count elements; the view starts at |
kevman | 0:38ceb79fef03 | 752 | * element offset. |
kevman | 0:38ceb79fef03 | 753 | * |
kevman | 0:38ceb79fef03 | 754 | * @param offset The offset of the first element viewed by the subspan. |
kevman | 0:38ceb79fef03 | 755 | * |
kevman | 0:38ceb79fef03 | 756 | * @param count The number of elements present in the subspan. If Count |
kevman | 0:38ceb79fef03 | 757 | * is equal to SPAN_DYNAMIC_EXTENT, then a Span starting at offset and |
kevman | 0:38ceb79fef03 | 758 | * containing the rest of the elements is returned. |
kevman | 0:38ceb79fef03 | 759 | * |
kevman | 0:38ceb79fef03 | 760 | * @return A subspan of this starting at offset and count long. |
kevman | 0:38ceb79fef03 | 761 | */ |
kevman | 0:38ceb79fef03 | 762 | Span<element_type, SPAN_DYNAMIC_EXTENT> subspan( |
kevman | 0:38ceb79fef03 | 763 | index_type offset, index_type count = SPAN_DYNAMIC_EXTENT |
kevman | 0:38ceb79fef03 | 764 | ) const |
kevman | 0:38ceb79fef03 | 765 | { |
kevman | 0:38ceb79fef03 | 766 | MBED_ASSERT(0 <= offset && offset <= _size); |
kevman | 0:38ceb79fef03 | 767 | MBED_ASSERT( |
kevman | 0:38ceb79fef03 | 768 | (count == SPAN_DYNAMIC_EXTENT) || |
kevman | 0:38ceb79fef03 | 769 | (0 <= count && (count + offset) <= _size) |
kevman | 0:38ceb79fef03 | 770 | ); |
kevman | 0:38ceb79fef03 | 771 | return Span<element_type, SPAN_DYNAMIC_EXTENT>( |
kevman | 0:38ceb79fef03 | 772 | _data + offset, |
kevman | 0:38ceb79fef03 | 773 | count == SPAN_DYNAMIC_EXTENT ? _size - offset : count |
kevman | 0:38ceb79fef03 | 774 | ); |
kevman | 0:38ceb79fef03 | 775 | } |
kevman | 0:38ceb79fef03 | 776 | |
kevman | 0:38ceb79fef03 | 777 | private: |
kevman | 0:38ceb79fef03 | 778 | pointer _data; |
kevman | 0:38ceb79fef03 | 779 | index_type _size; |
kevman | 0:38ceb79fef03 | 780 | }; |
kevman | 0:38ceb79fef03 | 781 | |
kevman | 0:38ceb79fef03 | 782 | /** |
kevman | 0:38ceb79fef03 | 783 | * Equality operator between two Span objects. |
kevman | 0:38ceb79fef03 | 784 | * |
kevman | 0:38ceb79fef03 | 785 | * @param lhs Left side of the binary operation. |
kevman | 0:38ceb79fef03 | 786 | * @param rhs Right side of the binary operation. |
kevman | 0:38ceb79fef03 | 787 | * |
kevman | 0:38ceb79fef03 | 788 | * @return True if Spans in input have the same size and the same content and |
kevman | 0:38ceb79fef03 | 789 | * false otherwise. |
kevman | 0:38ceb79fef03 | 790 | * |
kevman | 0:38ceb79fef03 | 791 | * @relates Span |
kevman | 0:38ceb79fef03 | 792 | */ |
kevman | 0:38ceb79fef03 | 793 | template<typename T, typename U, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent> |
kevman | 0:38ceb79fef03 | 794 | bool operator==(const Span<T, LhsExtent> &lhs, const Span<U, RhsExtent> &rhs) |
kevman | 0:38ceb79fef03 | 795 | { |
kevman | 0:38ceb79fef03 | 796 | if (lhs.size() != rhs.size()) { |
kevman | 0:38ceb79fef03 | 797 | return false; |
kevman | 0:38ceb79fef03 | 798 | } |
kevman | 0:38ceb79fef03 | 799 | |
kevman | 0:38ceb79fef03 | 800 | if (lhs.data() == rhs.data()) { |
kevman | 0:38ceb79fef03 | 801 | return true; |
kevman | 0:38ceb79fef03 | 802 | } |
kevman | 0:38ceb79fef03 | 803 | |
kevman | 0:38ceb79fef03 | 804 | return std::equal(lhs.data(), lhs.data() + lhs.size(), rhs.data()); |
kevman | 0:38ceb79fef03 | 805 | } |
kevman | 0:38ceb79fef03 | 806 | |
kevman | 0:38ceb79fef03 | 807 | /** |
kevman | 0:38ceb79fef03 | 808 | * Equality operation between a Span and a reference to a C++ array. |
kevman | 0:38ceb79fef03 | 809 | * |
kevman | 0:38ceb79fef03 | 810 | * @param lhs Left side of the binary operation. |
kevman | 0:38ceb79fef03 | 811 | * @param rhs Right side of the binary operation. |
kevman | 0:38ceb79fef03 | 812 | * |
kevman | 0:38ceb79fef03 | 813 | * @return True if elements in input have the same size and the same content and |
kevman | 0:38ceb79fef03 | 814 | * false otherwise. |
kevman | 0:38ceb79fef03 | 815 | */ |
kevman | 0:38ceb79fef03 | 816 | template<typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent> |
kevman | 0:38ceb79fef03 | 817 | bool operator==(const Span<T, LhsExtent> &lhs, T (&rhs)[RhsExtent]) |
kevman | 0:38ceb79fef03 | 818 | { |
kevman | 0:38ceb79fef03 | 819 | return lhs == Span<T>(rhs); |
kevman | 0:38ceb79fef03 | 820 | } |
kevman | 0:38ceb79fef03 | 821 | |
kevman | 0:38ceb79fef03 | 822 | /** |
kevman | 0:38ceb79fef03 | 823 | * Equality operation between a Span and a reference to a C++ array. |
kevman | 0:38ceb79fef03 | 824 | * |
kevman | 0:38ceb79fef03 | 825 | * @param lhs Left side of the binary operation. |
kevman | 0:38ceb79fef03 | 826 | * @param rhs Right side of the binary operation. |
kevman | 0:38ceb79fef03 | 827 | * |
kevman | 0:38ceb79fef03 | 828 | * @return True if elements in input have the same size and the same content |
kevman | 0:38ceb79fef03 | 829 | * and false otherwise. |
kevman | 0:38ceb79fef03 | 830 | */ |
kevman | 0:38ceb79fef03 | 831 | template<typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent> |
kevman | 0:38ceb79fef03 | 832 | bool operator==(T (&lhs)[LhsExtent], const Span<T, RhsExtent> &rhs) |
kevman | 0:38ceb79fef03 | 833 | { |
kevman | 0:38ceb79fef03 | 834 | return Span<T>(lhs) == rhs; |
kevman | 0:38ceb79fef03 | 835 | } |
kevman | 0:38ceb79fef03 | 836 | |
kevman | 0:38ceb79fef03 | 837 | /** |
kevman | 0:38ceb79fef03 | 838 | * Not equal operator |
kevman | 0:38ceb79fef03 | 839 | * |
kevman | 0:38ceb79fef03 | 840 | * @param lhs Left side of the binary operation. |
kevman | 0:38ceb79fef03 | 841 | * @param rhs Right side of the binary operation. |
kevman | 0:38ceb79fef03 | 842 | * |
kevman | 0:38ceb79fef03 | 843 | * @return True if arrays in input do not have the same size or the same content |
kevman | 0:38ceb79fef03 | 844 | * and false otherwise. |
kevman | 0:38ceb79fef03 | 845 | * |
kevman | 0:38ceb79fef03 | 846 | * @relates Span |
kevman | 0:38ceb79fef03 | 847 | */ |
kevman | 0:38ceb79fef03 | 848 | template<typename T, typename U, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent> |
kevman | 0:38ceb79fef03 | 849 | bool operator!=(const Span<T, LhsExtent> &lhs, const Span<U, RhsExtent> &rhs) |
kevman | 0:38ceb79fef03 | 850 | { |
kevman | 0:38ceb79fef03 | 851 | return !(lhs == rhs); |
kevman | 0:38ceb79fef03 | 852 | } |
kevman | 0:38ceb79fef03 | 853 | |
kevman | 0:38ceb79fef03 | 854 | /** |
kevman | 0:38ceb79fef03 | 855 | * Not Equal operation between a Span and a reference to a C++ array. |
kevman | 0:38ceb79fef03 | 856 | * |
kevman | 0:38ceb79fef03 | 857 | * @param lhs Left side of the binary operation. |
kevman | 0:38ceb79fef03 | 858 | * @param rhs Right side of the binary operation. |
kevman | 0:38ceb79fef03 | 859 | * |
kevman | 0:38ceb79fef03 | 860 | * @return True if elements in input have the same size and the same content |
kevman | 0:38ceb79fef03 | 861 | * and false otherwise. |
kevman | 0:38ceb79fef03 | 862 | */ |
kevman | 0:38ceb79fef03 | 863 | template<typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent> |
kevman | 0:38ceb79fef03 | 864 | bool operator!=(const Span<T, LhsExtent> &lhs, T (&rhs)[RhsExtent]) |
kevman | 0:38ceb79fef03 | 865 | { |
kevman | 0:38ceb79fef03 | 866 | return !(lhs == Span<T, RhsExtent>(rhs)); |
kevman | 0:38ceb79fef03 | 867 | } |
kevman | 0:38ceb79fef03 | 868 | |
kevman | 0:38ceb79fef03 | 869 | /** |
kevman | 0:38ceb79fef03 | 870 | * Not Equal operation between a Span and a reference to a C++ array. |
kevman | 0:38ceb79fef03 | 871 | * |
kevman | 0:38ceb79fef03 | 872 | * @param lhs Left side of the binary operation. |
kevman | 0:38ceb79fef03 | 873 | * @param rhs Right side of the binary operation. |
kevman | 0:38ceb79fef03 | 874 | * |
kevman | 0:38ceb79fef03 | 875 | * @return True if elements in input have the same size and the same content |
kevman | 0:38ceb79fef03 | 876 | * and false otherwise. |
kevman | 0:38ceb79fef03 | 877 | */ |
kevman | 0:38ceb79fef03 | 878 | template<typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent> |
kevman | 0:38ceb79fef03 | 879 | bool operator!=(T (&lhs)[LhsExtent], const Span<T, RhsExtent> &rhs) |
kevman | 0:38ceb79fef03 | 880 | { |
kevman | 0:38ceb79fef03 | 881 | return !(Span<T, LhsExtent>(lhs) == rhs); |
kevman | 0:38ceb79fef03 | 882 | } |
kevman | 0:38ceb79fef03 | 883 | |
kevman | 0:38ceb79fef03 | 884 | /** |
kevman | 0:38ceb79fef03 | 885 | * Generate a Span from a reference to a C/C++ array. |
kevman | 0:38ceb79fef03 | 886 | * |
kevman | 0:38ceb79fef03 | 887 | * @tparam T Type of elements held in elements. |
kevman | 0:38ceb79fef03 | 888 | * @tparam Extent Number of items held in elements. |
kevman | 0:38ceb79fef03 | 889 | * |
kevman | 0:38ceb79fef03 | 890 | * @param elements The reference to the array viewed. |
kevman | 0:38ceb79fef03 | 891 | * |
kevman | 0:38ceb79fef03 | 892 | * @return The Span to elements. |
kevman | 0:38ceb79fef03 | 893 | * |
kevman | 0:38ceb79fef03 | 894 | * @note This helper avoids the typing of template parameter when Span is |
kevman | 0:38ceb79fef03 | 895 | * created 'inline'. |
kevman | 0:38ceb79fef03 | 896 | * |
kevman | 0:38ceb79fef03 | 897 | * @relates Span |
kevman | 0:38ceb79fef03 | 898 | */ |
kevman | 0:38ceb79fef03 | 899 | template<typename T, size_t Size> |
kevman | 0:38ceb79fef03 | 900 | Span<T, Size> make_Span(T (&elements)[Size]) |
kevman | 0:38ceb79fef03 | 901 | { |
kevman | 0:38ceb79fef03 | 902 | return Span<T, Size>(elements); |
kevman | 0:38ceb79fef03 | 903 | } |
kevman | 0:38ceb79fef03 | 904 | |
kevman | 0:38ceb79fef03 | 905 | /** |
kevman | 0:38ceb79fef03 | 906 | * Generate a Span from a pointer to a C/C++ array. |
kevman | 0:38ceb79fef03 | 907 | * |
kevman | 0:38ceb79fef03 | 908 | * @tparam Extent Number of items held in elements. |
kevman | 0:38ceb79fef03 | 909 | * @tparam T Type of elements held in elements. |
kevman | 0:38ceb79fef03 | 910 | * |
kevman | 0:38ceb79fef03 | 911 | * @param elements The reference to the array viewed. |
kevman | 0:38ceb79fef03 | 912 | * |
kevman | 0:38ceb79fef03 | 913 | * @return The Span to elements. |
kevman | 0:38ceb79fef03 | 914 | * |
kevman | 0:38ceb79fef03 | 915 | * @note This helper avoids the typing of template parameter when Span is |
kevman | 0:38ceb79fef03 | 916 | * created 'inline'. |
kevman | 0:38ceb79fef03 | 917 | */ |
kevman | 0:38ceb79fef03 | 918 | template<ptrdiff_t Extent, typename T> |
kevman | 0:38ceb79fef03 | 919 | Span<T, Extent> make_Span(T *elements) |
kevman | 0:38ceb79fef03 | 920 | { |
kevman | 0:38ceb79fef03 | 921 | return Span<T, Extent>(elements, Extent); |
kevman | 0:38ceb79fef03 | 922 | } |
kevman | 0:38ceb79fef03 | 923 | |
kevman | 0:38ceb79fef03 | 924 | /** |
kevman | 0:38ceb79fef03 | 925 | * Generate a Span from a C/C++ pointer and the size of the array. |
kevman | 0:38ceb79fef03 | 926 | * |
kevman | 0:38ceb79fef03 | 927 | * @tparam T Type of elements held in array_ptr. |
kevman | 0:38ceb79fef03 | 928 | * |
kevman | 0:38ceb79fef03 | 929 | * @param array_ptr The pointer to the array viewed. |
kevman | 0:38ceb79fef03 | 930 | * @param array_size The number of T elements in the array. |
kevman | 0:38ceb79fef03 | 931 | * |
kevman | 0:38ceb79fef03 | 932 | * @return The Span to array_ptr with a size of array_size. |
kevman | 0:38ceb79fef03 | 933 | * |
kevman | 0:38ceb79fef03 | 934 | * @note This helper avoids the typing of template parameter when Span is |
kevman | 0:38ceb79fef03 | 935 | * created 'inline'. |
kevman | 0:38ceb79fef03 | 936 | * |
kevman | 0:38ceb79fef03 | 937 | * @relates Span |
kevman | 0:38ceb79fef03 | 938 | */ |
kevman | 0:38ceb79fef03 | 939 | template<typename T> |
kevman | 0:38ceb79fef03 | 940 | Span<T> make_Span(T *array_ptr, ptrdiff_t array_size) |
kevman | 0:38ceb79fef03 | 941 | { |
kevman | 0:38ceb79fef03 | 942 | return Span<T>(array_ptr, array_size); |
kevman | 0:38ceb79fef03 | 943 | } |
kevman | 0:38ceb79fef03 | 944 | |
kevman | 0:38ceb79fef03 | 945 | /** |
kevman | 0:38ceb79fef03 | 946 | * Generate a Span to a const content from a reference to a C/C++ array. |
kevman | 0:38ceb79fef03 | 947 | * |
kevman | 0:38ceb79fef03 | 948 | * @tparam T Type of elements held in elements. |
kevman | 0:38ceb79fef03 | 949 | * @tparam Extent Number of items held in elements. |
kevman | 0:38ceb79fef03 | 950 | * |
kevman | 0:38ceb79fef03 | 951 | * @param elements The array viewed. |
kevman | 0:38ceb79fef03 | 952 | * @return The Span to elements. |
kevman | 0:38ceb79fef03 | 953 | * |
kevman | 0:38ceb79fef03 | 954 | * @note This helper avoids the typing of template parameter when Span is |
kevman | 0:38ceb79fef03 | 955 | * created 'inline'. |
kevman | 0:38ceb79fef03 | 956 | */ |
kevman | 0:38ceb79fef03 | 957 | template<typename T, size_t Extent> |
kevman | 0:38ceb79fef03 | 958 | Span<const T, Extent> make_const_Span(const T (&elements)[Extent]) |
kevman | 0:38ceb79fef03 | 959 | { |
kevman | 0:38ceb79fef03 | 960 | return Span<const T, Extent>(elements); |
kevman | 0:38ceb79fef03 | 961 | } |
kevman | 0:38ceb79fef03 | 962 | |
kevman | 0:38ceb79fef03 | 963 | /** |
kevman | 0:38ceb79fef03 | 964 | * Generate a Span to a const content from a pointer to a C/C++ array. |
kevman | 0:38ceb79fef03 | 965 | * |
kevman | 0:38ceb79fef03 | 966 | * @tparam Extent Number of items held in elements. |
kevman | 0:38ceb79fef03 | 967 | * @tparam T Type of elements held in elements. |
kevman | 0:38ceb79fef03 | 968 | * |
kevman | 0:38ceb79fef03 | 969 | * @param elements The reference to the array viewed. |
kevman | 0:38ceb79fef03 | 970 | * |
kevman | 0:38ceb79fef03 | 971 | * @return The Span to elements. |
kevman | 0:38ceb79fef03 | 972 | * |
kevman | 0:38ceb79fef03 | 973 | * @note This helper avoids the typing of template parameter when Span is |
kevman | 0:38ceb79fef03 | 974 | * created 'inline'. |
kevman | 0:38ceb79fef03 | 975 | * |
kevman | 0:38ceb79fef03 | 976 | * @relates Span |
kevman | 0:38ceb79fef03 | 977 | */ |
kevman | 0:38ceb79fef03 | 978 | template<size_t Extent, typename T> |
kevman | 0:38ceb79fef03 | 979 | Span<const T, Extent> make_const_Span(const T *elements) |
kevman | 0:38ceb79fef03 | 980 | { |
kevman | 0:38ceb79fef03 | 981 | return Span<const T, Extent>(elements, Extent); |
kevman | 0:38ceb79fef03 | 982 | } |
kevman | 0:38ceb79fef03 | 983 | |
kevman | 0:38ceb79fef03 | 984 | /** |
kevman | 0:38ceb79fef03 | 985 | * Generate a Span to a const content from a C/C++ pointer and the size of the |
kevman | 0:38ceb79fef03 | 986 | * array. |
kevman | 0:38ceb79fef03 | 987 | * |
kevman | 0:38ceb79fef03 | 988 | * @tparam T Type of elements held in array_ptr. |
kevman | 0:38ceb79fef03 | 989 | * |
kevman | 0:38ceb79fef03 | 990 | * @param array_ptr The pointer to the array to viewed. |
kevman | 0:38ceb79fef03 | 991 | * @param array_size The number of T elements in the array. |
kevman | 0:38ceb79fef03 | 992 | * |
kevman | 0:38ceb79fef03 | 993 | * @return The Span to array_ptr with a size of array_size. |
kevman | 0:38ceb79fef03 | 994 | * |
kevman | 0:38ceb79fef03 | 995 | * @note This helper avoids the typing of template parameter when Span is |
kevman | 0:38ceb79fef03 | 996 | * created 'inline'. |
kevman | 0:38ceb79fef03 | 997 | * |
kevman | 0:38ceb79fef03 | 998 | * @relates Span |
kevman | 0:38ceb79fef03 | 999 | */ |
kevman | 0:38ceb79fef03 | 1000 | template<typename T> |
kevman | 0:38ceb79fef03 | 1001 | Span<const T> make_const_Span(T *array_ptr, size_t array_size) |
kevman | 0:38ceb79fef03 | 1002 | { |
kevman | 0:38ceb79fef03 | 1003 | return Span<const T>(array_ptr, array_size); |
kevman | 0:38ceb79fef03 | 1004 | } |
kevman | 0:38ceb79fef03 | 1005 | |
kevman | 0:38ceb79fef03 | 1006 | /**@}*/ |
kevman | 0:38ceb79fef03 | 1007 | |
kevman | 0:38ceb79fef03 | 1008 | /**@}*/ |
kevman | 0:38ceb79fef03 | 1009 | |
kevman | 0:38ceb79fef03 | 1010 | } // namespace mbed |
kevman | 0:38ceb79fef03 | 1011 | |
kevman | 0:38ceb79fef03 | 1012 | #endif /* MBED_PLATFORM_SPAN_H_ */ |