blob: f40a7ab8b99be8173b1f1a4dfeb9b99971b5e889 [file] [log] [blame]
Neil MacIntoshcec26a22016-02-24 11:26:28 -08001///////////////////////////////////////////////////////////////////////////////
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"
Neil MacIntosh26747242016-06-26 17:00:56 +030023#include "gsl_byte.h"
Neil MacIntoshb03b04b2016-07-20 13:17:47 -070024#include "gsl_util.h"
Neil MacIntoshcec26a22016-02-24 11:26:28 -080025#include <array>
Neil MacIntoshd3929c52016-02-24 16:11:33 -080026#include <iterator>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -070027#include <limits>
Neil MacIntoshcec26a22016-02-24 11:26:28 -080028#include <stdexcept>
29#include <type_traits>
30#include <utility>
31
32#ifdef _MSC_VER
33
Neil MacIntoshcec26a22016-02-24 11:26:28 -080034#pragma warning(push)
Neil MacIntosha9f0ce22016-03-31 12:01:07 -070035
36// turn off some warnings that are noisy about our Expects statements
Neil MacIntoshcec26a22016-02-24 11:26:28 -080037#pragma warning(disable : 4127) // conditional expression is constant
38
Neil MacIntosha9f0ce22016-03-31 12:01:07 -070039// 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
Neil MacIntoshb03b04b2016-07-20 13:17:47 -070042#pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
Neil MacIntosha9f0ce22016-03-31 12:01:07 -070043
Neil MacIntoshcec26a22016-02-24 11:26:28 -080044// 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
Neil MacIntoshb03b04b2016-07-20 13:17:47 -070054// noexcept is not understood
Neil MacIntoshcec26a22016-02-24 11:26:28 -080055#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
79namespace gsl
80{
81
Neil MacIntoshc40094a2016-03-01 12:11:41 -080082template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
83class span;
84
Neil MacIntoshb03b04b2016-07-20 13:17:47 -070085// [views.constants], constants
Neil MacIntoshc94a66f2016-06-12 18:28:19 -070086constexpr const std::ptrdiff_t dynamic_extent = -1;
87
Neil MacIntoshc94a66f2016-06-12 18:28:19 -070088// implementation details
Neil MacIntoshc40094a2016-03-01 12:11:41 -080089namespace details
90{
Neil MacIntoshb03b04b2016-07-20 13:17:47 -070091 template <class T>
92 struct is_span_oracle : std::false_type
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -070093 {
Neil MacIntoshb03b04b2016-07-20 13:17:47 -070094 };
95
96 template <class ElementType, std::ptrdiff_t Extent>
97 struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
98 {
99 };
100
101 template <class T>
102 struct is_span : is_span_oracle<std::remove_cv_t<T>>
103 {
104 };
105
106 template <class From, class To>
107 struct is_allowed_pointer_conversion
108 : std::bool_constant<std::is_pointer<From>::value && std::is_pointer<To>::value &&
109 std::is_convertible<From, To>::value>
110 {
111 };
112
113 template <class From, class To>
114 struct is_allowed_integral_conversion
115 : std::bool_constant<std::is_integral<From>::value && std::is_integral<To>::value &&
116 sizeof(From) == sizeof(To) && alignof(From) == alignof(To) &&
117 std::is_convertible<From, To>::value>
118 {
119 };
120
121 template <std::ptrdiff_t From, std::ptrdiff_t To>
122 struct is_allowed_extent_conversion
123 : std::bool_constant<From == To || From == gsl::dynamic_extent || To == gsl::dynamic_extent>
124 {
125 };
126
127 template <class From, class To>
128 struct is_allowed_element_type_conversion
129 : std::bool_constant<std::is_same<From, std::remove_cv_t<To>>::value ||
130 is_allowed_pointer_conversion<From, To>::value ||
131 is_allowed_integral_conversion<From, To>::value>
132 {
133 };
134
135 template <class From>
136 struct is_allowed_element_type_conversion<From, byte>
137 : std::bool_constant<!std::is_const<From>::value>
138 {
139 };
140
141 template <class From>
142 struct is_allowed_element_type_conversion<From, const byte> : std::true_type
143 {
144 };
145
146 template <class Span>
147 class const_span_iterator
148 {
149 public:
150 using iterator_category = std::random_access_iterator_tag;
151 using value_type = typename Span::element_type;
152 using difference_type = std::ptrdiff_t;
153
154 using const_pointer = std::add_const_t<value_type*>;
155 using pointer = const_pointer;
156
157 using const_reference = std::add_const_t<value_type&>;
158 using reference = const_reference;
159
160 constexpr const_span_iterator() : const_span_iterator(nullptr, 0) {}
161 constexpr const_span_iterator(const Span* span, typename Span::index_type index)
162 : span_(span), index_(index)
163 {
164 Expects(span == nullptr || (index_ >= 0 && index <= span_->length()));
165 }
166
167 constexpr reference operator*() const
168 {
169 Expects(span_);
170 return (*span_)[index_];
171 }
172 constexpr pointer operator->() const
173 {
174 Expects(span_);
175 return &((*span_)[index_]);
176 }
177
178 constexpr const_span_iterator& operator++() noexcept
179 {
180 Expects(span_ && index_ >= 0 && index_ < span_->length());
181 ++index_;
182 return *this;
183 }
184
185 constexpr const_span_iterator operator++(int) noexcept
186 {
187 auto ret = *this;
188 ++(*this);
189 return ret;
190 }
191
192 constexpr const_span_iterator& operator--() noexcept
193 {
194 Expects(span_ && index_ > 0 && index_ <= span_->length());
195 --index_;
196 return *this;
197 }
198
199 constexpr const_span_iterator operator--(int) noexcept
200 {
201 auto ret = *this;
202 --(*this);
203 return ret;
204 }
205
206 constexpr const_span_iterator operator+(difference_type n) const noexcept
207 {
208 auto ret{*this};
209 return ret += n;
210 }
211
212 constexpr const_span_iterator& operator+=(difference_type n) noexcept
213 {
214 Expects(span_ && (index_ + n) >= 0 && (index_ + n) <= span_->length());
215 index_ += n;
216 return *this;
217 }
218
219 constexpr const_span_iterator operator-(difference_type n) const noexcept
220 {
221 auto ret{*this};
222 return ret -= n;
223 }
224
225 constexpr const_span_iterator& operator-=(difference_type n) noexcept
226 {
227 return *this += -n;
228 }
229
230 constexpr difference_type operator-(const const_span_iterator& rhs) const noexcept
231 {
232 Expects(span_ == rhs.span_);
233 return index_ - rhs.index_;
234 }
235
236 constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); }
237
238 constexpr bool operator==(const const_span_iterator& rhs) const noexcept
239 {
240 return span_ == rhs.span_ && index_ == rhs.index_;
241 }
242
243 constexpr bool operator!=(const const_span_iterator& rhs) const noexcept
244 {
245 return !(*this == rhs);
246 }
247
248 constexpr bool operator<(const const_span_iterator& rhs) const noexcept
249 {
250 Expects(span_ == rhs.span_);
251 return index_ < rhs.index_;
252 }
253
254 constexpr bool operator<=(const const_span_iterator& rhs) const noexcept
255 {
256 return !(rhs < *this);
257 }
258
259 constexpr bool operator>(const const_span_iterator& rhs) const noexcept
260 {
261 return rhs < *this;
262 }
263
264 constexpr bool operator>=(const const_span_iterator& rhs) const noexcept
265 {
266 return !(rhs > *this);
267 }
268
269 void swap(const_span_iterator& rhs) noexcept
270 {
271 std::swap(index_, rhs.index_);
272 std::swap(m_span, rhs.m_span);
273 }
274
275 private:
276 const Span* span_;
277 ptrdiff_t index_;
278 };
279
280 template <class Span>
281 class span_iterator : public const_span_iterator<Span>
282 {
283 using base_type = const_span_iterator<Span>;
284
285 public:
286 using iterator_category = std::random_access_iterator_tag;
287 using value_type = typename Span::element_type;
288 using difference_type = std::ptrdiff_t;
289
290 using pointer = value_type*;
291 using reference = value_type&;
292
293 constexpr span_iterator() : base_type() {}
294 constexpr span_iterator(const Span* span, typename Span::index_type index)
295 : base_type(span, index)
296 {
297 }
298
299 constexpr reference operator*() const
300 {
301 return const_cast<reference>(base_type::operator*());
302 }
303 constexpr pointer operator->() const
304 {
305 return const_cast<pointer>(base_type::operator->());
306 }
307
308 constexpr span_iterator& operator++() noexcept
309 {
310 base_type::operator++();
311 return *this;
312 }
313
314 constexpr span_iterator operator++(int) noexcept { return base_type::operator++(1); }
315
316 constexpr span_iterator& operator--() noexcept
317 {
318 base_type::operator--();
319 return *this;
320 }
321
322 constexpr span_iterator operator--(int) noexcept { return base_type::operator--(1); }
323
324 constexpr span_iterator operator+(difference_type n) const noexcept
325 {
326 return base_type::operator+(n);
327 }
328
329 constexpr span_iterator& operator+=(difference_type n) noexcept
330 {
331 return base_type::operator+=(n);
332 }
333
334 constexpr span_iterator operator-(difference_type n) const noexcept
335 {
336 return base_type::operator-(n);
337 }
338
339 constexpr span_iterator& operator-=(difference_type n) noexcept
340 {
341 return base_type::operator-=(n);
342 }
343
344 constexpr difference_type operator-(const span_iterator& rhs) const noexcept
345 {
346 return base_type::operator-(rhs);
347 }
348
349 constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); }
350
351 constexpr bool operator==(const span_iterator& rhs) const noexcept
352 {
353 return base_type::operator==(rhs);
354 }
355
356 constexpr bool operator!=(const span_iterator& rhs) const noexcept
357 {
358 return !(*this == rhs);
359 }
360
361 constexpr bool operator<(const span_iterator& rhs) const noexcept
362 {
363 return base_type::operator<(rhs);
364 }
365
366 constexpr bool operator<=(const span_iterator& rhs) const noexcept
367 {
368 return !(rhs < *this);
369 }
370
371 constexpr bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; }
372
373 constexpr bool operator>=(const span_iterator& rhs) const noexcept
374 {
375 return !(rhs > *this);
376 }
377
378 void swap(span_iterator& rhs) noexcept { base_type::swap(rhs); }
379 };
380
381 template <typename Span>
382 constexpr const_span_iterator<Span>
383 operator+(typename const_span_iterator<Span>::difference_type n,
384 const const_span_iterator<Span>& rhs) noexcept
385 {
386 return rhs + n;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700387 }
388
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700389 template <typename Span>
390 constexpr const_span_iterator<Span>
391 operator-(typename const_span_iterator<Span>::difference_type n,
392 const const_span_iterator<Span>& rhs) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700393 {
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700394 return rhs - n;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700395 }
396
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700397 template <typename Span>
398 constexpr span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n,
399 const span_iterator<Span>& rhs) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700400 {
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700401 return rhs + n;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700402 }
403
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700404 template <typename Span>
405 constexpr span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n,
406 const span_iterator<Span>& rhs) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700407 {
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700408 return rhs - n;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700409 }
410
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800411} // namespace details
412
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700413// [span], class template span
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800414template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700415class span
416{
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800417public:
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700418 // constants and types
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800419 using element_type = ElementType;
420 using index_type = std::ptrdiff_t;
421 using pointer = element_type*;
422 using reference = element_type&;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700423
424 using iterator = details::span_iterator<span<ElementType, Extent>>;
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700425 using const_iterator = details::const_span_iterator<span>;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800426 using reverse_iterator = std::reverse_iterator<iterator>;
Neil MacIntosh26747242016-06-26 17:00:56 +0300427 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700428
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800429 constexpr static const index_type extent = Extent;
430
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700431 // [span.cons], span constructors, copy, assignment, and destructor
432 constexpr span() noexcept : storage_(nullptr, extent_type<0>()) {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800433
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700434 constexpr span(nullptr_t) noexcept : span() {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800435
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700436 constexpr span(pointer ptr, index_type count) : storage_(ptr, count) {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800437
Neil MacIntosh502cd662016-02-28 00:50:53 -0800438 constexpr span(pointer firstElem, pointer lastElem)
439 : storage_(firstElem, std::distance(firstElem, lastElem))
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700440 {
441 }
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800442
443 template <size_t N>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700444 constexpr span(element_type (&arr)[N]) noexcept : storage_(&arr[0], extent_type<N>())
445 {
446 }
447
448 template <size_t N>
449 constexpr span(std::array<element_type, N>& arr) noexcept : storage_(&arr[0], extent_type<N>())
450 {
451 }
Neil MacIntosh62f30202016-06-14 20:14:17 -0700452
453 template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
Neil MacIntoshf2ab3a52016-07-20 09:24:49 -0700454 constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) noexcept
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800455 : storage_(&arr[0], extent_type<N>())
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700456 {
457 }
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800458
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700459 template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
Neil MacIntoshf2ab3a52016-07-20 09:24:49 -0700460 constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800461 : storage_(&arr[0], extent_type<N>())
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700462 {
463 }
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800464
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800465 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
466 // on Container to be a contiguous sequence container.
467 template <class Container,
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700468 class = std::enable_if_t<
469 !details::is_span<Container>::value &&
470 std::is_convertible<Container::pointer, pointer>::value &&
471 std::is_convertible<Container::pointer,
472 decltype(std::declval<Container>().data())>::value>>
473 constexpr span(Container& cont) : span(cont.data(), cont.size())
474 {
475 }
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800476
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800477 template <class Container,
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700478 class = std::enable_if_t<
479 std::is_const<element_type>::value && !details::is_span<Container>::value &&
480 std::is_convertible<Container::pointer, pointer>::value &&
481 std::is_convertible<Container::pointer,
482 decltype(std::declval<Container>().data())>::value>>
483 constexpr span(const Container& cont) : span(cont.data(), cont.size())
484 {
485 }
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700486
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800487 constexpr span(const span& other) noexcept = default;
488 constexpr span(span&& other) noexcept = default;
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700489
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700490 template <
491 class OtherElementType, std::ptrdiff_t OtherExtent,
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700492 class = std::enable_if_t<
493 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700494 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
495 constexpr span(const span<OtherElementType, OtherExtent>& other)
496 : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
497 {
498 }
499
500 template <
501 class OtherElementType, std::ptrdiff_t OtherExtent,
502 class = std::enable_if_t<
503 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
504 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700505 constexpr span(span<OtherElementType, OtherExtent>&& other)
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700506 : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700507 {
508 }
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700509
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800510 ~span() noexcept = default;
511 constexpr span& operator=(const span& other) noexcept = default;
512 constexpr span& operator=(span&& other) noexcept = default;
513
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700514 // [span.sub], span subviews
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800515 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700516 constexpr span<element_type, Count> first() const
517 {
518 Expects(Count >= 0 && Count <= size());
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700519 return {data(), Count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700520 }
521
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800522 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700523 constexpr span<element_type, Count> last() const
524 {
525 Expects(Count >= 0 && Count <= size());
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700526 return {data() + (size() - Count), Count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700527 }
528
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800529 template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700530 constexpr span<element_type, Count> subspan() const
531 {
532 Expects((Offset == 0 || Offset > 0 && Offset <= size()) &&
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700533 (Count == dynamic_extent || Count >= 0 && Offset + Count <= size()));
534 return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700535 }
536
537 constexpr span<element_type, dynamic_extent> first(index_type count) const
538 {
539 Expects(count >= 0 && count <= size());
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700540 return {data(), count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700541 }
542
543 constexpr span<element_type, dynamic_extent> last(index_type count) const
544 {
545 Expects(count >= 0 && count <= size());
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700546 return {data() + (size() - count), count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700547 }
548
549 constexpr span<element_type, dynamic_extent> subspan(index_type offset,
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700550 index_type count = dynamic_extent) const
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700551 {
552 Expects((offset == 0 || offset > 0 && offset <= size()) &&
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700553 (count == dynamic_extent || count >= 0 && offset + count <= size()));
554 return {data() + offset, count == dynamic_extent ? size() - offset : count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700555 }
556
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700557 // [span.obs], span observers
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800558 constexpr index_type length() const noexcept { return size(); }
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700559 constexpr index_type size() const noexcept { return storage_.size(); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800560 constexpr index_type length_bytes() const noexcept { return size_bytes(); }
561 constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); }
562 constexpr bool empty() const noexcept { return size() == 0; }
563
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700564 // [span.elem], span element access
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800565 constexpr reference operator[](index_type idx) const
566 {
Neil MacIntosh502cd662016-02-28 00:50:53 -0800567 Expects(idx >= 0 && idx < storage_.size());
Neil MacIntoshf2ab3a52016-07-20 09:24:49 -0700568 return data()[idx];
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800569 }
570 constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800571 constexpr pointer data() const noexcept { return storage_.data(); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700572
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700573 // [span.iter], span iterator support
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700574 iterator begin() const noexcept { return {this, 0}; }
575 iterator end() const noexcept { return {this, length()}; }
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700576
577 const_iterator cbegin() const noexcept { return {this, 0}; }
578 const_iterator cend() const noexcept { return {this, length()}; }
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700579
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700580 reverse_iterator rbegin() const noexcept { return reverse_iterator{{this, length()}}; }
581 reverse_iterator rend() const noexcept { return reverse_iterator{{this, 0}}; }
582
583 const_reverse_iterator crbegin() const noexcept { return reverse_iterator{{this, length()}}; }
584 const_reverse_iterator crend() const noexcept { return reverse_iterator{{this, 0}}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800585
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800586private:
Neil MacIntosh502cd662016-02-28 00:50:53 -0800587 template <index_type Extent>
588 class extent_type;
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700589
Neil MacIntosh502cd662016-02-28 00:50:53 -0800590 template <index_type Extent>
591 class extent_type
592 {
593 public:
594 static_assert(Extent >= 0, "A fixed-size span must be >= 0 in size.");
595
596 constexpr extent_type() noexcept {}
597
598 template <index_type Other>
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700599 constexpr extent_type(extent_type<Other> ext) noexcept
Neil MacIntosh502cd662016-02-28 00:50:53 -0800600 {
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700601 static_assert(Other == Extent || Other == dynamic_extent,
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700602 "Mismatch between fixed-size extent and size of initializing data.");
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700603 Expects(ext.size() == Extent);
Neil MacIntosh502cd662016-02-28 00:50:53 -0800604 }
605
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700606 constexpr extent_type(index_type size) { Expects(size == Extent); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800607
608 constexpr inline index_type size() const noexcept { return Extent; }
609 };
610
611 template <>
612 class extent_type<dynamic_extent>
613 {
614 public:
615 template <index_type Other>
616 explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700617 {
618 }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800619
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700620 explicit constexpr extent_type(index_type size) : size_(size) { Expects(size >= 0); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800621
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700622 constexpr inline index_type size() const noexcept { return size_; }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800623
624 private:
625 index_type size_;
626 };
627
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700628 // this implementation detail class lets us take advantage of the
Neil MacIntosh502cd662016-02-28 00:50:53 -0800629 // empty base class optimization to pay for only storage of a single
630 // pointer in the case of fixed-size spans
631 template <class ExtentType>
632 class storage_type : public ExtentType
633 {
634 public:
635 template <class OtherExtentType>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700636 constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
637 {
638 Expects((!data && size() == 0) || (data && size() >= 0));
639 }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800640
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700641 constexpr inline pointer data() const noexcept { return data_; }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800642
643 private:
644 pointer data_;
645 };
646
647 storage_type<extent_type<Extent>> storage_;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800648};
649
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700650// [span.comparison], span comparison operators
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800651template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700652constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
653{
654 return std::equal(l.begin(), l.end(), r.begin(), r.end());
655}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800656
657template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700658constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
659{
660 return !(l == r);
661}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800662
663template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700664constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
665{
666 return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
667}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800668
669template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700670constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
671{
672 return !(l > r);
673}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800674
675template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700676constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
677{
678 return r < l;
679}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800680
681template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700682constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
683{
684 return !(l < r);
685}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800686
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700687namespace details
688{
689 // if we only supported compilers with good constexpr support then
690 // this pair of classes could collapse down to a constexpr function
691
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700692 // we should use a narrow_cast<> to go to size_t, but older compilers may not see it as
693 // constexpr
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700694 // and so will fail compilation of the template
695 template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700696 struct calculate_byte_size
697 : std::integral_constant<std::ptrdiff_t,
698 static_cast<ptrdiff_t>(sizeof(ElementType) *
699 static_cast<size_t>(Extent))>
700 {
701 };
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700702
703 template <class ElementType>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700704 struct calculate_byte_size<ElementType, dynamic_extent>
705 : std::integral_constant<std::ptrdiff_t, dynamic_extent>
706 {
707 };
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700708}
709
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700710// [span.objectrep], views of object representation
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800711template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700712span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
713as_bytes(span<ElementType, Extent> s) noexcept
714{
715 return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
716}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800717
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700718template <class ElementType, ptrdiff_t Extent,
719 class = std::enable_if_t<!std::is_const<ElementType>::value>>
720span<byte, details::calculate_byte_size<ElementType, Extent>::value>
721as_writeable_bytes(span<ElementType, Extent> s) noexcept
722{
723 return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
724}
Neil MacIntoshcec26a22016-02-24 11:26:28 -0800725
726} // namespace gsl
727
728#ifdef _MSC_VER
729
730#undef constexpr
731#pragma pop_macro("constexpr")
732
733#if _MSC_VER <= 1800
734#pragma warning(pop)
735
736#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
737#undef noexcept
738#pragma pop_macro("noexcept")
739#endif // GSL_THROW_ON_CONTRACT_VIOLATION
740
741#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
742
743#endif // _MSC_VER <= 1800
744
745#endif // _MSC_VER
746
747#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
748
749#undef noexcept
750
751#ifdef _MSC_VER
752#pragma warning(pop)
753#pragma pop_macro("noexcept")
754#endif
755
756#endif // GSL_THROW_ON_CONTRACT_VIOLATION
757
758#endif // GSL_SPAN_H