blob: 6fe3bffdc9a74d5c8be48186afd2619a9145997a [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"
23#include "gsl_util.h"
Neil MacIntosh26747242016-06-26 17:00:56 +030024#include "gsl_byte.h"
Neil MacIntoshcec26a22016-02-24 11:26:28 -080025#include <array>
Neil MacIntoshcec26a22016-02-24 11:26:28 -080026#include <limits>
Neil MacIntoshd3929c52016-02-24 16:11:33 -080027#include <iterator>
Neil MacIntoshcec26a22016-02-24 11:26:28 -080028#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
73namespace gsl
74{
75
Neil MacIntoshc40094a2016-03-01 12:11:41 -080076template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
77class span;
78
79
Neil MacIntoshc94a66f2016-06-12 18:28:19 -070080// [views.constants], constants
81constexpr const std::ptrdiff_t dynamic_extent = -1;
82
83
84// implementation details
Neil MacIntoshc40094a2016-03-01 12:11:41 -080085namespace details
86{
87template <class T>
88struct is_span_oracle : std::false_type
89{
90};
91
92template <class ElementType, std::ptrdiff_t Extent>
93struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
94{
95};
96
97template <class T>
98struct is_span : is_span_oracle<std::remove_cv_t<T>>
99{
100};
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700101
102template <class From, class To>
103struct is_allowed_pointer_conversion
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700104 : std::bool_constant<
105 std::is_pointer<From>::value &&
106 std::is_pointer<To>::value &&
107 std::is_convertible<From, To>::value
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700108 >
109{
110};
111
112template <class From, class To>
113struct is_allowed_integral_conversion
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700114 : 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
124template <std::ptrdiff_t From, std::ptrdiff_t To>
125struct is_allowed_extent_conversion
126 : std::bool_constant<
127 From == To ||
128 From == gsl::dynamic_extent ||
129 To == gsl::dynamic_extent
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700130 >
131{
132};
133
134template <class From, class To>
135struct is_allowed_element_type_conversion
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700136 : 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 MacIntosh717a2e32016-03-16 19:39:55 -0700140 >
141{
142};
143
144template <class From>
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700145struct is_allowed_element_type_conversion<From, byte>
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700146 : std::bool_constant<!std::is_const<From>::value>
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700147{
148};
149
150template <class From>
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700151struct is_allowed_element_type_conversion<From, const byte>
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700152 : std::true_type
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700153{
154};
155
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700156template <class Span>
Neil MacIntosh26747242016-06-26 17:00:56 +0300157class const_span_iterator
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700158{
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700159public:
Neil MacIntosh26747242016-06-26 17:00:56 +0300160 using iterator_category = std::random_access_iterator_tag;
161 using value_type = typename Span::element_type;
162 using difference_type = std::ptrdiff_t;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700163
Neil MacIntosh26747242016-06-26 17:00:56 +0300164 using const_pointer = std::add_const_t<value_type*>;
165 using pointer = const_pointer;
166
167 using const_reference = std::add_const_t<value_type&>;
168 using reference = const_reference;
169
170 constexpr const_span_iterator() : const_span_iterator(nullptr, 0) {}
171 constexpr const_span_iterator(const Span* span, typename Span::index_type index) : span_(span), index_(index)
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700172 {
173 Expects(span == nullptr || (index_ >= 0 && index <= span_->length()));
174 }
175
Neil MacIntosh7b001722016-06-20 01:41:49 -0700176 constexpr reference operator*() const { Expects(span_); return (*span_)[index_]; }
177 constexpr pointer operator->() const { Expects(span_); return &((*span_)[index_]); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700178
Neil MacIntosh26747242016-06-26 17:00:56 +0300179 constexpr const_span_iterator& operator++() noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700180 {
181 Expects(span_ && index_ >= 0 && index_ < span_->length());
182 ++index_;
183 return *this;
184 }
185
Neil MacIntosh26747242016-06-26 17:00:56 +0300186 constexpr const_span_iterator operator++(int) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700187 {
188 auto ret = *this;
189 ++(*this);
190 return ret;
191 }
192
Neil MacIntosh26747242016-06-26 17:00:56 +0300193 constexpr const_span_iterator& operator--() noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700194 {
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700195 Expects(span_ && index_ > 0 && index_ <= span_->length());
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700196 --index_;
197 return *this;
198 }
199
Neil MacIntosh26747242016-06-26 17:00:56 +0300200 constexpr const_span_iterator operator--(int) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700201 {
202 auto ret = *this;
203 --(*this);
204 return ret;
205 }
206
Neil MacIntosh26747242016-06-26 17:00:56 +0300207 constexpr const_span_iterator operator+(difference_type n) const noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700208 {
209 auto ret{*this};
210 return ret += n;
211 }
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700212
Neil MacIntosh26747242016-06-26 17:00:56 +0300213 constexpr const_span_iterator& operator+=(difference_type n) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700214 {
Neil MacIntosh520c72d2016-07-18 12:00:33 -0700215 Expects(span_ && (index_ + n) >= 0 && (index_ + n) <= span_->length());
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700216 index_ += n;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700217 return *this;
218 }
219
Neil MacIntosh26747242016-06-26 17:00:56 +0300220 constexpr const_span_iterator operator-(difference_type n) const noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700221 {
222 auto ret{*this};
223 return ret -= n;
224 }
225
Neil MacIntosh26747242016-06-26 17:00:56 +0300226 constexpr const_span_iterator& operator-=(difference_type n) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700227 {
228 return *this += -n;
229 }
230
Neil MacIntosh26747242016-06-26 17:00:56 +0300231 constexpr difference_type operator-(const const_span_iterator& rhs) const noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700232 {
233 Expects(span_ == rhs.span_);
234 return index_ - rhs.index_;
235 }
236
Neil MacIntosh7b001722016-06-20 01:41:49 -0700237 constexpr reference operator[](difference_type n) const noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700238 {
239 return *(*this + n);
240 }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700241
Neil MacIntosh26747242016-06-26 17:00:56 +0300242 constexpr bool operator==(const const_span_iterator& rhs) const noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700243 {
244 return span_ == rhs.span_ && index_ == rhs.index_;
245 }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700246
Neil MacIntosh26747242016-06-26 17:00:56 +0300247 constexpr bool operator!=(const const_span_iterator& rhs) const noexcept { return !(*this == rhs); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700248
Neil MacIntosh26747242016-06-26 17:00:56 +0300249 constexpr bool operator<(const const_span_iterator& rhs) const noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700250 {
251 Expects(span_ == rhs.span_);
252 return index_ < rhs.index_;
253 }
254
Neil MacIntosh26747242016-06-26 17:00:56 +0300255 constexpr bool operator<=(const const_span_iterator& rhs) const noexcept { return !(rhs < *this); }
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700256
Neil MacIntosh26747242016-06-26 17:00:56 +0300257 constexpr bool operator>(const const_span_iterator& rhs) const noexcept { return rhs < *this; }
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700258
Neil MacIntosh26747242016-06-26 17:00:56 +0300259 constexpr bool operator>=(const const_span_iterator& rhs) const noexcept { return !(rhs > *this); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700260
Neil MacIntosh26747242016-06-26 17:00:56 +0300261 void swap(const_span_iterator& rhs) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700262 {
263 std::swap(index_, rhs.index_);
264 std::swap(m_span, rhs.m_span);
265 }
266
267private:
268 const Span* span_;
269 ptrdiff_t index_;
270};
271
Neil MacIntosh26747242016-06-26 17:00:56 +0300272
273template <class Span>
274class span_iterator : public const_span_iterator<Span>
275{
276 using base_type = const_span_iterator<Span>;
277
278public:
279 using iterator_category = std::random_access_iterator_tag;
280 using value_type = typename Span::element_type;
281 using difference_type = std::ptrdiff_t;
282
283 using pointer = value_type*;
284 using reference = value_type&;
285
286 constexpr span_iterator() : base_type() {}
287 constexpr span_iterator(const Span* span, typename Span::index_type index) : base_type(span, index) {}
288
289 constexpr reference operator*() const { return reinterpret_cast<reference>(base_type::operator*()); }
290 constexpr pointer operator->() const { return reinterpret_cast<pointer>(base_type::operator->()); }
291
292 constexpr span_iterator& operator++() noexcept { base_type::operator++(); return *this; }
293
294 constexpr span_iterator operator++(int) noexcept { return base_type::operator++(1); }
295
296 constexpr span_iterator& operator--() noexcept { base_type::operator--(); return *this; }
297
298 constexpr span_iterator operator--(int) noexcept { return base_type::operator--(1); }
299
300 constexpr span_iterator operator+(difference_type n) const noexcept { return base_type::operator+(n); }
301
302 constexpr span_iterator& operator+=(difference_type n) noexcept { return base_type::operator+=(n); }
303
304 constexpr span_iterator operator-(difference_type n) const noexcept { return base_type::operator-(n); }
305
306 constexpr span_iterator& operator-=(difference_type n) noexcept { return base_type::operator-=(n); }
307
308 constexpr difference_type operator-(const span_iterator& rhs) const noexcept { return base_type::operator-(rhs); }
309
310 constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); }
311
312 constexpr bool operator==(const span_iterator& rhs) const noexcept { return base_type::operator==(rhs); }
313
314 constexpr bool operator!=(const span_iterator& rhs) const noexcept { return !(*this == rhs); }
315
316 constexpr bool operator<(const span_iterator& rhs) const noexcept { return base_type::operator<(rhs); }
317
318 constexpr bool operator<=(const span_iterator& rhs) const noexcept { return !(rhs < *this); }
319
320 constexpr bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; }
321
322 constexpr bool operator>=(const span_iterator& rhs) const noexcept { return !(rhs > *this); }
323
324 void swap(span_iterator& rhs) noexcept { base_type::swap(rhs); }
325};
326
327template <typename Span>
328constexpr const_span_iterator<Span> operator+(typename const_span_iterator<Span>::difference_type n,
329 const const_span_iterator<Span>& rhs) noexcept
330{
331 return rhs + n;
332}
333
334template <typename Span>
335constexpr const_span_iterator<Span> operator-(typename const_span_iterator<Span>::difference_type n,
336 const const_span_iterator<Span>& rhs) noexcept
337{
338 return rhs - n;
339}
340
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700341template <typename Span>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700342constexpr span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n,
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700343 const span_iterator<Span>& rhs) noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700344{
345 return rhs + n;
346}
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700347
348template <typename Span>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700349constexpr span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n,
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700350 const span_iterator<Span>& rhs) noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700351{
352 return rhs - n;
353}
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700354
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800355} // namespace details
356
357
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800358// [span], class template span
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800359template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800360class span {
361public:
362 // constants and types
363 using element_type = ElementType;
364 using index_type = std::ptrdiff_t;
365 using pointer = element_type*;
366 using reference = element_type&;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700367
368 using iterator = details::span_iterator<span<ElementType, Extent>>;
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700369 using const_iterator = details::const_span_iterator<span>;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800370 using reverse_iterator = std::reverse_iterator<iterator>;
Neil MacIntosh26747242016-06-26 17:00:56 +0300371 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700372
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800373 constexpr static const index_type extent = Extent;
374
375 // [span.cons], span constructors, copy, assignment, and destructor
Neil MacIntosh502cd662016-02-28 00:50:53 -0800376 constexpr span() noexcept : storage_(nullptr, extent_type<0>())
377 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800378
379 constexpr span(nullptr_t) noexcept : span()
380 {}
381
Neil MacIntosh502cd662016-02-28 00:50:53 -0800382 constexpr span(pointer ptr, index_type count) : storage_(ptr, count)
Neil MacIntosh25ff7ec2016-05-29 13:54:19 -0700383 { Expects((!ptr && count == 0) || (ptr && count >= 0)); }
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800384
Neil MacIntosh502cd662016-02-28 00:50:53 -0800385 constexpr span(pointer firstElem, pointer lastElem)
386 : storage_(firstElem, std::distance(firstElem, lastElem))
387 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800388
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800389 template <size_t N>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700390 constexpr span(element_type(&arr)[N]) noexcept
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800391 : storage_(&arr[0], extent_type<N>())
392 {}
393
394 template <size_t N>
Neil MacIntosh62f30202016-06-14 20:14:17 -0700395 constexpr span(std::array<element_type, N>& arr)
396 : storage_(&arr[0], extent_type<N>())
397 {}
398
399 template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800400 constexpr span(std::array<std::remove_const_t<element_type>, N>& arr)
401 : storage_(&arr[0], extent_type<N>())
402 {}
403
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700404 template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800405 constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr)
406 : storage_(&arr[0], extent_type<N>())
Neil MacIntosh502cd662016-02-28 00:50:53 -0800407 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800408
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800409 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
410 // on Container to be a contiguous sequence container.
411 template <class Container,
412 class = std::enable_if_t<!details::is_span<Container>::value &&
413 std::is_convertible<Container::pointer, pointer>::value &&
414 std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
415 >
416 constexpr span(Container& cont) : span(cont.data(), cont.size()) {}
417
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800418 template <class Container,
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700419 class = std::enable_if_t<std::is_const<element_type>::value &&
420 !details::is_span<Container>::value &&
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800421 std::is_convertible<Container::pointer, pointer>::value &&
422 std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
423 >
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700424 constexpr span(const Container& cont) : span(cont.data(), cont.size()) {}
425
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800426 constexpr span(const span& other) noexcept = default;
427 constexpr span(span&& other) noexcept = default;
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700428
429 template <class OtherElementType, std::ptrdiff_t OtherExtent,
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700430 class = std::enable_if_t<
431 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700432 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
433 >
434 >
435 constexpr span(const span<OtherElementType, OtherExtent>& other)
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700436 : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700437 {}
438
439 template <class OtherElementType, std::ptrdiff_t OtherExtent,
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700440 class = std::enable_if_t<
441 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700442 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
443 >
444 >
445 constexpr span(span<OtherElementType, OtherExtent>&& other)
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700446 : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700447 {}
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700448
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800449 ~span() noexcept = default;
450 constexpr span& operator=(const span& other) noexcept = default;
451 constexpr span& operator=(span&& other) noexcept = default;
452
453 // [span.sub], span subviews
454 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700455 constexpr span<element_type, Count> first() const
456 {
457 Expects(Count >= 0 && Count <= size());
458 return { data(), Count };
459 }
460
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800461 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700462 constexpr span<element_type, Count> last() const
463 {
464 Expects(Count >= 0 && Count <= size());
Neil MacIntosh64598bc2016-06-14 20:20:09 -0700465 return{ data() + (size() - Count), Count };
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700466 }
467
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800468 template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700469 constexpr span<element_type, Count> subspan() const
470 {
471 Expects((Offset == 0 || Offset > 0 && Offset <= size()) &&
472 (Count == dynamic_extent || Count >= 0 && Offset + Count <= size()));
473 return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };
474 }
475
476 constexpr span<element_type, dynamic_extent> first(index_type count) const
477 {
478 Expects(count >= 0 && count <= size());
479 return { data(), count };
480 }
481
482 constexpr span<element_type, dynamic_extent> last(index_type count) const
483 {
484 Expects(count >= 0 && count <= size());
Neil MacIntosh64598bc2016-06-14 20:20:09 -0700485 return { data() + (size() - count), count };
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700486 }
487
488 constexpr span<element_type, dynamic_extent> subspan(index_type offset,
489 index_type count = dynamic_extent) const
490 {
491 Expects((offset == 0 || offset > 0 && offset <= size()) &&
492 (count == dynamic_extent || count >= 0 && offset + count <= size()));
493 return { data() + offset, count == dynamic_extent ? size() - offset : count };
494 }
495
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800496 // [span.obs], span observers
497 constexpr index_type length() const noexcept { return size(); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800498 constexpr index_type size() const noexcept { return storage_.size(); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800499 constexpr index_type length_bytes() const noexcept { return size_bytes(); }
500 constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); }
501 constexpr bool empty() const noexcept { return size() == 0; }
502
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800503 // [span.elem], span element access
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800504 constexpr reference operator[](index_type idx) const
505 {
Neil MacIntosh502cd662016-02-28 00:50:53 -0800506 Expects(idx >= 0 && idx < storage_.size());
507 return storage_.data()[idx];
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800508 }
509 constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800510 constexpr pointer data() const noexcept { return storage_.data(); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700511
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800512 // [span.iter], span iterator support
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700513 iterator begin() const noexcept { return {this, 0}; }
514 iterator end() const noexcept { return {this, length()}; }
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700515
516 const_iterator cbegin() const noexcept { return {this, 0}; }
517 const_iterator cend() const noexcept { return {this, length()}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800518
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700519 reverse_iterator rbegin() const noexcept { return reverse_iterator{{this, length()}}; }
520 reverse_iterator rend() const noexcept { return reverse_iterator{{this, 0}}; }
521
522 const_reverse_iterator crbegin() const noexcept { return reverse_iterator{{this, length()}}; }
523 const_reverse_iterator crend() const noexcept { return reverse_iterator{{this, 0}}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800524
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800525private:
Neil MacIntosh502cd662016-02-28 00:50:53 -0800526 template <index_type Extent>
527 class extent_type;
528
529 template <index_type Extent>
530 class extent_type
531 {
532 public:
533 static_assert(Extent >= 0, "A fixed-size span must be >= 0 in size.");
534
535 constexpr extent_type() noexcept {}
536
537 template <index_type Other>
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700538 constexpr extent_type(extent_type<Other> ext) noexcept
Neil MacIntosh502cd662016-02-28 00:50:53 -0800539 {
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700540 static_assert(Other == Extent || Other == dynamic_extent,
Neil MacIntosh502cd662016-02-28 00:50:53 -0800541 "Mismatch between fixed-size extent and size of initializing data.");
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700542 Expects(ext.size() == Extent);
Neil MacIntosh502cd662016-02-28 00:50:53 -0800543 }
544
545 constexpr extent_type(index_type size)
546 { Expects(size == Extent); }
547
548 constexpr inline index_type size() const noexcept { return Extent; }
549 };
550
551 template <>
552 class extent_type<dynamic_extent>
553 {
554 public:
555 template <index_type Other>
556 explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
557 {}
558
559 explicit constexpr extent_type(index_type size) : size_(size)
560 { Expects(size >= 0); }
561
562 constexpr inline index_type size() const noexcept
563 { return size_; }
564
565 private:
566 index_type size_;
567 };
568
569 // this implementation detail class lets us take advantage of the
570 // empty base class optimization to pay for only storage of a single
571 // pointer in the case of fixed-size spans
572 template <class ExtentType>
573 class storage_type : public ExtentType
574 {
575 public:
576 template <class OtherExtentType>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700577 constexpr storage_type(pointer data, OtherExtentType ext)
Neil MacIntosh502cd662016-02-28 00:50:53 -0800578 : ExtentType(ext), data_(data) {}
579
Neil MacIntosh502cd662016-02-28 00:50:53 -0800580 constexpr inline pointer data() const noexcept
581 { return data_; }
582
583 private:
584 pointer data_;
585 };
586
587 storage_type<extent_type<Extent>> storage_;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800588};
589
590
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800591// [span.comparison], span comparison operators
592template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700593constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700594{ return std::equal(l.begin(), l.end(), r.begin(), r.end()); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800595
596template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700597constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700598{ return !(l == r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800599
600template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700601constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700602{ return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800603
604template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700605constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700606{ return !(l > r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800607
608template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700609constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700610{ return r < l; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800611
612template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700613constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700614{ return !(l < r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800615
616
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700617namespace details
618{
619 // if we only supported compilers with good constexpr support then
620 // this pair of classes could collapse down to a constexpr function
621
622 // we should use a narrow_cast<> to go to size_t, but older compilers may not see it as constexpr
623 // and so will fail compilation of the template
624 template <class ElementType, ptrdiff_t Extent>
625 struct calculate_byte_size :
626 std::integral_constant<std::ptrdiff_t, static_cast<ptrdiff_t>(sizeof(ElementType) * static_cast<size_t>(Extent))>
627 {};
628
629 template <class ElementType>
630 struct calculate_byte_size<ElementType, dynamic_extent> :
631 std::integral_constant<std::ptrdiff_t, dynamic_extent>
632 {};
633}
634
635
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800636// [span.objectrep], views of object representation
637template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700638span<const byte, details::calculate_byte_size<ElementType, Extent>::value> as_bytes(span<ElementType, Extent> s) noexcept
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700639{ return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800640
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700641template <class ElementType, ptrdiff_t Extent, class = std::enable_if_t<!std::is_const<ElementType>::value>>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700642span<byte, details::calculate_byte_size<ElementType, Extent>::value> as_writeable_bytes(span<ElementType, Extent> s) noexcept
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700643{ return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; }
Neil MacIntoshcec26a22016-02-24 11:26:28 -0800644
645} // namespace gsl
646
647#ifdef _MSC_VER
648
649#undef constexpr
650#pragma pop_macro("constexpr")
651
652#if _MSC_VER <= 1800
653#pragma warning(pop)
654
655#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
656#undef noexcept
657#pragma pop_macro("noexcept")
658#endif // GSL_THROW_ON_CONTRACT_VIOLATION
659
660#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
661
662#endif // _MSC_VER <= 1800
663
664#endif // _MSC_VER
665
666#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
667
668#undef noexcept
669
670#ifdef _MSC_VER
671#pragma warning(pop)
672#pragma pop_macro("noexcept")
673#endif
674
675#endif // GSL_THROW_ON_CONTRACT_VIOLATION
676
677#endif // GSL_SPAN_H