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" |
| 23 | #include "gsl_util.h" |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame^] | 24 | #include "byte.h" |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 25 | #include <array> |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 26 | #include <limits> |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 27 | #include <iterator> |
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 | |
| 34 | // turn off some warnings that are noisy about our Expects statements |
| 35 | #pragma warning(push) |
| 36 | #pragma warning(disable : 4127) // conditional expression is constant |
| 37 | |
| 38 | // No MSVC does constexpr fully yet |
| 39 | #pragma push_macro("constexpr") |
| 40 | #define constexpr |
| 41 | |
| 42 | // VS 2013 workarounds |
| 43 | #if _MSC_VER <= 1800 |
| 44 | |
| 45 | #define GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
| 46 | #define GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT |
| 47 | |
| 48 | // noexcept is not understood |
| 49 | #ifndef GSL_THROW_ON_CONTRACT_VIOLATION |
| 50 | #pragma push_macro("noexcept") |
| 51 | #define noexcept /* nothing */ |
| 52 | #endif |
| 53 | |
| 54 | // turn off some misguided warnings |
| 55 | #pragma warning(push) |
| 56 | #pragma warning(disable : 4351) // warns about newly introduced aggregate initializer behavior |
| 57 | #pragma warning(disable : 4512) // warns that assignment op could not be generated |
| 58 | |
| 59 | #endif // _MSC_VER <= 1800 |
| 60 | |
| 61 | #endif // _MSC_VER |
| 62 | |
| 63 | #ifdef GSL_THROW_ON_CONTRACT_VIOLATION |
| 64 | |
| 65 | #ifdef _MSC_VER |
| 66 | #pragma push_macro("noexcept") |
| 67 | #endif |
| 68 | |
| 69 | #define noexcept /* nothing */ |
| 70 | |
| 71 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 72 | |
| 73 | namespace gsl |
| 74 | { |
| 75 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 76 | template <class ElementType, std::ptrdiff_t Extent = dynamic_extent> |
| 77 | class span; |
| 78 | |
| 79 | |
| 80 | namespace details |
| 81 | { |
| 82 | template <class T> |
| 83 | struct is_span_oracle : std::false_type |
| 84 | { |
| 85 | }; |
| 86 | |
| 87 | template <class ElementType, std::ptrdiff_t Extent> |
| 88 | struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type |
| 89 | { |
| 90 | }; |
| 91 | |
| 92 | template <class T> |
| 93 | struct is_span : is_span_oracle<std::remove_cv_t<T>> |
| 94 | { |
| 95 | }; |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 96 | |
| 97 | template <class From, class To> |
| 98 | struct is_allowed_pointer_conversion |
| 99 | : std::integral_constant<bool, |
| 100 | std::is_pointer<From>::value && |
| 101 | std::is_pointer<To>::value && |
| 102 | std::is_convertible<From, To>::value |
| 103 | > |
| 104 | { |
| 105 | }; |
| 106 | |
| 107 | template <class From, class To> |
| 108 | struct is_allowed_integral_conversion |
| 109 | : std::integral_constant<bool, |
| 110 | std::is_integral<From>::value && |
| 111 | std::is_integral<To>::value && |
| 112 | sizeof(From) == sizeof(To) && |
| 113 | alignof(From) == alignof(To) && |
| 114 | std::is_convertible<From, To>::value |
| 115 | > |
| 116 | { |
| 117 | }; |
| 118 | |
| 119 | template <class From, class To> |
| 120 | struct is_allowed_element_type_conversion |
| 121 | : std::integral_constant<bool, |
| 122 | std::is_same<From, std::remove_cv_t<To>>::value || |
| 123 | is_allowed_pointer_conversion<From, To>::value || |
| 124 | is_allowed_integral_conversion<From, To>::value |
| 125 | > |
| 126 | { |
| 127 | }; |
| 128 | |
| 129 | template <class From> |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame^] | 130 | struct is_allowed_element_type_conversion<From, byte> |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 131 | : std::integral_constant<bool, !std::is_const<From>::value> |
| 132 | { |
| 133 | }; |
| 134 | |
| 135 | template <class From> |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame^] | 136 | struct is_allowed_element_type_conversion<From, const byte> |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 137 | : std::integral_constant<bool, true> |
| 138 | { |
| 139 | }; |
| 140 | |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 141 | template <class Span> |
| 142 | class span_iterator |
| 143 | : public std::iterator<std::random_access_iterator_tag, typename Span::element_type> |
| 144 | { |
| 145 | using Base = std::iterator<std::random_access_iterator_tag, typename Span::element_type>; |
| 146 | |
| 147 | public: |
| 148 | using typename Base::reference; |
| 149 | using typename Base::pointer; |
| 150 | using typename Base::difference_type; |
| 151 | |
| 152 | span_iterator() : span_iterator(nullptr, 0) {} |
| 153 | span_iterator(const Span* span, typename Span::index_type index) : span_(span), index_(index) |
| 154 | { |
| 155 | Expects(span == nullptr || (index_ >= 0 && index <= span_->length())); |
| 156 | } |
| 157 | |
| 158 | reference operator*() const { Expects(span_); return (*span_)[index_]; } |
| 159 | pointer operator->() const { Expects(span_); return &((*span_)[index_]); } |
| 160 | |
| 161 | span_iterator& operator++() noexcept |
| 162 | { |
| 163 | Expects(span_ && index_ >= 0 && index_ < span_->length()); |
| 164 | ++index_; |
| 165 | return *this; |
| 166 | } |
| 167 | |
| 168 | span_iterator operator++(int) noexcept |
| 169 | { |
| 170 | auto ret = *this; |
| 171 | ++(*this); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | span_iterator& operator--() noexcept |
| 176 | { |
| 177 | Expects(span_ && index > 0 && index_ <= span_->length()); |
| 178 | --index_; |
| 179 | return *this; |
| 180 | } |
| 181 | |
| 182 | span_iterator operator--(int) noexcept |
| 183 | { |
| 184 | auto ret = *this; |
| 185 | --(*this); |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | span_iterator operator+(difference_type n) const noexcept |
| 190 | { |
| 191 | auto ret{*this}; |
| 192 | return ret += n; |
| 193 | } |
| 194 | |
| 195 | span_iterator& operator+=(difference_type n) noexcept |
| 196 | { |
| 197 | index_ += n; |
| 198 | Expects(span_ && index_ >= 0 && index_ <= span_->length()); |
| 199 | return *this; |
| 200 | } |
| 201 | |
| 202 | span_iterator operator-(difference_type n) const noexcept |
| 203 | { |
| 204 | auto ret{*this}; |
| 205 | return ret -= n; |
| 206 | } |
| 207 | |
| 208 | span_iterator& operator-=(difference_type n) noexcept |
| 209 | { |
| 210 | return *this += -n; |
| 211 | } |
| 212 | |
| 213 | difference_type operator-(const span_iterator& rhs) const noexcept |
| 214 | { |
| 215 | Expects(span_ == rhs.span_); |
| 216 | return index_ - rhs.index_; |
| 217 | } |
| 218 | |
| 219 | reference operator[](difference_type n) const noexcept |
| 220 | { return *(*this + n); } |
| 221 | |
| 222 | bool operator==(const span_iterator& rhs) const noexcept |
| 223 | { return span_ == rhs.span_ && index_ == rhs.index_; } |
| 224 | |
| 225 | bool operator!=(const span_iterator& rhs) const noexcept { return !(*this == rhs); } |
| 226 | |
| 227 | bool operator<(const span_iterator& rhs) const noexcept |
| 228 | { |
| 229 | Expects(span_ == rhs.span_); |
| 230 | return index_ < rhs.index_; |
| 231 | } |
| 232 | |
| 233 | bool operator<=(const span_iterator& rhs) const noexcept { return !(rhs < *this); } |
| 234 | |
| 235 | bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; } |
| 236 | |
| 237 | bool operator>=(const span_iterator& rhs) const noexcept { return !(rhs > *this); } |
| 238 | |
| 239 | void swap(span_iterator& rhs) noexcept |
| 240 | { |
| 241 | std::swap(index_, rhs.index_); |
| 242 | std::swap(m_span, rhs.m_span); |
| 243 | } |
| 244 | |
| 245 | private: |
| 246 | const Span* span_; |
| 247 | ptrdiff_t index_; |
| 248 | }; |
| 249 | |
| 250 | template <typename Span> |
| 251 | span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n, |
| 252 | const span_iterator<Span>& rhs) noexcept |
| 253 | { return rhs + n; } |
| 254 | |
| 255 | template <typename Span> |
| 256 | span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n, |
| 257 | const span_iterator<Span>& rhs) noexcept |
Neil MacIntosh | 25ff7ec | 2016-05-29 13:54:19 -0700 | [diff] [blame] | 258 | { return rhs - n; } |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 259 | |
| 260 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 261 | } // namespace details |
| 262 | |
| 263 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 264 | // [views.constants], constants |
| 265 | constexpr const std::ptrdiff_t dynamic_extent = -1; |
| 266 | |
| 267 | |
| 268 | // [span], class template span |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 269 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 270 | class span { |
| 271 | public: |
| 272 | // constants and types |
| 273 | using element_type = ElementType; |
| 274 | using index_type = std::ptrdiff_t; |
| 275 | using pointer = element_type*; |
| 276 | using reference = element_type&; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 277 | |
| 278 | using iterator = details::span_iterator<span<ElementType, Extent>>; |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 279 | using reverse_iterator = std::reverse_iterator<iterator>; |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 280 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 281 | constexpr static const index_type extent = Extent; |
| 282 | |
| 283 | // [span.cons], span constructors, copy, assignment, and destructor |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 284 | constexpr span() noexcept : storage_(nullptr, extent_type<0>()) |
| 285 | {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 286 | |
| 287 | constexpr span(nullptr_t) noexcept : span() |
| 288 | {} |
| 289 | |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 290 | constexpr span(pointer ptr, index_type count) : storage_(ptr, count) |
Neil MacIntosh | 25ff7ec | 2016-05-29 13:54:19 -0700 | [diff] [blame] | 291 | { Expects((!ptr && count == 0) || (ptr && count >= 0)); } |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 292 | |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 293 | constexpr span(pointer firstElem, pointer lastElem) |
| 294 | : storage_(firstElem, std::distance(firstElem, lastElem)) |
| 295 | {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 296 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 297 | template <size_t N> |
Neil MacIntosh | f61a9bb | 2016-02-29 13:16:48 -0800 | [diff] [blame] | 298 | constexpr span(element_type(&arr)[N]) |
| 299 | : storage_(&arr[0], extent_type<N>()) |
| 300 | {} |
| 301 | |
| 302 | template <size_t N> |
| 303 | constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) |
| 304 | : storage_(&arr[0], extent_type<N>()) |
| 305 | {} |
| 306 | |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame] | 307 | template <size_t N, class = std::enable_if_t<is_const<element_type>::value>> |
Neil MacIntosh | f61a9bb | 2016-02-29 13:16:48 -0800 | [diff] [blame] | 308 | constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) |
| 309 | : storage_(&arr[0], extent_type<N>()) |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 310 | {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 311 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 312 | // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement |
| 313 | // on Container to be a contiguous sequence container. |
| 314 | template <class Container, |
| 315 | class = std::enable_if_t<!details::is_span<Container>::value && |
| 316 | std::is_convertible<Container::pointer, pointer>::value && |
| 317 | std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value> |
| 318 | > |
| 319 | constexpr span(Container& cont) : span(cont.data(), cont.size()) {} |
| 320 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 321 | template <class Container, |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame] | 322 | class = std::enable_if_t<std::is_const<element_type>::value && |
| 323 | !details::is_span<Container>::value && |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 324 | std::is_convertible<Container::pointer, pointer>::value && |
| 325 | std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value> |
| 326 | > |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame] | 327 | constexpr span(const Container& cont) : span(cont.data(), cont.size()) {} |
| 328 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 329 | constexpr span(const span& other) noexcept = default; |
| 330 | constexpr span(span&& other) noexcept = default; |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 331 | |
| 332 | template <class OtherElementType, std::ptrdiff_t OtherExtent, |
| 333 | class = std::enable_if_t<!std::is_same<element_type, OtherElementType>::value && |
| 334 | details::is_allowed_element_type_conversion<OtherElementType, element_type>::value |
| 335 | > |
| 336 | > |
| 337 | constexpr span(const span<OtherElementType, OtherExtent>& other) |
| 338 | : storage_(reinterpret_cast<pointer>(other.data()), other.length()) |
| 339 | {} |
| 340 | |
| 341 | template <class OtherElementType, std::ptrdiff_t OtherExtent, |
| 342 | class = std::enable_if_t<!std::is_same<element_type, OtherElementType>::value && |
| 343 | details::is_allowed_element_type_conversion<OtherElementType, element_type>::value |
| 344 | > |
| 345 | > |
| 346 | constexpr span(span<OtherElementType, OtherExtent>&& other) |
| 347 | : storage_(reinterpret_cast<pointer>(other.data()), other.length()) |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 348 | {} |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 349 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 350 | ~span() noexcept = default; |
| 351 | constexpr span& operator=(const span& other) noexcept = default; |
| 352 | constexpr span& operator=(span&& other) noexcept = default; |
| 353 | |
| 354 | // [span.sub], span subviews |
| 355 | template <ptrdiff_t Count> |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 356 | constexpr span<element_type, Count> first() const |
| 357 | { |
| 358 | Expects(Count >= 0 && Count <= size()); |
| 359 | return { data(), Count }; |
| 360 | } |
| 361 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 362 | template <ptrdiff_t Count> |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 363 | constexpr span<element_type, Count> last() const |
| 364 | { |
| 365 | Expects(Count >= 0 && Count <= size()); |
| 366 | return{ Count == 0 ? data() : data() + (size() - Count), Count }; |
| 367 | } |
| 368 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 369 | template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent> |
Neil MacIntosh | c8a412f | 2016-03-18 16:49:29 -0700 | [diff] [blame] | 370 | constexpr span<element_type, Count> subspan() const |
| 371 | { |
| 372 | Expects((Offset == 0 || Offset > 0 && Offset <= size()) && |
| 373 | (Count == dynamic_extent || Count >= 0 && Offset + Count <= size())); |
| 374 | return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count }; |
| 375 | } |
| 376 | |
| 377 | constexpr span<element_type, dynamic_extent> first(index_type count) const |
| 378 | { |
| 379 | Expects(count >= 0 && count <= size()); |
| 380 | return { data(), count }; |
| 381 | } |
| 382 | |
| 383 | constexpr span<element_type, dynamic_extent> last(index_type count) const |
| 384 | { |
| 385 | Expects(count >= 0 && count <= size()); |
| 386 | return { count == 0 ? data() : data() + (size() - count), count }; |
| 387 | } |
| 388 | |
| 389 | constexpr span<element_type, dynamic_extent> subspan(index_type offset, |
| 390 | index_type count = dynamic_extent) const |
| 391 | { |
| 392 | Expects((offset == 0 || offset > 0 && offset <= size()) && |
| 393 | (count == dynamic_extent || count >= 0 && offset + count <= size())); |
| 394 | return { data() + offset, count == dynamic_extent ? size() - offset : count }; |
| 395 | } |
| 396 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 397 | // [span.obs], span observers |
| 398 | constexpr index_type length() const noexcept { return size(); } |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 399 | constexpr index_type size() const noexcept { return storage_.size(); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 400 | constexpr index_type length_bytes() const noexcept { return size_bytes(); } |
| 401 | constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); } |
| 402 | constexpr bool empty() const noexcept { return size() == 0; } |
| 403 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 404 | // [span.elem], span element access |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 405 | constexpr reference operator[](index_type idx) const |
| 406 | { |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 407 | Expects(idx >= 0 && idx < storage_.size()); |
| 408 | return storage_.data()[idx]; |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 409 | } |
| 410 | constexpr reference operator()(index_type idx) const { return this->operator[](idx); } |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 411 | constexpr pointer data() const noexcept { return storage_.data(); } |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 412 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 413 | // [span.iter], span iterator support |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 414 | iterator begin() const noexcept { return {this, 0}; } |
| 415 | iterator end() const noexcept { return {this, length()}; } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 416 | |
Neil MacIntosh | d9d6ff0 | 2016-05-29 13:52:28 -0700 | [diff] [blame] | 417 | reverse_iterator rbegin() const noexcept { return {this, length()}; } |
| 418 | reverse_iterator rend() const noexcept { return {this, 0}; } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 419 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 420 | private: |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 421 | template <index_type Extent> |
| 422 | class extent_type; |
| 423 | |
| 424 | template <index_type Extent> |
| 425 | class extent_type |
| 426 | { |
| 427 | public: |
| 428 | static_assert(Extent >= 0, "A fixed-size span must be >= 0 in size."); |
| 429 | |
| 430 | constexpr extent_type() noexcept {} |
| 431 | |
| 432 | template <index_type Other> |
| 433 | constexpr extent_type(extent_type<Other>) noexcept |
| 434 | { |
| 435 | static_assert(Other == Extent, |
| 436 | "Mismatch between fixed-size extent and size of initializing data."); |
| 437 | } |
| 438 | |
| 439 | constexpr extent_type(index_type size) |
| 440 | { Expects(size == Extent); } |
| 441 | |
| 442 | constexpr inline index_type size() const noexcept { return Extent; } |
| 443 | }; |
| 444 | |
| 445 | template <> |
| 446 | class extent_type<dynamic_extent> |
| 447 | { |
| 448 | public: |
| 449 | template <index_type Other> |
| 450 | explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) |
| 451 | {} |
| 452 | |
| 453 | explicit constexpr extent_type(index_type size) : size_(size) |
| 454 | { Expects(size >= 0); } |
| 455 | |
| 456 | constexpr inline index_type size() const noexcept |
| 457 | { return size_; } |
| 458 | |
| 459 | private: |
| 460 | index_type size_; |
| 461 | }; |
| 462 | |
| 463 | // this implementation detail class lets us take advantage of the |
| 464 | // empty base class optimization to pay for only storage of a single |
| 465 | // pointer in the case of fixed-size spans |
| 466 | template <class ExtentType> |
| 467 | class storage_type : public ExtentType |
| 468 | { |
| 469 | public: |
| 470 | template <class OtherExtentType> |
| 471 | storage_type(pointer data, OtherExtentType ext) |
| 472 | : ExtentType(ext), data_(data) {} |
| 473 | |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 474 | constexpr inline pointer data() const noexcept |
| 475 | { return data_; } |
| 476 | |
| 477 | private: |
| 478 | pointer data_; |
| 479 | }; |
| 480 | |
| 481 | storage_type<extent_type<Extent>> storage_; |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 485 | // [span.comparison], span comparison operators |
| 486 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 487 | constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept |
| 488 | { return std::equal(l.begin(), l.end(), r.begin(), r.end()); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 489 | |
| 490 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 491 | constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept |
| 492 | { return !(l == r); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 493 | |
| 494 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 495 | constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept |
| 496 | { return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 497 | |
| 498 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 499 | constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept |
| 500 | { return !(l > r); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 501 | |
| 502 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 503 | constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept |
| 504 | { return r < l; } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 505 | |
| 506 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | d63c980 | 2016-05-29 14:05:09 -0700 | [diff] [blame] | 507 | constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept |
| 508 | { return !(l < r); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 509 | |
| 510 | |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame^] | 511 | namespace details |
| 512 | { |
| 513 | // if we only supported compilers with good constexpr support then |
| 514 | // this pair of classes could collapse down to a constexpr function |
| 515 | |
| 516 | // we should use a narrow_cast<> to go to size_t, but older compilers may not see it as constexpr |
| 517 | // and so will fail compilation of the template |
| 518 | template <class ElementType, ptrdiff_t Extent> |
| 519 | struct calculate_byte_size : |
| 520 | std::integral_constant<std::ptrdiff_t, static_cast<ptrdiff_t>(sizeof(ElementType) * static_cast<size_t>(Extent))> |
| 521 | {}; |
| 522 | |
| 523 | template <class ElementType> |
| 524 | struct calculate_byte_size<ElementType, dynamic_extent> : |
| 525 | std::integral_constant<std::ptrdiff_t, dynamic_extent> |
| 526 | {}; |
| 527 | } |
| 528 | |
| 529 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 530 | // [span.objectrep], views of object representation |
| 531 | template <class ElementType, ptrdiff_t Extent> |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame^] | 532 | constexpr span<const byte, details::calculate_byte_size<ElementType, Extent>::value> as_bytes(span<ElementType, Extent> s) noexcept |
| 533 | { return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 534 | |
Neil MacIntosh | ba8ebef | 2016-05-29 17:06:29 -0700 | [diff] [blame^] | 535 | template <class ElementType, ptrdiff_t Extent, class = std::enable_if_t<!std::is_const<ElementType>::value>> |
| 536 | constexpr span<byte, details::calculate_byte_size<ElementType, Extent>::value> as_writeable_bytes(span<ElementType, Extent> s) noexcept |
| 537 | { return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; } |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 538 | |
| 539 | } // namespace gsl |
| 540 | |
| 541 | #ifdef _MSC_VER |
| 542 | |
| 543 | #undef constexpr |
| 544 | #pragma pop_macro("constexpr") |
| 545 | |
| 546 | #if _MSC_VER <= 1800 |
| 547 | #pragma warning(pop) |
| 548 | |
| 549 | #ifndef GSL_THROW_ON_CONTRACT_VIOLATION |
| 550 | #undef noexcept |
| 551 | #pragma pop_macro("noexcept") |
| 552 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 553 | |
| 554 | #undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
| 555 | |
| 556 | #endif // _MSC_VER <= 1800 |
| 557 | |
| 558 | #endif // _MSC_VER |
| 559 | |
| 560 | #if defined(GSL_THROW_ON_CONTRACT_VIOLATION) |
| 561 | |
| 562 | #undef noexcept |
| 563 | |
| 564 | #ifdef _MSC_VER |
| 565 | #pragma warning(pop) |
| 566 | #pragma pop_macro("noexcept") |
| 567 | #endif |
| 568 | |
| 569 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 570 | |
| 571 | #endif // GSL_SPAN_H |