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