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