Brian Silverman | a6f7ce0 | 2018-07-07 15:04:00 -0700 | [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 | #ifndef GSL_SPAN_H |
| 18 | #define GSL_SPAN_H |
| 19 | |
| 20 | #include <gsl/gsl_assert> // for Expects |
| 21 | #include <gsl/gsl_byte> // for byte |
| 22 | #include <gsl/gsl_util> // for narrow_cast, narrow |
| 23 | |
| 24 | #include <algorithm> // for lexicographical_compare |
| 25 | #include <array> // for array |
| 26 | #include <cstddef> // for ptrdiff_t, size_t, nullptr_t |
| 27 | #include <iterator> // for reverse_iterator, distance, random_access_... |
| 28 | #include <limits> |
| 29 | #include <stdexcept> |
| 30 | #include <type_traits> // for enable_if_t, declval, is_convertible, inte... |
| 31 | #include <utility> |
| 32 | |
| 33 | #ifdef _MSC_VER |
| 34 | #pragma warning(push) |
| 35 | |
| 36 | // turn off some warnings that are noisy about our Expects statements |
| 37 | #pragma warning(disable : 4127) // conditional expression is constant |
| 38 | #pragma warning(disable : 4702) // unreachable code |
| 39 | |
| 40 | // blanket turn off warnings from CppCoreCheck for now |
| 41 | // so people aren't annoyed by them when running the tool. |
| 42 | // more targeted suppressions will be added in a future update to the GSL |
| 43 | #pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495) |
| 44 | |
| 45 | #if _MSC_VER < 1910 |
| 46 | #pragma push_macro("constexpr") |
| 47 | #define constexpr /*constexpr*/ |
| 48 | #define GSL_USE_STATIC_CONSTEXPR_WORKAROUND |
| 49 | |
| 50 | #endif // _MSC_VER < 1910 |
| 51 | #else // _MSC_VER |
| 52 | |
| 53 | // See if we have enough C++17 power to use a static constexpr data member |
| 54 | // without needing an out-of-line definition |
| 55 | #if !(defined(__cplusplus) && (__cplusplus >= 201703L)) |
| 56 | #define GSL_USE_STATIC_CONSTEXPR_WORKAROUND |
| 57 | #endif // !(defined(__cplusplus) && (__cplusplus >= 201703L)) |
| 58 | |
| 59 | #endif // _MSC_VER |
| 60 | |
| 61 | // GCC 7 does not like the signed unsigned missmatch (size_t ptrdiff_t) |
| 62 | // While there is a conversion from signed to unsigned, it happens at |
| 63 | // compiletime, so the compiler wouldn't have to warn indiscriminently, but |
| 64 | // could check if the source value actually doesn't fit into the target type |
| 65 | // and only warn in those cases. |
| 66 | #if __GNUC__ > 6 |
| 67 | #pragma GCC diagnostic push |
| 68 | #pragma GCC diagnostic ignored "-Wsign-conversion" |
| 69 | #endif |
| 70 | |
| 71 | namespace gsl |
| 72 | { |
| 73 | |
| 74 | // [views.constants], constants |
| 75 | constexpr const std::ptrdiff_t dynamic_extent = -1; |
| 76 | |
| 77 | template <class ElementType, std::ptrdiff_t Extent = dynamic_extent> |
| 78 | class span; |
| 79 | |
| 80 | // implementation details |
| 81 | namespace details |
| 82 | { |
| 83 | template <class T> |
| 84 | struct is_span_oracle : std::false_type |
| 85 | { |
| 86 | }; |
| 87 | |
| 88 | template <class ElementType, std::ptrdiff_t Extent> |
| 89 | struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type |
| 90 | { |
| 91 | }; |
| 92 | |
| 93 | template <class T> |
| 94 | struct is_span : public is_span_oracle<std::remove_cv_t<T>> |
| 95 | { |
| 96 | }; |
| 97 | |
| 98 | template <class T> |
| 99 | struct is_std_array_oracle : std::false_type |
| 100 | { |
| 101 | }; |
| 102 | |
| 103 | template <class ElementType, std::size_t Extent> |
| 104 | struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type |
| 105 | { |
| 106 | }; |
| 107 | |
| 108 | template <class T> |
| 109 | struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>> |
| 110 | { |
| 111 | }; |
| 112 | |
| 113 | template <std::ptrdiff_t From, std::ptrdiff_t To> |
| 114 | struct is_allowed_extent_conversion |
| 115 | : public std::integral_constant<bool, From == To || From == gsl::dynamic_extent || |
| 116 | To == gsl::dynamic_extent> |
| 117 | { |
| 118 | }; |
| 119 | |
| 120 | template <class From, class To> |
| 121 | struct is_allowed_element_type_conversion |
| 122 | : public std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value> |
| 123 | { |
| 124 | }; |
| 125 | |
| 126 | template <class Span, bool IsConst> |
| 127 | class span_iterator |
| 128 | { |
| 129 | using element_type_ = typename Span::element_type; |
| 130 | |
| 131 | public: |
| 132 | |
| 133 | #ifdef _MSC_VER |
| 134 | // Tell Microsoft standard library that span_iterators are checked. |
| 135 | using _Unchecked_type = typename Span::pointer; |
| 136 | #endif |
| 137 | |
| 138 | using iterator_category = std::random_access_iterator_tag; |
| 139 | using value_type = std::remove_cv_t<element_type_>; |
| 140 | using difference_type = typename Span::index_type; |
| 141 | |
| 142 | using reference = std::conditional_t<IsConst, const element_type_, element_type_>&; |
| 143 | using pointer = std::add_pointer_t<reference>; |
| 144 | |
| 145 | span_iterator() = default; |
| 146 | |
| 147 | constexpr span_iterator(const Span* span, typename Span::index_type idx) noexcept |
| 148 | : span_(span), index_(idx) |
| 149 | {} |
| 150 | |
| 151 | friend span_iterator<Span, true>; |
| 152 | template<bool B, std::enable_if_t<!B && IsConst>* = nullptr> |
| 153 | constexpr span_iterator(const span_iterator<Span, B>& other) noexcept |
| 154 | : span_iterator(other.span_, other.index_) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | constexpr reference operator*() const |
| 159 | { |
| 160 | Expects(index_ != span_->size()); |
| 161 | return *(span_->data() + index_); |
| 162 | } |
| 163 | |
| 164 | constexpr pointer operator->() const |
| 165 | { |
| 166 | Expects(index_ != span_->size()); |
| 167 | return span_->data() + index_; |
| 168 | } |
| 169 | |
| 170 | constexpr span_iterator& operator++() |
| 171 | { |
| 172 | Expects(0 <= index_ && index_ != span_->size()); |
| 173 | ++index_; |
| 174 | return *this; |
| 175 | } |
| 176 | |
| 177 | constexpr span_iterator operator++(int) |
| 178 | { |
| 179 | auto ret = *this; |
| 180 | ++(*this); |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | constexpr span_iterator& operator--() |
| 185 | { |
| 186 | Expects(index_ != 0 && index_ <= span_->size()); |
| 187 | --index_; |
| 188 | return *this; |
| 189 | } |
| 190 | |
| 191 | constexpr span_iterator operator--(int) |
| 192 | { |
| 193 | auto ret = *this; |
| 194 | --(*this); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | constexpr span_iterator operator+(difference_type n) const |
| 199 | { |
| 200 | auto ret = *this; |
| 201 | return ret += n; |
| 202 | } |
| 203 | |
| 204 | friend constexpr span_iterator operator+(difference_type n, span_iterator const& rhs) |
| 205 | { |
| 206 | return rhs + n; |
| 207 | } |
| 208 | |
| 209 | constexpr span_iterator& operator+=(difference_type n) |
| 210 | { |
| 211 | Expects((index_ + n) >= 0 && (index_ + n) <= span_->size()); |
| 212 | index_ += n; |
| 213 | return *this; |
| 214 | } |
| 215 | |
| 216 | constexpr span_iterator operator-(difference_type n) const |
| 217 | { |
| 218 | auto ret = *this; |
| 219 | return ret -= n; |
| 220 | } |
| 221 | |
| 222 | constexpr span_iterator& operator-=(difference_type n) { return *this += -n; } |
| 223 | |
| 224 | constexpr difference_type operator-(span_iterator rhs) const |
| 225 | { |
| 226 | Expects(span_ == rhs.span_); |
| 227 | return index_ - rhs.index_; |
| 228 | } |
| 229 | |
| 230 | constexpr reference operator[](difference_type n) const |
| 231 | { |
| 232 | return *(*this + n); |
| 233 | } |
| 234 | |
| 235 | constexpr friend bool operator==(span_iterator lhs, |
| 236 | span_iterator rhs) noexcept |
| 237 | { |
| 238 | return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_; |
| 239 | } |
| 240 | |
| 241 | constexpr friend bool operator!=(span_iterator lhs, |
| 242 | span_iterator rhs) noexcept |
| 243 | { |
| 244 | return !(lhs == rhs); |
| 245 | } |
| 246 | |
| 247 | constexpr friend bool operator<(span_iterator lhs, |
| 248 | span_iterator rhs) noexcept |
| 249 | { |
| 250 | return lhs.index_ < rhs.index_; |
| 251 | } |
| 252 | |
| 253 | constexpr friend bool operator<=(span_iterator lhs, |
| 254 | span_iterator rhs) noexcept |
| 255 | { |
| 256 | return !(rhs < lhs); |
| 257 | } |
| 258 | |
| 259 | constexpr friend bool operator>(span_iterator lhs, |
| 260 | span_iterator rhs) noexcept |
| 261 | { |
| 262 | return rhs < lhs; |
| 263 | } |
| 264 | |
| 265 | constexpr friend bool operator>=(span_iterator lhs, |
| 266 | span_iterator rhs) noexcept |
| 267 | { |
| 268 | return !(rhs > lhs); |
| 269 | } |
| 270 | |
| 271 | #ifdef _MSC_VER |
| 272 | // MSVC++ iterator debugging support; allows STL algorithms in 15.8+ |
| 273 | // to unwrap span_iterator to a pointer type after a range check in STL |
| 274 | // algorithm calls |
| 275 | friend constexpr void _Verify_range(span_iterator lhs, |
| 276 | span_iterator rhs) noexcept |
| 277 | { // test that [lhs, rhs) forms a valid range inside an STL algorithm |
| 278 | Expects(lhs.span_ == rhs.span_ // range spans have to match |
| 279 | && lhs.index_ <= rhs.index_); // range must not be transposed |
| 280 | } |
| 281 | |
| 282 | constexpr void _Verify_offset(const difference_type n) const noexcept |
| 283 | { // test that the iterator *this + n is a valid range in an STL |
| 284 | // algorithm call |
| 285 | Expects((index_ + n) >= 0 && (index_ + n) <= span_->size()); |
| 286 | } |
| 287 | |
| 288 | constexpr pointer _Unwrapped() const noexcept |
| 289 | { // after seeking *this to a high water mark, or using one of the |
| 290 | // _Verify_xxx functions above, unwrap this span_iterator to a raw |
| 291 | // pointer |
| 292 | return span_->data() + index_; |
| 293 | } |
| 294 | |
| 295 | // Tell the STL that span_iterator should not be unwrapped if it can't |
| 296 | // validate in advance, even in release / optimized builds: |
| 297 | #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND) |
| 298 | static constexpr const bool _Unwrap_when_unverified = false; |
| 299 | #else |
| 300 | static constexpr bool _Unwrap_when_unverified = false; |
| 301 | #endif |
| 302 | constexpr void _Seek_to(const pointer p) noexcept |
| 303 | { // adjust the position of *this to previously verified location p |
| 304 | // after _Unwrapped |
| 305 | index_ = p - span_->data(); |
| 306 | } |
| 307 | #endif |
| 308 | |
| 309 | protected: |
| 310 | const Span* span_ = nullptr; |
| 311 | std::ptrdiff_t index_ = 0; |
| 312 | }; |
| 313 | |
| 314 | template <std::ptrdiff_t Ext> |
| 315 | class extent_type |
| 316 | { |
| 317 | public: |
| 318 | using index_type = std::ptrdiff_t; |
| 319 | |
| 320 | static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size."); |
| 321 | |
| 322 | constexpr extent_type() noexcept {} |
| 323 | |
| 324 | template <index_type Other> |
| 325 | constexpr extent_type(extent_type<Other> ext) |
| 326 | { |
| 327 | static_assert(Other == Ext || Other == dynamic_extent, |
| 328 | "Mismatch between fixed-size extent and size of initializing data."); |
| 329 | Expects(ext.size() == Ext); |
| 330 | } |
| 331 | |
| 332 | constexpr extent_type(index_type size) { Expects(size == Ext); } |
| 333 | |
| 334 | constexpr index_type size() const noexcept { return Ext; } |
| 335 | }; |
| 336 | |
| 337 | template <> |
| 338 | class extent_type<dynamic_extent> |
| 339 | { |
| 340 | public: |
| 341 | using index_type = std::ptrdiff_t; |
| 342 | |
| 343 | template <index_type Other> |
| 344 | explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) |
| 345 | { |
| 346 | } |
| 347 | |
| 348 | explicit constexpr extent_type(index_type size) : size_(size) { Expects(size >= 0); } |
| 349 | |
| 350 | constexpr index_type size() const noexcept { return size_; } |
| 351 | |
| 352 | private: |
| 353 | index_type size_; |
| 354 | }; |
| 355 | |
| 356 | template <class ElementType, std::ptrdiff_t Extent, std::ptrdiff_t Offset, std::ptrdiff_t Count> |
| 357 | struct calculate_subspan_type |
| 358 | { |
| 359 | using type = span<ElementType, Count != dynamic_extent ? Count : (Extent != dynamic_extent ? Extent - Offset : Extent)>; |
| 360 | }; |
| 361 | } // namespace details |
| 362 | |
| 363 | // [span], class template span |
| 364 | template <class ElementType, std::ptrdiff_t Extent> |
| 365 | class span |
| 366 | { |
| 367 | public: |
| 368 | // constants and types |
| 369 | using element_type = ElementType; |
| 370 | using value_type = std::remove_cv_t<ElementType>; |
| 371 | using index_type = std::ptrdiff_t; |
| 372 | using pointer = element_type*; |
| 373 | using reference = element_type&; |
| 374 | |
| 375 | using iterator = details::span_iterator<span<ElementType, Extent>, false>; |
| 376 | using const_iterator = details::span_iterator<span<ElementType, Extent>, true>; |
| 377 | using reverse_iterator = std::reverse_iterator<iterator>; |
| 378 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 379 | |
| 380 | using size_type = index_type; |
| 381 | |
| 382 | #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND) |
| 383 | static constexpr const index_type extent { Extent }; |
| 384 | #else |
| 385 | static constexpr index_type extent { Extent }; |
| 386 | #endif |
| 387 | |
| 388 | // [span.cons], span constructors, copy, assignment, and destructor |
| 389 | template <bool Dependent = false, |
| 390 | // "Dependent" is needed to make "std::enable_if_t<Dependent || Extent <= 0>" SFINAE, |
| 391 | // since "std::enable_if_t<Extent <= 0>" is ill-formed when Extent is greater than 0. |
| 392 | class = std::enable_if_t<(Dependent || Extent <= 0)>> |
| 393 | constexpr span() noexcept : storage_(nullptr, details::extent_type<0>()) |
| 394 | { |
| 395 | } |
| 396 | |
| 397 | constexpr span(pointer ptr, index_type count) : storage_(ptr, count) {} |
| 398 | |
| 399 | constexpr span(pointer firstElem, pointer lastElem) |
| 400 | : storage_(firstElem, std::distance(firstElem, lastElem)) |
| 401 | { |
| 402 | } |
| 403 | |
| 404 | template <std::size_t N> |
| 405 | constexpr span(element_type (&arr)[N]) noexcept |
| 406 | : storage_(KnownNotNull{&arr[0]}, details::extent_type<N>()) |
| 407 | { |
| 408 | } |
| 409 | |
| 410 | template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>> |
| 411 | constexpr span(std::array<ArrayElementType, N>& arr) noexcept |
| 412 | : storage_(&arr[0], details::extent_type<N>()) |
| 413 | { |
| 414 | } |
| 415 | |
| 416 | template <std::size_t N> |
| 417 | constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept |
| 418 | : storage_(&arr[0], details::extent_type<N>()) |
| 419 | { |
| 420 | } |
| 421 | |
| 422 | // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement |
| 423 | // on Container to be a contiguous sequence container. |
| 424 | template <class Container, |
| 425 | class = std::enable_if_t< |
| 426 | !details::is_span<Container>::value && !details::is_std_array<Container>::value && |
| 427 | std::is_convertible<typename Container::pointer, pointer>::value && |
| 428 | std::is_convertible<typename Container::pointer, |
| 429 | decltype(std::declval<Container>().data())>::value>> |
| 430 | constexpr span(Container& cont) : span(cont.data(), narrow<index_type>(cont.size())) |
| 431 | { |
| 432 | } |
| 433 | |
| 434 | template <class Container, |
| 435 | class = std::enable_if_t< |
| 436 | std::is_const<element_type>::value && !details::is_span<Container>::value && |
| 437 | std::is_convertible<typename Container::pointer, pointer>::value && |
| 438 | std::is_convertible<typename Container::pointer, |
| 439 | decltype(std::declval<Container>().data())>::value>> |
| 440 | constexpr span(const Container& cont) : span(cont.data(), narrow<index_type>(cont.size())) |
| 441 | { |
| 442 | } |
| 443 | |
| 444 | constexpr span(const span& other) noexcept = default; |
| 445 | |
| 446 | template < |
| 447 | class OtherElementType, std::ptrdiff_t OtherExtent, |
| 448 | class = std::enable_if_t< |
| 449 | details::is_allowed_extent_conversion<OtherExtent, Extent>::value && |
| 450 | details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>> |
| 451 | constexpr span(const span<OtherElementType, OtherExtent>& other) |
| 452 | : storage_(other.data(), details::extent_type<OtherExtent>(other.size())) |
| 453 | { |
| 454 | } |
| 455 | |
| 456 | ~span() noexcept = default; |
| 457 | constexpr span& operator=(const span& other) noexcept = default; |
| 458 | |
| 459 | // [span.sub], span subviews |
| 460 | template <std::ptrdiff_t Count> |
| 461 | constexpr span<element_type, Count> first() const |
| 462 | { |
| 463 | Expects(Count >= 0 && Count <= size()); |
| 464 | return {data(), Count}; |
| 465 | } |
| 466 | |
| 467 | template <std::ptrdiff_t Count> |
| 468 | constexpr span<element_type, Count> last() const |
| 469 | { |
| 470 | Expects(Count >= 0 && size() - Count >= 0); |
| 471 | return {data() + (size() - Count), Count}; |
| 472 | } |
| 473 | |
| 474 | template <std::ptrdiff_t Offset, std::ptrdiff_t Count = dynamic_extent> |
| 475 | constexpr auto subspan() const -> typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type |
| 476 | { |
| 477 | Expects((Offset >= 0 && size() - Offset >= 0) && |
| 478 | (Count == dynamic_extent || (Count >= 0 && Offset + Count <= size()))); |
| 479 | |
| 480 | return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count}; |
| 481 | } |
| 482 | |
| 483 | constexpr span<element_type, dynamic_extent> first(index_type count) const |
| 484 | { |
| 485 | Expects(count >= 0 && count <= size()); |
| 486 | return {data(), count}; |
| 487 | } |
| 488 | |
| 489 | constexpr span<element_type, dynamic_extent> last(index_type count) const |
| 490 | { |
| 491 | return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{}); |
| 492 | } |
| 493 | |
| 494 | constexpr span<element_type, dynamic_extent> subspan(index_type offset, |
| 495 | index_type count = dynamic_extent) const |
| 496 | { |
| 497 | return make_subspan(offset, count, subspan_selector<Extent>{}); |
| 498 | } |
| 499 | |
| 500 | |
| 501 | // [span.obs], span observers |
| 502 | constexpr index_type size() const noexcept { return storage_.size(); } |
| 503 | constexpr index_type size_bytes() const noexcept |
| 504 | { |
| 505 | return size() * narrow_cast<index_type>(sizeof(element_type)); |
| 506 | } |
| 507 | constexpr bool empty() const noexcept { return size() == 0; } |
| 508 | |
| 509 | // [span.elem], span element access |
| 510 | constexpr reference operator[](index_type idx) const |
| 511 | { |
| 512 | Expects(idx >= 0 && idx < storage_.size()); |
| 513 | return data()[idx]; |
| 514 | } |
| 515 | |
| 516 | constexpr reference at(index_type idx) const { return this->operator[](idx); } |
| 517 | constexpr reference operator()(index_type idx) const { return this->operator[](idx); } |
| 518 | constexpr pointer data() const noexcept { return storage_.data(); } |
| 519 | |
| 520 | // [span.iter], span iterator support |
| 521 | constexpr iterator begin() const noexcept { return {this, 0}; } |
| 522 | constexpr iterator end() const noexcept { return {this, size()}; } |
| 523 | |
| 524 | constexpr const_iterator cbegin() const noexcept { return {this, 0}; } |
| 525 | constexpr const_iterator cend() const noexcept { return {this, size()}; } |
| 526 | |
| 527 | constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } |
| 528 | constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; } |
| 529 | |
| 530 | constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator{cend()}; } |
| 531 | constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator{cbegin()}; } |
| 532 | |
| 533 | #ifdef _MSC_VER |
| 534 | // Tell MSVC how to unwrap spans in range-based-for |
| 535 | constexpr pointer _Unchecked_begin() const noexcept { return data(); } |
| 536 | constexpr pointer _Unchecked_end() const noexcept { return data() + size(); } |
| 537 | #endif // _MSC_VER |
| 538 | |
| 539 | private: |
| 540 | |
| 541 | // Needed to remove unnecessary null check in subspans |
| 542 | struct KnownNotNull |
| 543 | { |
| 544 | pointer p; |
| 545 | }; |
| 546 | |
| 547 | // this implementation detail class lets us take advantage of the |
| 548 | // empty base class optimization to pay for only storage of a single |
| 549 | // pointer in the case of fixed-size spans |
| 550 | template <class ExtentType> |
| 551 | class storage_type : public ExtentType |
| 552 | { |
| 553 | public: |
| 554 | // KnownNotNull parameter is needed to remove unnecessary null check |
| 555 | // in subspans and constructors from arrays |
| 556 | template <class OtherExtentType> |
| 557 | constexpr storage_type(KnownNotNull data, OtherExtentType ext) : ExtentType(ext), data_(data.p) |
| 558 | { |
| 559 | Expects(ExtentType::size() >= 0); |
| 560 | } |
| 561 | |
| 562 | |
| 563 | template <class OtherExtentType> |
| 564 | constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data) |
| 565 | { |
| 566 | Expects(ExtentType::size() >= 0); |
| 567 | Expects(data || ExtentType::size() == 0); |
| 568 | } |
| 569 | |
| 570 | constexpr pointer data() const noexcept { return data_; } |
| 571 | |
| 572 | private: |
| 573 | pointer data_; |
| 574 | }; |
| 575 | |
| 576 | storage_type<details::extent_type<Extent>> storage_; |
| 577 | |
| 578 | // The rest is needed to remove unnecessary null check |
| 579 | // in subspans and constructors from arrays |
| 580 | constexpr span(KnownNotNull ptr, index_type count) : storage_(ptr, count) {} |
| 581 | |
| 582 | template <std::ptrdiff_t CallerExtent> |
| 583 | class subspan_selector {}; |
| 584 | |
| 585 | template <std::ptrdiff_t CallerExtent> |
| 586 | span<element_type, dynamic_extent> make_subspan(index_type offset, |
| 587 | index_type count, |
| 588 | subspan_selector<CallerExtent>) const |
| 589 | { |
| 590 | span<element_type, dynamic_extent> tmp(*this); |
| 591 | return tmp.subspan(offset, count); |
| 592 | } |
| 593 | |
| 594 | span<element_type, dynamic_extent> make_subspan(index_type offset, |
| 595 | index_type count, |
| 596 | subspan_selector<dynamic_extent>) const |
| 597 | { |
| 598 | Expects(offset >= 0 && size() - offset >= 0); |
| 599 | if (count == dynamic_extent) |
| 600 | { |
| 601 | return { KnownNotNull{ data() + offset }, size() - offset }; |
| 602 | } |
| 603 | |
| 604 | Expects(count >= 0 && size() - offset >= count); |
| 605 | return { KnownNotNull{ data() + offset }, count }; |
| 606 | } |
| 607 | }; |
| 608 | |
| 609 | #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND) |
| 610 | template <class ElementType, std::ptrdiff_t Extent> |
| 611 | constexpr const typename span<ElementType, Extent>::index_type span<ElementType, Extent>::extent; |
| 612 | #endif |
| 613 | |
| 614 | |
| 615 | // [span.comparison], span comparison operators |
| 616 | template <class ElementType, std::ptrdiff_t FirstExtent, std::ptrdiff_t SecondExtent> |
| 617 | constexpr bool operator==(span<ElementType, FirstExtent> l, |
| 618 | span<ElementType, SecondExtent> r) |
| 619 | { |
| 620 | return std::equal(l.begin(), l.end(), r.begin(), r.end()); |
| 621 | } |
| 622 | |
| 623 | template <class ElementType, std::ptrdiff_t Extent> |
| 624 | constexpr bool operator!=(span<ElementType, Extent> l, |
| 625 | span<ElementType, Extent> r) |
| 626 | { |
| 627 | return !(l == r); |
| 628 | } |
| 629 | |
| 630 | template <class ElementType, std::ptrdiff_t Extent> |
| 631 | constexpr bool operator<(span<ElementType, Extent> l, |
| 632 | span<ElementType, Extent> r) |
| 633 | { |
| 634 | return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); |
| 635 | } |
| 636 | |
| 637 | template <class ElementType, std::ptrdiff_t Extent> |
| 638 | constexpr bool operator<=(span<ElementType, Extent> l, |
| 639 | span<ElementType, Extent> r) |
| 640 | { |
| 641 | return !(l > r); |
| 642 | } |
| 643 | |
| 644 | template <class ElementType, std::ptrdiff_t Extent> |
| 645 | constexpr bool operator>(span<ElementType, Extent> l, |
| 646 | span<ElementType, Extent> r) |
| 647 | { |
| 648 | return r < l; |
| 649 | } |
| 650 | |
| 651 | template <class ElementType, std::ptrdiff_t Extent> |
| 652 | constexpr bool operator>=(span<ElementType, Extent> l, |
| 653 | span<ElementType, Extent> r) |
| 654 | { |
| 655 | return !(l < r); |
| 656 | } |
| 657 | |
| 658 | namespace details |
| 659 | { |
| 660 | // if we only supported compilers with good constexpr support then |
| 661 | // this pair of classes could collapse down to a constexpr function |
| 662 | |
| 663 | // we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as |
| 664 | // constexpr |
| 665 | // and so will fail compilation of the template |
| 666 | template <class ElementType, std::ptrdiff_t Extent> |
| 667 | struct calculate_byte_size |
| 668 | : std::integral_constant<std::ptrdiff_t, |
| 669 | static_cast<std::ptrdiff_t>(sizeof(ElementType) * |
| 670 | static_cast<std::size_t>(Extent))> |
| 671 | { |
| 672 | }; |
| 673 | |
| 674 | template <class ElementType> |
| 675 | struct calculate_byte_size<ElementType, dynamic_extent> |
| 676 | : std::integral_constant<std::ptrdiff_t, dynamic_extent> |
| 677 | { |
| 678 | }; |
| 679 | } |
| 680 | |
| 681 | // [span.objectrep], views of object representation |
| 682 | template <class ElementType, std::ptrdiff_t Extent> |
| 683 | span<const byte, details::calculate_byte_size<ElementType, Extent>::value> |
| 684 | as_bytes(span<ElementType, Extent> s) noexcept |
| 685 | { |
| 686 | return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; |
| 687 | } |
| 688 | |
| 689 | template <class ElementType, std::ptrdiff_t Extent, |
| 690 | class = std::enable_if_t<!std::is_const<ElementType>::value>> |
| 691 | span<byte, details::calculate_byte_size<ElementType, Extent>::value> |
| 692 | as_writeable_bytes(span<ElementType, Extent> s) noexcept |
| 693 | { |
| 694 | return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; |
| 695 | } |
| 696 | |
| 697 | // |
| 698 | // make_span() - Utility functions for creating spans |
| 699 | // |
| 700 | template <class ElementType> |
| 701 | constexpr span<ElementType> make_span(ElementType* ptr, typename span<ElementType>::index_type count) |
| 702 | { |
| 703 | return span<ElementType>(ptr, count); |
| 704 | } |
| 705 | |
| 706 | template <class ElementType> |
| 707 | constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem) |
| 708 | { |
| 709 | return span<ElementType>(firstElem, lastElem); |
| 710 | } |
| 711 | |
| 712 | template <class ElementType, std::size_t N> |
| 713 | constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept |
| 714 | { |
| 715 | return span<ElementType, N>(arr); |
| 716 | } |
| 717 | |
| 718 | template <class Container> |
| 719 | constexpr span<typename Container::value_type> make_span(Container& cont) |
| 720 | { |
| 721 | return span<typename Container::value_type>(cont); |
| 722 | } |
| 723 | |
| 724 | template <class Container> |
| 725 | constexpr span<const typename Container::value_type> make_span(const Container& cont) |
| 726 | { |
| 727 | return span<const typename Container::value_type>(cont); |
| 728 | } |
| 729 | |
| 730 | template <class Ptr> |
| 731 | constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::ptrdiff_t count) |
| 732 | { |
| 733 | return span<typename Ptr::element_type>(cont, count); |
| 734 | } |
| 735 | |
| 736 | template <class Ptr> |
| 737 | constexpr span<typename Ptr::element_type> make_span(Ptr& cont) |
| 738 | { |
| 739 | return span<typename Ptr::element_type>(cont); |
| 740 | } |
| 741 | |
| 742 | // Specialization of gsl::at for span |
| 743 | template <class ElementType, std::ptrdiff_t Extent> |
| 744 | constexpr ElementType& at(span<ElementType, Extent> s, index i) |
| 745 | { |
| 746 | // No bounds checking here because it is done in span::operator[] called below |
| 747 | return s[i]; |
| 748 | } |
| 749 | |
| 750 | } // namespace gsl |
| 751 | |
| 752 | #ifdef _MSC_VER |
| 753 | #if _MSC_VER < 1910 |
| 754 | #undef constexpr |
| 755 | #pragma pop_macro("constexpr") |
| 756 | |
| 757 | #endif // _MSC_VER < 1910 |
| 758 | |
| 759 | #pragma warning(pop) |
| 760 | #endif // _MSC_VER |
| 761 | |
| 762 | #if __GNUC__ > 6 |
| 763 | #pragma GCC diagnostic pop |
| 764 | #endif // __GNUC__ > 6 |
| 765 | |
| 766 | #endif // GSL_SPAN_H |