blob: 676fdf22067bf7535d7feaa1fb2b750a0a0b404c [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 MacIntoshba8ebef2016-05-29 17:06:29 -070024#include "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>
157class span_iterator
158 : public std::iterator<std::random_access_iterator_tag, typename Span::element_type>
159{
160 using Base = std::iterator<std::random_access_iterator_tag, typename Span::element_type>;
161
162public:
163 using typename Base::reference;
164 using typename Base::pointer;
165 using typename Base::difference_type;
166
167 span_iterator() : span_iterator(nullptr, 0) {}
168 span_iterator(const Span* span, typename Span::index_type index) : span_(span), index_(index)
169 {
170 Expects(span == nullptr || (index_ >= 0 && index <= span_->length()));
171 }
172
173 reference operator*() const { Expects(span_); return (*span_)[index_]; }
174 pointer operator->() const { Expects(span_); return &((*span_)[index_]); }
175
176 span_iterator& operator++() noexcept
177 {
178 Expects(span_ && index_ >= 0 && index_ < span_->length());
179 ++index_;
180 return *this;
181 }
182
183 span_iterator operator++(int) noexcept
184 {
185 auto ret = *this;
186 ++(*this);
187 return ret;
188 }
189
190 span_iterator& operator--() noexcept
191 {
192 Expects(span_ && index > 0 && index_ <= span_->length());
193 --index_;
194 return *this;
195 }
196
197 span_iterator operator--(int) noexcept
198 {
199 auto ret = *this;
200 --(*this);
201 return ret;
202 }
203
204 span_iterator operator+(difference_type n) const noexcept
205 {
206 auto ret{*this};
207 return ret += n;
208 }
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700209
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700210 span_iterator& operator+=(difference_type n) noexcept
211 {
212 index_ += n;
213 Expects(span_ && index_ >= 0 && index_ <= span_->length());
214 return *this;
215 }
216
217 span_iterator operator-(difference_type n) const noexcept
218 {
219 auto ret{*this};
220 return ret -= n;
221 }
222
223 span_iterator& operator-=(difference_type n) noexcept
224 {
225 return *this += -n;
226 }
227
228 difference_type operator-(const span_iterator& rhs) const noexcept
229 {
230 Expects(span_ == rhs.span_);
231 return index_ - rhs.index_;
232 }
233
234 reference operator[](difference_type n) const noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700235 {
236 return *(*this + n);
237 }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700238
239 bool operator==(const span_iterator& rhs) const noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700240 {
241 return span_ == rhs.span_ && index_ == rhs.index_;
242 }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700243
244 bool operator!=(const span_iterator& rhs) const noexcept { return !(*this == rhs); }
245
246 bool operator<(const span_iterator& rhs) const noexcept
247 {
248 Expects(span_ == rhs.span_);
249 return index_ < rhs.index_;
250 }
251
252 bool operator<=(const span_iterator& rhs) const noexcept { return !(rhs < *this); }
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700253
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700254 bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; }
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700255
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700256 bool operator>=(const span_iterator& rhs) const noexcept { return !(rhs > *this); }
257
258 void swap(span_iterator& rhs) noexcept
259 {
260 std::swap(index_, rhs.index_);
261 std::swap(m_span, rhs.m_span);
262 }
263
264private:
265 const Span* span_;
266 ptrdiff_t index_;
267};
268
269template <typename Span>
270span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n,
271 const span_iterator<Span>& rhs) noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700272{
273 return rhs + n;
274}
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700275
276template <typename Span>
277span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n,
278 const span_iterator<Span>& rhs) noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700279{
280 return rhs - n;
281}
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700282
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800283} // namespace details
284
285
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800286// [span], class template span
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800287template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800288class span {
289public:
290 // constants and types
291 using element_type = ElementType;
292 using index_type = std::ptrdiff_t;
293 using pointer = element_type*;
294 using reference = element_type&;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700295
296 using iterator = details::span_iterator<span<ElementType, Extent>>;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800297 using reverse_iterator = std::reverse_iterator<iterator>;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700298
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800299 constexpr static const index_type extent = Extent;
300
301 // [span.cons], span constructors, copy, assignment, and destructor
Neil MacIntosh502cd662016-02-28 00:50:53 -0800302 constexpr span() noexcept : storage_(nullptr, extent_type<0>())
303 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800304
305 constexpr span(nullptr_t) noexcept : span()
306 {}
307
Neil MacIntosh502cd662016-02-28 00:50:53 -0800308 constexpr span(pointer ptr, index_type count) : storage_(ptr, count)
Neil MacIntosh25ff7ec2016-05-29 13:54:19 -0700309 { Expects((!ptr && count == 0) || (ptr && count >= 0)); }
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800310
Neil MacIntosh502cd662016-02-28 00:50:53 -0800311 constexpr span(pointer firstElem, pointer lastElem)
312 : storage_(firstElem, std::distance(firstElem, lastElem))
313 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800314
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800315 template <size_t N>
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800316 constexpr span(element_type(&arr)[N])
317 : storage_(&arr[0], extent_type<N>())
318 {}
319
320 template <size_t N>
321 constexpr span(std::array<std::remove_const_t<element_type>, N>& arr)
322 : storage_(&arr[0], extent_type<N>())
323 {}
324
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700325 template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800326 constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr)
327 : storage_(&arr[0], extent_type<N>())
Neil MacIntosh502cd662016-02-28 00:50:53 -0800328 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800329
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800330 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
331 // on Container to be a contiguous sequence container.
332 template <class Container,
333 class = std::enable_if_t<!details::is_span<Container>::value &&
334 std::is_convertible<Container::pointer, pointer>::value &&
335 std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
336 >
337 constexpr span(Container& cont) : span(cont.data(), cont.size()) {}
338
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800339 template <class Container,
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700340 class = std::enable_if_t<std::is_const<element_type>::value &&
341 !details::is_span<Container>::value &&
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800342 std::is_convertible<Container::pointer, pointer>::value &&
343 std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
344 >
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700345 constexpr span(const Container& cont) : span(cont.data(), cont.size()) {}
346
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800347 constexpr span(const span& other) noexcept = default;
348 constexpr span(span&& other) noexcept = default;
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700349
350 template <class OtherElementType, std::ptrdiff_t OtherExtent,
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700351 class = std::enable_if_t<
352 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700353 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
354 >
355 >
356 constexpr span(const span<OtherElementType, OtherExtent>& other)
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700357 : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700358 {}
359
360 template <class OtherElementType, std::ptrdiff_t OtherExtent,
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700361 class = std::enable_if_t<
362 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700363 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
364 >
365 >
366 constexpr span(span<OtherElementType, OtherExtent>&& other)
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700367 : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700368 {}
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700369
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800370 ~span() noexcept = default;
371 constexpr span& operator=(const span& other) noexcept = default;
372 constexpr span& operator=(span&& other) noexcept = default;
373
374 // [span.sub], span subviews
375 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700376 constexpr span<element_type, Count> first() const
377 {
378 Expects(Count >= 0 && Count <= size());
379 return { data(), Count };
380 }
381
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800382 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700383 constexpr span<element_type, Count> last() const
384 {
385 Expects(Count >= 0 && Count <= size());
386 return{ Count == 0 ? data() : data() + (size() - Count), Count };
387 }
388
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800389 template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700390 constexpr span<element_type, Count> subspan() const
391 {
392 Expects((Offset == 0 || Offset > 0 && Offset <= size()) &&
393 (Count == dynamic_extent || Count >= 0 && Offset + Count <= size()));
394 return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };
395 }
396
397 constexpr span<element_type, dynamic_extent> first(index_type count) const
398 {
399 Expects(count >= 0 && count <= size());
400 return { data(), count };
401 }
402
403 constexpr span<element_type, dynamic_extent> last(index_type count) const
404 {
405 Expects(count >= 0 && count <= size());
406 return { count == 0 ? data() : data() + (size() - count), count };
407 }
408
409 constexpr span<element_type, dynamic_extent> subspan(index_type offset,
410 index_type count = dynamic_extent) const
411 {
412 Expects((offset == 0 || offset > 0 && offset <= size()) &&
413 (count == dynamic_extent || count >= 0 && offset + count <= size()));
414 return { data() + offset, count == dynamic_extent ? size() - offset : count };
415 }
416
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800417 // [span.obs], span observers
418 constexpr index_type length() const noexcept { return size(); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800419 constexpr index_type size() const noexcept { return storage_.size(); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800420 constexpr index_type length_bytes() const noexcept { return size_bytes(); }
421 constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); }
422 constexpr bool empty() const noexcept { return size() == 0; }
423
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800424 // [span.elem], span element access
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800425 constexpr reference operator[](index_type idx) const
426 {
Neil MacIntosh502cd662016-02-28 00:50:53 -0800427 Expects(idx >= 0 && idx < storage_.size());
428 return storage_.data()[idx];
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800429 }
430 constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800431 constexpr pointer data() const noexcept { return storage_.data(); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700432
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800433 // [span.iter], span iterator support
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700434 iterator begin() const noexcept { return {this, 0}; }
435 iterator end() const noexcept { return {this, length()}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800436
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700437 reverse_iterator rbegin() const noexcept { return {this, length()}; }
438 reverse_iterator rend() const noexcept { return {this, 0}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800439
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800440private:
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700441 constexpr static const bool is_span_type = true;
442
Neil MacIntosh502cd662016-02-28 00:50:53 -0800443 template <index_type Extent>
444 class extent_type;
445
446 template <index_type Extent>
447 class extent_type
448 {
449 public:
450 static_assert(Extent >= 0, "A fixed-size span must be >= 0 in size.");
451
452 constexpr extent_type() noexcept {}
453
454 template <index_type Other>
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700455 constexpr extent_type(extent_type<Other> ext) noexcept
Neil MacIntosh502cd662016-02-28 00:50:53 -0800456 {
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700457 static_assert(Other == Extent || Other == dynamic_extent,
Neil MacIntosh502cd662016-02-28 00:50:53 -0800458 "Mismatch between fixed-size extent and size of initializing data.");
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700459 Expects(ext.size() == Extent);
Neil MacIntosh502cd662016-02-28 00:50:53 -0800460 }
461
462 constexpr extent_type(index_type size)
463 { Expects(size == Extent); }
464
465 constexpr inline index_type size() const noexcept { return Extent; }
466 };
467
468 template <>
469 class extent_type<dynamic_extent>
470 {
471 public:
472 template <index_type Other>
473 explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
474 {}
475
476 explicit constexpr extent_type(index_type size) : size_(size)
477 { Expects(size >= 0); }
478
479 constexpr inline index_type size() const noexcept
480 { return size_; }
481
482 private:
483 index_type size_;
484 };
485
486 // this implementation detail class lets us take advantage of the
487 // empty base class optimization to pay for only storage of a single
488 // pointer in the case of fixed-size spans
489 template <class ExtentType>
490 class storage_type : public ExtentType
491 {
492 public:
493 template <class OtherExtentType>
494 storage_type(pointer data, OtherExtentType ext)
495 : ExtentType(ext), data_(data) {}
496
Neil MacIntosh502cd662016-02-28 00:50:53 -0800497 constexpr inline pointer data() const noexcept
498 { return data_; }
499
500 private:
501 pointer data_;
502 };
503
504 storage_type<extent_type<Extent>> storage_;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800505};
506
507
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800508// [span.comparison], span comparison operators
509template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700510constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
511{ return std::equal(l.begin(), l.end(), r.begin(), r.end()); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800512
513template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700514constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
515{ return !(l == r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800516
517template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700518constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
519{ return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800520
521template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700522constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
523{ return !(l > r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800524
525template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700526constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
527{ return r < l; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800528
529template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700530constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
531{ return !(l < r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800532
533
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700534namespace details
535{
536 // if we only supported compilers with good constexpr support then
537 // this pair of classes could collapse down to a constexpr function
538
539 // we should use a narrow_cast<> to go to size_t, but older compilers may not see it as constexpr
540 // and so will fail compilation of the template
541 template <class ElementType, ptrdiff_t Extent>
542 struct calculate_byte_size :
543 std::integral_constant<std::ptrdiff_t, static_cast<ptrdiff_t>(sizeof(ElementType) * static_cast<size_t>(Extent))>
544 {};
545
546 template <class ElementType>
547 struct calculate_byte_size<ElementType, dynamic_extent> :
548 std::integral_constant<std::ptrdiff_t, dynamic_extent>
549 {};
550}
551
552
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800553// [span.objectrep], views of object representation
554template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700555constexpr span<const byte, details::calculate_byte_size<ElementType, Extent>::value> as_bytes(span<ElementType, Extent> s) noexcept
556{ return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800557
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700558template <class ElementType, ptrdiff_t Extent, class = std::enable_if_t<!std::is_const<ElementType>::value>>
559constexpr span<byte, details::calculate_byte_size<ElementType, Extent>::value> as_writeable_bytes(span<ElementType, Extent> s) noexcept
560{ return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; }
Neil MacIntoshcec26a22016-02-24 11:26:28 -0800561
562} // namespace gsl
563
564#ifdef _MSC_VER
565
566#undef constexpr
567#pragma pop_macro("constexpr")
568
569#if _MSC_VER <= 1800
570#pragma warning(pop)
571
572#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
573#undef noexcept
574#pragma pop_macro("noexcept")
575#endif // GSL_THROW_ON_CONTRACT_VIOLATION
576
577#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
578
579#endif // _MSC_VER <= 1800
580
581#endif // _MSC_VER
582
583#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
584
585#undef noexcept
586
587#ifdef _MSC_VER
588#pragma warning(pop)
589#pragma pop_macro("noexcept")
590#endif
591
592#endif // GSL_THROW_ON_CONTRACT_VIOLATION
593
594#endif // GSL_SPAN_H