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