Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (c) 2015 Microsoft Corporation. All rights reserved. |
| 4 | // |
| 5 | // This code is licensed under the MIT License (MIT). |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 13 | // THE SOFTWARE. |
| 14 | // |
| 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #ifndef GSL_SPAN_H |
| 20 | #define GSL_SPAN_H |
| 21 | |
| 22 | #include "gsl_assert.h" |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 23 | #include "gsl_byte.h" |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 24 | #include "gsl_util.h" |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 25 | #include <array> |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 26 | #include <iterator> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 27 | #include <limits> |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 28 | #include <stdexcept> |
| 29 | #include <type_traits> |
| 30 | #include <utility> |
| 31 | |
| 32 | #ifdef _MSC_VER |
| 33 | |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 34 | #pragma warning(push) |
Neil MacIntosh | a9f0ce2 | 2016-03-31 12:01:07 -0700 | [diff] [blame] | 35 | |
| 36 | // turn off some warnings that are noisy about our Expects statements |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 37 | #pragma warning(disable : 4127) // conditional expression is constant |
| 38 | |
Neil MacIntosh | a9f0ce2 | 2016-03-31 12:01:07 -0700 | [diff] [blame] | 39 | // blanket turn off warnings from CppCoreCheck for now |
| 40 | // so people aren't annoyed by them when running the tool. |
| 41 | // more targeted suppressions will be added in a future update to the GSL |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 42 | #pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495) |
Neil MacIntosh | a9f0ce2 | 2016-03-31 12:01:07 -0700 | [diff] [blame] | 43 | |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 44 | // No MSVC does constexpr fully yet |
| 45 | #pragma push_macro("constexpr") |
| 46 | #define constexpr |
| 47 | |
| 48 | // VS 2013 workarounds |
| 49 | #if _MSC_VER <= 1800 |
| 50 | |
| 51 | #define GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
| 52 | #define GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT |
| 53 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 54 | // noexcept is not understood |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 55 | #ifndef GSL_THROW_ON_CONTRACT_VIOLATION |
| 56 | #pragma push_macro("noexcept") |
| 57 | #define noexcept /* nothing */ |
| 58 | #endif |
| 59 | |
| 60 | // turn off some misguided warnings |
| 61 | #pragma warning(push) |
| 62 | #pragma warning(disable : 4351) // warns about newly introduced aggregate initializer behavior |
| 63 | #pragma warning(disable : 4512) // warns that assignment op could not be generated |
| 64 | |
| 65 | #endif // _MSC_VER <= 1800 |
| 66 | |
| 67 | #endif // _MSC_VER |
| 68 | |
| 69 | #ifdef GSL_THROW_ON_CONTRACT_VIOLATION |
| 70 | |
| 71 | #ifdef _MSC_VER |
| 72 | #pragma push_macro("noexcept") |
| 73 | #endif |
| 74 | |
| 75 | #define noexcept /* nothing */ |
| 76 | |
| 77 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 78 | |
| 79 | namespace gsl |
| 80 | { |
| 81 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 82 | // [views.constants], constants |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 83 | constexpr const std::ptrdiff_t dynamic_extent = -1; |
| 84 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 85 | template <class ElementType, std::ptrdiff_t Extent = dynamic_extent> |
| 86 | class span; |
| 87 | |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 88 | // implementation details |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 89 | namespace details |
| 90 | { |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 91 | template <class T> |
| 92 | struct is_span_oracle : std::false_type |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 93 | { |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | template <class ElementType, std::ptrdiff_t Extent> |
| 97 | struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type |
| 98 | { |
| 99 | }; |
| 100 | |
| 101 | template <class T> |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 102 | struct is_span : public is_span_oracle<std::remove_cv_t<T>> |
| 103 | { |
| 104 | }; |
| 105 | |
| 106 | template <class T> |
| 107 | struct is_std_array_oracle : std::false_type |
| 108 | { |
| 109 | }; |
| 110 | |
| 111 | template <class ElementType, size_t Extent> |
| 112 | struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type |
| 113 | { |
| 114 | }; |
| 115 | |
| 116 | template <class T> |
| 117 | struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 118 | { |
| 119 | }; |
| 120 | |
| 121 | template <class From, class To> |
| 122 | struct is_allowed_pointer_conversion |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 123 | : public std::integral_constant<bool, std::is_pointer<From>::value && std::is_pointer<To>::value && |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 124 | std::is_convertible<From, To>::value> |
| 125 | { |
| 126 | }; |
| 127 | |
| 128 | template <class From, class To> |
| 129 | struct is_allowed_integral_conversion |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 130 | : public std::integral_constant<bool, std::is_integral<From>::value && std::is_integral<To>::value && |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 131 | sizeof(From) == sizeof(To) && alignof(From) == alignof(To) && |
| 132 | std::is_convertible<From, To>::value> |
| 133 | { |
| 134 | }; |
| 135 | |
| 136 | template <std::ptrdiff_t From, std::ptrdiff_t To> |
| 137 | struct is_allowed_extent_conversion |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 138 | : public std::integral_constant<bool, From == To || From == gsl::dynamic_extent || To == gsl::dynamic_extent> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 139 | { |
| 140 | }; |
| 141 | |
| 142 | template <class From, class To> |
| 143 | struct is_allowed_element_type_conversion |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 144 | : public std::integral_constant<bool, std::is_same<From, std::remove_cv_t<To>>::value || |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 145 | is_allowed_pointer_conversion<From, To>::value || |
| 146 | is_allowed_integral_conversion<From, To>::value> |
| 147 | { |
| 148 | }; |
| 149 | |
| 150 | template <class From> |
| 151 | struct is_allowed_element_type_conversion<From, byte> |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 152 | : public std::integral_constant<bool, !std::is_const<From>::value> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 153 | { |
| 154 | }; |
| 155 | |
| 156 | template <class From> |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 157 | struct is_allowed_element_type_conversion<From, const byte> : public std::true_type |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 158 | { |
| 159 | }; |
| 160 | |
| 161 | template <class Span> |
| 162 | class const_span_iterator |
| 163 | { |
| 164 | public: |
| 165 | using iterator_category = std::random_access_iterator_tag; |
| 166 | using value_type = typename Span::element_type; |
| 167 | using difference_type = std::ptrdiff_t; |
| 168 | |
| 169 | using const_pointer = std::add_const_t<value_type*>; |
| 170 | using pointer = const_pointer; |
| 171 | |
| 172 | using const_reference = std::add_const_t<value_type&>; |
| 173 | using reference = const_reference; |
| 174 | |
| 175 | constexpr const_span_iterator() : const_span_iterator(nullptr, 0) {} |
| 176 | constexpr const_span_iterator(const Span* span, typename Span::index_type index) |
| 177 | : span_(span), index_(index) |
| 178 | { |
| 179 | Expects(span == nullptr || (index_ >= 0 && index <= span_->length())); |
| 180 | } |
| 181 | |
| 182 | constexpr reference operator*() const |
| 183 | { |
| 184 | Expects(span_); |
| 185 | return (*span_)[index_]; |
| 186 | } |
| 187 | constexpr pointer operator->() const |
| 188 | { |
| 189 | Expects(span_); |
| 190 | return &((*span_)[index_]); |
| 191 | } |
| 192 | |
| 193 | constexpr const_span_iterator& operator++() noexcept |
| 194 | { |
| 195 | Expects(span_ && index_ >= 0 && index_ < span_->length()); |
| 196 | ++index_; |
| 197 | return *this; |
| 198 | } |
| 199 | |
| 200 | constexpr const_span_iterator operator++(int) noexcept |
| 201 | { |
| 202 | auto ret = *this; |
| 203 | ++(*this); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | constexpr const_span_iterator& operator--() noexcept |
| 208 | { |
| 209 | Expects(span_ && index_ > 0 && index_ <= span_->length()); |
| 210 | --index_; |
| 211 | return *this; |
| 212 | } |
| 213 | |
| 214 | constexpr const_span_iterator operator--(int) noexcept |
| 215 | { |
| 216 | auto ret = *this; |
| 217 | --(*this); |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | constexpr const_span_iterator operator+(difference_type n) const noexcept |
| 222 | { |
Neil MacIntosh | 4de3d4e | 2016-07-26 18:44:13 -0700 | [diff] [blame^] | 223 | auto ret = *this; |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 224 | return ret += n; |
| 225 | } |
| 226 | |
| 227 | constexpr const_span_iterator& operator+=(difference_type n) noexcept |
| 228 | { |
| 229 | Expects(span_ && (index_ + n) >= 0 && (index_ + n) <= span_->length()); |
| 230 | index_ += n; |
| 231 | return *this; |
| 232 | } |
| 233 | |
| 234 | constexpr const_span_iterator operator-(difference_type n) const noexcept |
| 235 | { |
Neil MacIntosh | 4de3d4e | 2016-07-26 18:44:13 -0700 | [diff] [blame^] | 236 | auto ret = *this; |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 237 | return ret -= n; |
| 238 | } |
| 239 | |
| 240 | constexpr const_span_iterator& operator-=(difference_type n) noexcept |
| 241 | { |
| 242 | return *this += -n; |
| 243 | } |
| 244 | |
| 245 | constexpr difference_type operator-(const const_span_iterator& rhs) const noexcept |
| 246 | { |
| 247 | Expects(span_ == rhs.span_); |
| 248 | return index_ - rhs.index_; |
| 249 | } |
| 250 | |
| 251 | constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); } |
| 252 | |
| 253 | constexpr bool operator==(const const_span_iterator& rhs) const noexcept |
| 254 | { |
| 255 | return span_ == rhs.span_ && index_ == rhs.index_; |
| 256 | } |
| 257 | |
| 258 | constexpr bool operator!=(const const_span_iterator& rhs) const noexcept |
| 259 | { |
| 260 | return !(*this == rhs); |
| 261 | } |
| 262 | |
| 263 | constexpr bool operator<(const const_span_iterator& rhs) const noexcept |
| 264 | { |
| 265 | Expects(span_ == rhs.span_); |
| 266 | return index_ < rhs.index_; |
| 267 | } |
| 268 | |
| 269 | constexpr bool operator<=(const const_span_iterator& rhs) const noexcept |
| 270 | { |
| 271 | return !(rhs < *this); |
| 272 | } |
| 273 | |
| 274 | constexpr bool operator>(const const_span_iterator& rhs) const noexcept |
| 275 | { |
| 276 | return rhs < *this; |
| 277 | } |
| 278 | |
| 279 | constexpr bool operator>=(const const_span_iterator& rhs) const noexcept |
| 280 | { |
| 281 | return !(rhs > *this); |
| 282 | } |
| 283 | |
| 284 | void swap(const_span_iterator& rhs) noexcept |
| 285 | { |
| 286 | std::swap(index_, rhs.index_); |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 287 | std::swap(span_, rhs.span_); |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | private: |
| 291 | const Span* span_; |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 292 | std::ptrdiff_t index_; |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | template <class Span> |
| 296 | class span_iterator : public const_span_iterator<Span> |
| 297 | { |
| 298 | using base_type = const_span_iterator<Span>; |
| 299 | |
| 300 | public: |
| 301 | using iterator_category = std::random_access_iterator_tag; |
| 302 | using value_type = typename Span::element_type; |
| 303 | using difference_type = std::ptrdiff_t; |
| 304 | |
| 305 | using pointer = value_type*; |
| 306 | using reference = value_type&; |
| 307 | |
| 308 | constexpr span_iterator() : base_type() {} |
| 309 | constexpr span_iterator(const Span* span, typename Span::index_type index) |
| 310 | : base_type(span, index) |
| 311 | { |
| 312 | } |
| 313 | |
| 314 | constexpr reference operator*() const |
| 315 | { |
| 316 | return const_cast<reference>(base_type::operator*()); |
| 317 | } |
| 318 | constexpr pointer operator->() const |
| 319 | { |
| 320 | return const_cast<pointer>(base_type::operator->()); |
| 321 | } |
| 322 | |
| 323 | constexpr span_iterator& operator++() noexcept |
| 324 | { |
| 325 | base_type::operator++(); |
| 326 | return *this; |
| 327 | } |
| 328 | |
| 329 | constexpr span_iterator operator++(int) noexcept { return base_type::operator++(1); } |
| 330 | |
| 331 | constexpr span_iterator& operator--() noexcept |
| 332 | { |
| 333 | base_type::operator--(); |
| 334 | return *this; |
| 335 | } |
| 336 | |
| 337 | constexpr span_iterator operator--(int) noexcept { return base_type::operator--(1); } |
| 338 | |
| 339 | constexpr span_iterator operator+(difference_type n) const noexcept |
| 340 | { |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 341 | return {base_type::operator+(n)}; |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | constexpr span_iterator& operator+=(difference_type n) noexcept |
| 345 | { |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 346 | return {base_type::operator+=(n)}; |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | constexpr span_iterator operator-(difference_type n) const noexcept |
| 350 | { |
| 351 | return base_type::operator-(n); |
| 352 | } |
| 353 | |
| 354 | constexpr span_iterator& operator-=(difference_type n) noexcept |
| 355 | { |
| 356 | return base_type::operator-=(n); |
| 357 | } |
| 358 | |
| 359 | constexpr difference_type operator-(const span_iterator& rhs) const noexcept |
| 360 | { |
| 361 | return base_type::operator-(rhs); |
| 362 | } |
| 363 | |
| 364 | constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); } |
| 365 | |
| 366 | constexpr bool operator==(const span_iterator& rhs) const noexcept |
| 367 | { |
| 368 | return base_type::operator==(rhs); |
| 369 | } |
| 370 | |
| 371 | constexpr bool operator!=(const span_iterator& rhs) const noexcept |
| 372 | { |
| 373 | return !(*this == rhs); |
| 374 | } |
| 375 | |
| 376 | constexpr bool operator<(const span_iterator& rhs) const noexcept |
| 377 | { |
| 378 | return base_type::operator<(rhs); |
| 379 | } |
| 380 | |
| 381 | constexpr bool operator<=(const span_iterator& rhs) const noexcept |
| 382 | { |
| 383 | return !(rhs < *this); |
| 384 | } |
| 385 | |
| 386 | constexpr bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; } |
| 387 | |
| 388 | constexpr bool operator>=(const span_iterator& rhs) const noexcept |
| 389 | { |
| 390 | return !(rhs > *this); |
| 391 | } |
| 392 | |
| 393 | void swap(span_iterator& rhs) noexcept { base_type::swap(rhs); } |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 394 | private: |
| 395 | constexpr span_iterator(const base_type& base) : base_type(base) |
| 396 | { |
| 397 | } |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 398 | }; |
| 399 | |
| 400 | template <typename Span> |
| 401 | constexpr const_span_iterator<Span> |
| 402 | operator+(typename const_span_iterator<Span>::difference_type n, |
| 403 | const const_span_iterator<Span>& rhs) noexcept |
| 404 | { |
| 405 | return rhs + n; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 408 | template <typename Span> |
| 409 | constexpr const_span_iterator<Span> |
| 410 | operator-(typename const_span_iterator<Span>::difference_type n, |
| 411 | const const_span_iterator<Span>& rhs) noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 412 | { |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 413 | return rhs - n; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 416 | template <typename Span> |
| 417 | constexpr span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n, |
| 418 | const span_iterator<Span>& rhs) noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 419 | { |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 420 | return rhs + n; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 423 | template <typename Span> |
| 424 | constexpr span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n, |
| 425 | const span_iterator<Span>& rhs) noexcept |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 426 | { |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 427 | return rhs - n; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 430 | template <std::ptrdiff_t Ext> |
| 431 | class extent_type |
| 432 | { |
| 433 | public: |
| 434 | using index_type = std::ptrdiff_t; |
| 435 | |
| 436 | static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size."); |
| 437 | |
| 438 | constexpr extent_type() noexcept {} |
| 439 | |
| 440 | template <index_type Other> |
| 441 | constexpr extent_type(extent_type<Other> ext) noexcept |
| 442 | { |
| 443 | static_assert(Other == Ext || Other == dynamic_extent, |
| 444 | "Mismatch between fixed-size extent and size of initializing data."); |
| 445 | Expects(ext.size() == Ext); |
| 446 | } |
| 447 | |
| 448 | constexpr extent_type(index_type size) { Expects(size == Ext); } |
| 449 | |
| 450 | constexpr inline index_type size() const noexcept { return Ext; } |
| 451 | }; |
| 452 | |
| 453 | template <> |
| 454 | class extent_type<dynamic_extent> |
| 455 | { |
| 456 | public: |
| 457 | using index_type = std::ptrdiff_t; |
| 458 | |
| 459 | template <index_type Other> |
| 460 | explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) |
| 461 | { |
| 462 | } |
| 463 | |
| 464 | explicit constexpr extent_type(index_type size) : size_(size) { Expects(size >= 0); } |
| 465 | |
| 466 | constexpr inline index_type size() const noexcept { return size_; } |
| 467 | |
| 468 | private: |
| 469 | index_type size_; |
| 470 | }; |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 471 | } // namespace details |
| 472 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 473 | // [span], class template span |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 474 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 475 | class span |
| 476 | { |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 477 | public: |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 478 | // constants and types |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 479 | using element_type = ElementType; |
| 480 | using index_type = std::ptrdiff_t; |
| 481 | using pointer = element_type*; |
| 482 | using reference = element_type&; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 483 | |
| 484 | using iterator = details::span_iterator<span<ElementType, Extent>>; |
Neil MacIntosh | 30a038c | 2016-07-18 11:38:01 -0700 | [diff] [blame] | 485 | using const_iterator = details::const_span_iterator<span>; |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 486 | using reverse_iterator = std::reverse_iterator<iterator>; |
Neil MacIntosh | 2674724 | 2016-06-26 17:00:56 +0300 | [diff] [blame] | 487 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 488 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 489 | constexpr static const index_type extent = Extent; |
| 490 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 491 | // [span.cons], span constructors, copy, assignment, and destructor |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 492 | constexpr span() noexcept : storage_(nullptr, details::extent_type<0>()) {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 493 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 494 | constexpr span(std::nullptr_t) noexcept : span() {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 495 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 496 | constexpr span(pointer ptr, index_type count) : storage_(ptr, count) {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 497 | |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 498 | constexpr span(pointer firstElem, pointer lastElem) |
| 499 | : storage_(firstElem, std::distance(firstElem, lastElem)) |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 500 | { |
| 501 | } |
Neil MacIntosh | f61a9bb | 2016-02-29 13:16:48 -0800 | [diff] [blame] | 502 | |
| 503 | template <size_t N> |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 504 | constexpr span(element_type (&arr)[N]) noexcept : storage_(&arr[0], details::extent_type<N>()) |
| 505 | { |
| 506 | } |
| 507 | |
| 508 | template <size_t N, class ArrayElementType = std::remove_const_t<element_type>> |
| 509 | constexpr span(std::array<ArrayElementType, N>& arr) noexcept |
| 510 | : storage_(&arr[0], details::extent_type<N>()) |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 511 | { |
| 512 | } |
| 513 | |
| 514 | template <size_t N> |
Neil MacIntosh | f2ab3a5 | 2016-07-20 09:24:49 -0700 | [diff] [blame] | 515 | constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 516 | : storage_(&arr[0], details::extent_type<N>()) |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 517 | { |
| 518 | } |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 519 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 520 | // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement |
| 521 | // on Container to be a contiguous sequence container. |
| 522 | template <class Container, |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 523 | class = std::enable_if_t< |
| 524 | !details::is_span<Container>::value && |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 525 | !details::is_std_array<Container>::value && |
| 526 | std::is_convertible<typename Container::pointer, pointer>::value && |
| 527 | std::is_convertible<typename Container::pointer, |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 528 | decltype(std::declval<Container>().data())>::value>> |
| 529 | constexpr span(Container& cont) : span(cont.data(), cont.size()) |
| 530 | { |
| 531 | } |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 532 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 533 | template <class Container, |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 534 | class = std::enable_if_t< |
| 535 | std::is_const<element_type>::value && !details::is_span<Container>::value && |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 536 | std::is_convertible<typename Container::pointer, pointer>::value && |
| 537 | std::is_convertible<typename Container::pointer, |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 538 | decltype(std::declval<Container>().data())>::value>> |
| 539 | constexpr span(const Container& cont) : span(cont.data(), cont.size()) |
| 540 | { |
| 541 | } |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame] | 542 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 543 | constexpr span(const span& other) noexcept = default; |
| 544 | constexpr span(span&& other) noexcept = default; |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 545 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 546 | template < |
| 547 | class OtherElementType, std::ptrdiff_t OtherExtent, |
Neil MacIntosh | c94a66f | 2016-06-12 18:28:19 -0700 | [diff] [blame] | 548 | class = std::enable_if_t< |
| 549 | details::is_allowed_extent_conversion<OtherExtent, Extent>::value && |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 550 | details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>> |
| 551 | constexpr span(const span<OtherElementType, OtherExtent>& other) |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 552 | : storage_(reinterpret_cast<pointer>(other.data()), details::extent_type<OtherExtent>(other.size())) |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 553 | { |
| 554 | } |
| 555 | |
| 556 | template < |
| 557 | class OtherElementType, std::ptrdiff_t OtherExtent, |
| 558 | class = std::enable_if_t< |
| 559 | details::is_allowed_extent_conversion<OtherExtent, Extent>::value && |
| 560 | details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>> |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 561 | constexpr span(span<OtherElementType, OtherExtent>&& other) |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 562 | : storage_(reinterpret_cast<pointer>(other.data()), details::extent_type<OtherExtent>(other.size())) |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 563 | { |
| 564 | } |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 565 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 566 | ~span() noexcept = default; |
| 567 | constexpr span& operator=(const span& other) noexcept = default; |
| 568 | constexpr span& operator=(span&& other) noexcept = default; |
| 569 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 570 | // [span.sub], span subviews |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 571 | template <std::ptrdiff_t Count> |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 572 | constexpr span<element_type, Count> first() const |
| 573 | { |
| 574 | Expects(Count >= 0 && Count <= size()); |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 575 | return {data(), Count}; |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 578 | template <std::ptrdiff_t Count> |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 579 | constexpr span<element_type, Count> last() const |
| 580 | { |
| 581 | Expects(Count >= 0 && Count <= size()); |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 582 | return {data() + (size() - Count), Count}; |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 585 | template <std::ptrdiff_t Offset, std::ptrdiff_t Count = dynamic_extent> |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 586 | constexpr span<element_type, Count> subspan() const |
| 587 | { |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 588 | Expects((Offset == 0 || (Offset > 0 && Offset <= size())) && |
| 589 | (Count == dynamic_extent || (Count >= 0 && Offset + Count <= size()))); |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 590 | return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count}; |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | constexpr span<element_type, dynamic_extent> first(index_type count) const |
| 594 | { |
| 595 | Expects(count >= 0 && count <= size()); |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 596 | return {data(), count}; |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | constexpr span<element_type, dynamic_extent> last(index_type count) const |
| 600 | { |
| 601 | Expects(count >= 0 && count <= size()); |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 602 | return {data() + (size() - count), count}; |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | constexpr span<element_type, dynamic_extent> subspan(index_type offset, |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 606 | index_type count = dynamic_extent) const |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 607 | { |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 608 | Expects((offset == 0 || (offset > 0 && offset <= size())) && |
| 609 | (count == dynamic_extent || (count >= 0 && offset + count <= size()))); |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 610 | return {data() + offset, count == dynamic_extent ? size() - offset : count}; |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 613 | // [span.obs], span observers |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 614 | constexpr index_type length() const noexcept { return size(); } |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 615 | constexpr index_type size() const noexcept { return storage_.size(); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 616 | constexpr index_type length_bytes() const noexcept { return size_bytes(); } |
| 617 | constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); } |
| 618 | constexpr bool empty() const noexcept { return size() == 0; } |
| 619 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 620 | // [span.elem], span element access |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 621 | constexpr reference operator[](index_type idx) const |
| 622 | { |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 623 | Expects(idx >= 0 && idx < storage_.size()); |
Neil MacIntosh | f2ab3a5 | 2016-07-20 09:24:49 -0700 | [diff] [blame] | 624 | return data()[idx]; |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 625 | } |
| 626 | constexpr reference operator()(index_type idx) const { return this->operator[](idx); } |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 627 | constexpr pointer data() const noexcept { return storage_.data(); } |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 628 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 629 | // [span.iter], span iterator support |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 630 | iterator begin() const noexcept { return {this, 0}; } |
| 631 | iterator end() const noexcept { return {this, length()}; } |
Neil MacIntosh | 30a038c | 2016-07-18 11:38:01 -0700 | [diff] [blame] | 632 | |
| 633 | const_iterator cbegin() const noexcept { return {this, 0}; } |
| 634 | const_iterator cend() const noexcept { return {this, length()}; } |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 635 | |
Neil MacIntosh | 30a038c | 2016-07-18 11:38:01 -0700 | [diff] [blame] | 636 | reverse_iterator rbegin() const noexcept { return reverse_iterator{{this, length()}}; } |
| 637 | reverse_iterator rend() const noexcept { return reverse_iterator{{this, 0}}; } |
| 638 | |
| 639 | const_reverse_iterator crbegin() const noexcept { return reverse_iterator{{this, length()}}; } |
| 640 | const_reverse_iterator crend() const noexcept { return reverse_iterator{{this, 0}}; } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 641 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 642 | private: |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 643 | // this implementation detail class lets us take advantage of the |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 644 | // empty base class optimization to pay for only storage of a single |
| 645 | // pointer in the case of fixed-size spans |
| 646 | template <class ExtentType> |
| 647 | class storage_type : public ExtentType |
| 648 | { |
| 649 | public: |
| 650 | template <class OtherExtentType> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 651 | constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data) |
| 652 | { |
Neil MacIntosh | 4de3d4e | 2016-07-26 18:44:13 -0700 | [diff] [blame^] | 653 | Expects((!data && ExtentType::size() == 0) || (data && ExtentType::size() >= 0)); |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 654 | } |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 655 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 656 | constexpr inline pointer data() const noexcept { return data_; } |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 657 | |
| 658 | private: |
| 659 | pointer data_; |
| 660 | }; |
| 661 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 662 | storage_type<details::extent_type<Extent>> storage_; |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 663 | }; |
| 664 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 665 | // [span.comparison], span comparison operators |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 666 | template <class ElementType, std::ptrdiff_t FirstExtent, std::ptrdiff_t SecondExtent> |
| 667 | constexpr bool operator==(const span<ElementType, FirstExtent>& l, const span<ElementType, SecondExtent>& r) |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 668 | { |
| 669 | return std::equal(l.begin(), l.end(), r.begin(), r.end()); |
| 670 | } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 671 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 672 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 673 | constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
| 674 | { |
| 675 | return !(l == r); |
| 676 | } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 677 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 678 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 679 | constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
| 680 | { |
| 681 | return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); |
| 682 | } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 683 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 684 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 685 | constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
| 686 | { |
| 687 | return !(l > r); |
| 688 | } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 689 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 690 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 691 | constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
| 692 | { |
| 693 | return r < l; |
| 694 | } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 695 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 696 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 697 | constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) |
| 698 | { |
| 699 | return !(l < r); |
| 700 | } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 701 | |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame] | 702 | namespace details |
| 703 | { |
| 704 | // if we only supported compilers with good constexpr support then |
| 705 | // this pair of classes could collapse down to a constexpr function |
| 706 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 707 | // we should use a narrow_cast<> to go to size_t, but older compilers may not see it as |
| 708 | // constexpr |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame] | 709 | // and so will fail compilation of the template |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 710 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 711 | struct calculate_byte_size |
| 712 | : std::integral_constant<std::ptrdiff_t, |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 713 | static_cast<std::ptrdiff_t>(sizeof(ElementType) * |
| 714 | static_cast<std::size_t>(Extent))> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 715 | { |
| 716 | }; |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame] | 717 | |
| 718 | template <class ElementType> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 719 | struct calculate_byte_size<ElementType, dynamic_extent> |
| 720 | : std::integral_constant<std::ptrdiff_t, dynamic_extent> |
| 721 | { |
| 722 | }; |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 725 | // [span.objectrep], views of object representation |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 726 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 727 | span<const byte, details::calculate_byte_size<ElementType, Extent>::value> |
| 728 | as_bytes(span<ElementType, Extent> s) noexcept |
| 729 | { |
| 730 | return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; |
| 731 | } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 732 | |
Neil MacIntosh | c366f44 | 2016-07-26 18:34:27 -0700 | [diff] [blame] | 733 | template <class ElementType, std::ptrdiff_t Extent, |
Neil MacIntosh | b03b04b | 2016-07-20 13:17:47 -0700 | [diff] [blame] | 734 | class = std::enable_if_t<!std::is_const<ElementType>::value>> |
| 735 | span<byte, details::calculate_byte_size<ElementType, Extent>::value> |
| 736 | as_writeable_bytes(span<ElementType, Extent> s) noexcept |
| 737 | { |
| 738 | return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; |
| 739 | } |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 740 | |
| 741 | } // namespace gsl |
| 742 | |
| 743 | #ifdef _MSC_VER |
| 744 | |
| 745 | #undef constexpr |
| 746 | #pragma pop_macro("constexpr") |
| 747 | |
| 748 | #if _MSC_VER <= 1800 |
| 749 | #pragma warning(pop) |
| 750 | |
| 751 | #ifndef GSL_THROW_ON_CONTRACT_VIOLATION |
| 752 | #undef noexcept |
| 753 | #pragma pop_macro("noexcept") |
| 754 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 755 | |
| 756 | #undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
| 757 | |
| 758 | #endif // _MSC_VER <= 1800 |
| 759 | |
| 760 | #endif // _MSC_VER |
| 761 | |
| 762 | #if defined(GSL_THROW_ON_CONTRACT_VIOLATION) |
| 763 | |
| 764 | #undef noexcept |
| 765 | |
| 766 | #ifdef _MSC_VER |
| 767 | #pragma warning(pop) |
| 768 | #pragma pop_macro("noexcept") |
| 769 | #endif |
| 770 | |
| 771 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 772 | |
| 773 | #endif // GSL_SPAN_H |