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