blob: 3e0ccc907faa283e52853786158954e244855c82 [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>
Neil MacIntosh62f30202016-06-14 20:14:17 -0700321 constexpr span(std::array<element_type, N>& arr)
322 : storage_(&arr[0], extent_type<N>())
323 {}
324
325 template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800326 constexpr span(std::array<std::remove_const_t<element_type>, N>& arr)
327 : storage_(&arr[0], extent_type<N>())
328 {}
329
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700330 template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800331 constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr)
332 : storage_(&arr[0], extent_type<N>())
Neil MacIntosh502cd662016-02-28 00:50:53 -0800333 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800334
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800335 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
336 // on Container to be a contiguous sequence container.
337 template <class Container,
338 class = std::enable_if_t<!details::is_span<Container>::value &&
339 std::is_convertible<Container::pointer, pointer>::value &&
340 std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
341 >
342 constexpr span(Container& cont) : span(cont.data(), cont.size()) {}
343
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800344 template <class Container,
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700345 class = std::enable_if_t<std::is_const<element_type>::value &&
346 !details::is_span<Container>::value &&
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800347 std::is_convertible<Container::pointer, pointer>::value &&
348 std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
349 >
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700350 constexpr span(const Container& cont) : span(cont.data(), cont.size()) {}
351
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800352 constexpr span(const span& other) noexcept = default;
353 constexpr span(span&& other) noexcept = default;
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700354
355 template <class OtherElementType, std::ptrdiff_t OtherExtent,
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700356 class = std::enable_if_t<
357 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700358 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
359 >
360 >
361 constexpr span(const span<OtherElementType, OtherExtent>& other)
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700362 : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700363 {}
364
365 template <class OtherElementType, std::ptrdiff_t OtherExtent,
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700366 class = std::enable_if_t<
367 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700368 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
369 >
370 >
371 constexpr span(span<OtherElementType, OtherExtent>&& other)
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700372 : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700373 {}
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700374
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800375 ~span() noexcept = default;
376 constexpr span& operator=(const span& other) noexcept = default;
377 constexpr span& operator=(span&& other) noexcept = default;
378
379 // [span.sub], span subviews
380 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700381 constexpr span<element_type, Count> first() const
382 {
383 Expects(Count >= 0 && Count <= size());
384 return { data(), Count };
385 }
386
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800387 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700388 constexpr span<element_type, Count> last() const
389 {
390 Expects(Count >= 0 && Count <= size());
391 return{ Count == 0 ? data() : data() + (size() - Count), Count };
392 }
393
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800394 template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700395 constexpr span<element_type, Count> subspan() const
396 {
397 Expects((Offset == 0 || Offset > 0 && Offset <= size()) &&
398 (Count == dynamic_extent || Count >= 0 && Offset + Count <= size()));
399 return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };
400 }
401
402 constexpr span<element_type, dynamic_extent> first(index_type count) const
403 {
404 Expects(count >= 0 && count <= size());
405 return { data(), count };
406 }
407
408 constexpr span<element_type, dynamic_extent> last(index_type count) const
409 {
410 Expects(count >= 0 && count <= size());
411 return { count == 0 ? data() : data() + (size() - count), count };
412 }
413
414 constexpr span<element_type, dynamic_extent> subspan(index_type offset,
415 index_type count = dynamic_extent) const
416 {
417 Expects((offset == 0 || offset > 0 && offset <= size()) &&
418 (count == dynamic_extent || count >= 0 && offset + count <= size()));
419 return { data() + offset, count == dynamic_extent ? size() - offset : count };
420 }
421
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800422 // [span.obs], span observers
423 constexpr index_type length() const noexcept { return size(); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800424 constexpr index_type size() const noexcept { return storage_.size(); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800425 constexpr index_type length_bytes() const noexcept { return size_bytes(); }
426 constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); }
427 constexpr bool empty() const noexcept { return size() == 0; }
428
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800429 // [span.elem], span element access
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800430 constexpr reference operator[](index_type idx) const
431 {
Neil MacIntosh502cd662016-02-28 00:50:53 -0800432 Expects(idx >= 0 && idx < storage_.size());
433 return storage_.data()[idx];
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800434 }
435 constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800436 constexpr pointer data() const noexcept { return storage_.data(); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700437
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800438 // [span.iter], span iterator support
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700439 iterator begin() const noexcept { return {this, 0}; }
440 iterator end() const noexcept { return {this, length()}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800441
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700442 reverse_iterator rbegin() const noexcept { return {this, length()}; }
443 reverse_iterator rend() const noexcept { return {this, 0}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800444
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800445private:
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700446 constexpr static const bool is_span_type = true;
447
Neil MacIntosh502cd662016-02-28 00:50:53 -0800448 template <index_type Extent>
449 class extent_type;
450
451 template <index_type Extent>
452 class extent_type
453 {
454 public:
455 static_assert(Extent >= 0, "A fixed-size span must be >= 0 in size.");
456
457 constexpr extent_type() noexcept {}
458
459 template <index_type Other>
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700460 constexpr extent_type(extent_type<Other> ext) noexcept
Neil MacIntosh502cd662016-02-28 00:50:53 -0800461 {
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700462 static_assert(Other == Extent || Other == dynamic_extent,
Neil MacIntosh502cd662016-02-28 00:50:53 -0800463 "Mismatch between fixed-size extent and size of initializing data.");
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700464 Expects(ext.size() == Extent);
Neil MacIntosh502cd662016-02-28 00:50:53 -0800465 }
466
467 constexpr extent_type(index_type size)
468 { Expects(size == Extent); }
469
470 constexpr inline index_type size() const noexcept { return Extent; }
471 };
472
473 template <>
474 class extent_type<dynamic_extent>
475 {
476 public:
477 template <index_type Other>
478 explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
479 {}
480
481 explicit constexpr extent_type(index_type size) : size_(size)
482 { Expects(size >= 0); }
483
484 constexpr inline index_type size() const noexcept
485 { return size_; }
486
487 private:
488 index_type size_;
489 };
490
491 // this implementation detail class lets us take advantage of the
492 // empty base class optimization to pay for only storage of a single
493 // pointer in the case of fixed-size spans
494 template <class ExtentType>
495 class storage_type : public ExtentType
496 {
497 public:
498 template <class OtherExtentType>
499 storage_type(pointer data, OtherExtentType ext)
500 : ExtentType(ext), data_(data) {}
501
Neil MacIntosh502cd662016-02-28 00:50:53 -0800502 constexpr inline pointer data() const noexcept
503 { return data_; }
504
505 private:
506 pointer data_;
507 };
508
509 storage_type<extent_type<Extent>> storage_;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800510};
511
512
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800513// [span.comparison], span comparison operators
514template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700515constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
516{ return std::equal(l.begin(), l.end(), r.begin(), r.end()); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800517
518template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700519constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
520{ return !(l == r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800521
522template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700523constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
524{ return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800525
526template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700527constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
528{ return !(l > r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800529
530template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700531constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
532{ return r < l; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800533
534template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700535constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
536{ return !(l < r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800537
538
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700539namespace details
540{
541 // if we only supported compilers with good constexpr support then
542 // this pair of classes could collapse down to a constexpr function
543
544 // we should use a narrow_cast<> to go to size_t, but older compilers may not see it as constexpr
545 // and so will fail compilation of the template
546 template <class ElementType, ptrdiff_t Extent>
547 struct calculate_byte_size :
548 std::integral_constant<std::ptrdiff_t, static_cast<ptrdiff_t>(sizeof(ElementType) * static_cast<size_t>(Extent))>
549 {};
550
551 template <class ElementType>
552 struct calculate_byte_size<ElementType, dynamic_extent> :
553 std::integral_constant<std::ptrdiff_t, dynamic_extent>
554 {};
555}
556
557
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800558// [span.objectrep], views of object representation
559template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700560constexpr span<const byte, details::calculate_byte_size<ElementType, Extent>::value> as_bytes(span<ElementType, Extent> s) noexcept
561{ return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800562
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700563template <class ElementType, ptrdiff_t Extent, class = std::enable_if_t<!std::is_const<ElementType>::value>>
564constexpr span<byte, details::calculate_byte_size<ElementType, Extent>::value> as_writeable_bytes(span<ElementType, Extent> s) noexcept
565{ return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; }
Neil MacIntoshcec26a22016-02-24 11:26:28 -0800566
567} // namespace gsl
568
569#ifdef _MSC_VER
570
571#undef constexpr
572#pragma pop_macro("constexpr")
573
574#if _MSC_VER <= 1800
575#pragma warning(pop)
576
577#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
578#undef noexcept
579#pragma pop_macro("noexcept")
580#endif // GSL_THROW_ON_CONTRACT_VIOLATION
581
582#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
583
584#endif // _MSC_VER <= 1800
585
586#endif // _MSC_VER
587
588#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
589
590#undef noexcept
591
592#ifdef _MSC_VER
593#pragma warning(pop)
594#pragma pop_macro("noexcept")
595#endif
596
597#endif // GSL_THROW_ON_CONTRACT_VIOLATION
598
599#endif // GSL_SPAN_H