blob: decc8e2e9a92aec228cf50ca96ce686dc4ceb8fb [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 MacIntoshb03b04b2016-07-20 13:17:47 -070082// [views.constants], constants
Neil MacIntoshc94a66f2016-06-12 18:28:19 -070083constexpr const std::ptrdiff_t dynamic_extent = -1;
84
Neil MacIntoshc366f442016-07-26 18:34:27 -070085template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
86class span;
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>
Neil MacIntoshc366f442016-07-26 18:34:27 -0700102 struct is_span : public is_span_oracle<std::remove_cv_t<T>>
103 {
104 };
105
106 template <class T>
107 struct is_std_array_oracle : std::false_type
108 {
109 };
110
111 template <class ElementType, size_t Extent>
112 struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type
113 {
114 };
115
116 template <class T>
117 struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700118 {
119 };
120
121 template <class From, class To>
122 struct is_allowed_pointer_conversion
Neil MacIntoshc366f442016-07-26 18:34:27 -0700123 : public std::integral_constant<bool, std::is_pointer<From>::value && std::is_pointer<To>::value &&
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700124 std::is_convertible<From, To>::value>
125 {
126 };
127
128 template <class From, class To>
129 struct is_allowed_integral_conversion
Neil MacIntoshc366f442016-07-26 18:34:27 -0700130 : public std::integral_constant<bool, std::is_integral<From>::value && std::is_integral<To>::value &&
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700131 sizeof(From) == sizeof(To) && alignof(From) == alignof(To) &&
132 std::is_convertible<From, To>::value>
133 {
134 };
135
136 template <std::ptrdiff_t From, std::ptrdiff_t To>
137 struct is_allowed_extent_conversion
Neil MacIntoshc366f442016-07-26 18:34:27 -0700138 : public std::integral_constant<bool, From == To || From == gsl::dynamic_extent || To == gsl::dynamic_extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700139 {
140 };
141
142 template <class From, class To>
143 struct is_allowed_element_type_conversion
Neil MacIntoshc366f442016-07-26 18:34:27 -0700144 : public std::integral_constant<bool, std::is_same<From, std::remove_cv_t<To>>::value ||
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700145 is_allowed_pointer_conversion<From, To>::value ||
146 is_allowed_integral_conversion<From, To>::value>
147 {
148 };
149
150 template <class From>
151 struct is_allowed_element_type_conversion<From, byte>
Neil MacIntoshc366f442016-07-26 18:34:27 -0700152 : public std::integral_constant<bool, !std::is_const<From>::value>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700153 {
154 };
155
156 template <class From>
Neil MacIntoshc366f442016-07-26 18:34:27 -0700157 struct is_allowed_element_type_conversion<From, const byte> : public std::true_type
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700158 {
159 };
160
161 template <class Span>
162 class const_span_iterator
163 {
164 public:
165 using iterator_category = std::random_access_iterator_tag;
166 using value_type = typename Span::element_type;
167 using difference_type = std::ptrdiff_t;
168
169 using const_pointer = std::add_const_t<value_type*>;
170 using pointer = const_pointer;
171
172 using const_reference = std::add_const_t<value_type&>;
173 using reference = const_reference;
174
175 constexpr const_span_iterator() : const_span_iterator(nullptr, 0) {}
176 constexpr const_span_iterator(const Span* span, typename Span::index_type index)
177 : span_(span), index_(index)
178 {
179 Expects(span == nullptr || (index_ >= 0 && index <= span_->length()));
180 }
181
182 constexpr reference operator*() const
183 {
184 Expects(span_);
185 return (*span_)[index_];
186 }
187 constexpr pointer operator->() const
188 {
189 Expects(span_);
190 return &((*span_)[index_]);
191 }
192
193 constexpr const_span_iterator& operator++() noexcept
194 {
195 Expects(span_ && index_ >= 0 && index_ < span_->length());
196 ++index_;
197 return *this;
198 }
199
200 constexpr const_span_iterator operator++(int) noexcept
201 {
202 auto ret = *this;
203 ++(*this);
204 return ret;
205 }
206
207 constexpr const_span_iterator& operator--() noexcept
208 {
209 Expects(span_ && index_ > 0 && index_ <= span_->length());
210 --index_;
211 return *this;
212 }
213
214 constexpr const_span_iterator operator--(int) noexcept
215 {
216 auto ret = *this;
217 --(*this);
218 return ret;
219 }
220
221 constexpr const_span_iterator operator+(difference_type n) const noexcept
222 {
Neil MacIntosh4de3d4e2016-07-26 18:44:13 -0700223 auto ret = *this;
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700224 return ret += n;
225 }
226
227 constexpr const_span_iterator& operator+=(difference_type n) noexcept
228 {
229 Expects(span_ && (index_ + n) >= 0 && (index_ + n) <= span_->length());
230 index_ += n;
231 return *this;
232 }
233
234 constexpr const_span_iterator operator-(difference_type n) const noexcept
235 {
Neil MacIntosh4de3d4e2016-07-26 18:44:13 -0700236 auto ret = *this;
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700237 return ret -= n;
238 }
239
240 constexpr const_span_iterator& operator-=(difference_type n) noexcept
241 {
242 return *this += -n;
243 }
244
245 constexpr difference_type operator-(const const_span_iterator& rhs) const noexcept
246 {
247 Expects(span_ == rhs.span_);
248 return index_ - rhs.index_;
249 }
250
251 constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); }
252
253 constexpr bool operator==(const const_span_iterator& rhs) const noexcept
254 {
255 return span_ == rhs.span_ && index_ == rhs.index_;
256 }
257
258 constexpr bool operator!=(const const_span_iterator& rhs) const noexcept
259 {
260 return !(*this == rhs);
261 }
262
263 constexpr bool operator<(const const_span_iterator& rhs) const noexcept
264 {
265 Expects(span_ == rhs.span_);
266 return index_ < rhs.index_;
267 }
268
269 constexpr bool operator<=(const const_span_iterator& rhs) const noexcept
270 {
271 return !(rhs < *this);
272 }
273
274 constexpr bool operator>(const const_span_iterator& rhs) const noexcept
275 {
276 return rhs < *this;
277 }
278
279 constexpr bool operator>=(const const_span_iterator& rhs) const noexcept
280 {
281 return !(rhs > *this);
282 }
283
284 void swap(const_span_iterator& rhs) noexcept
285 {
286 std::swap(index_, rhs.index_);
Neil MacIntoshc366f442016-07-26 18:34:27 -0700287 std::swap(span_, rhs.span_);
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700288 }
289
290 private:
291 const Span* span_;
Neil MacIntoshc366f442016-07-26 18:34:27 -0700292 std::ptrdiff_t index_;
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700293 };
294
295 template <class Span>
296 class span_iterator : public const_span_iterator<Span>
297 {
298 using base_type = const_span_iterator<Span>;
299
300 public:
301 using iterator_category = std::random_access_iterator_tag;
302 using value_type = typename Span::element_type;
303 using difference_type = std::ptrdiff_t;
304
305 using pointer = value_type*;
306 using reference = value_type&;
307
308 constexpr span_iterator() : base_type() {}
309 constexpr span_iterator(const Span* span, typename Span::index_type index)
310 : base_type(span, index)
311 {
312 }
313
314 constexpr reference operator*() const
315 {
316 return const_cast<reference>(base_type::operator*());
317 }
318 constexpr pointer operator->() const
319 {
320 return const_cast<pointer>(base_type::operator->());
321 }
322
323 constexpr span_iterator& operator++() noexcept
324 {
325 base_type::operator++();
326 return *this;
327 }
328
329 constexpr span_iterator operator++(int) noexcept { return base_type::operator++(1); }
330
331 constexpr span_iterator& operator--() noexcept
332 {
333 base_type::operator--();
334 return *this;
335 }
336
337 constexpr span_iterator operator--(int) noexcept { return base_type::operator--(1); }
338
339 constexpr span_iterator operator+(difference_type n) const noexcept
340 {
Neil MacIntoshc366f442016-07-26 18:34:27 -0700341 return {base_type::operator+(n)};
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700342 }
343
344 constexpr span_iterator& operator+=(difference_type n) noexcept
345 {
Neil MacIntoshc366f442016-07-26 18:34:27 -0700346 return {base_type::operator+=(n)};
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700347 }
348
349 constexpr span_iterator operator-(difference_type n) const noexcept
350 {
351 return base_type::operator-(n);
352 }
353
354 constexpr span_iterator& operator-=(difference_type n) noexcept
355 {
356 return base_type::operator-=(n);
357 }
358
359 constexpr difference_type operator-(const span_iterator& rhs) const noexcept
360 {
361 return base_type::operator-(rhs);
362 }
363
364 constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); }
365
366 constexpr bool operator==(const span_iterator& rhs) const noexcept
367 {
368 return base_type::operator==(rhs);
369 }
370
371 constexpr bool operator!=(const span_iterator& rhs) const noexcept
372 {
373 return !(*this == rhs);
374 }
375
376 constexpr bool operator<(const span_iterator& rhs) const noexcept
377 {
378 return base_type::operator<(rhs);
379 }
380
381 constexpr bool operator<=(const span_iterator& rhs) const noexcept
382 {
383 return !(rhs < *this);
384 }
385
386 constexpr bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; }
387
388 constexpr bool operator>=(const span_iterator& rhs) const noexcept
389 {
390 return !(rhs > *this);
391 }
392
393 void swap(span_iterator& rhs) noexcept { base_type::swap(rhs); }
Neil MacIntoshc366f442016-07-26 18:34:27 -0700394 private:
395 constexpr span_iterator(const base_type& base) : base_type(base)
396 {
397 }
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700398 };
399
400 template <typename Span>
401 constexpr const_span_iterator<Span>
402 operator+(typename const_span_iterator<Span>::difference_type n,
403 const const_span_iterator<Span>& rhs) noexcept
404 {
405 return rhs + n;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700406 }
407
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700408 template <typename Span>
409 constexpr const_span_iterator<Span>
410 operator-(typename const_span_iterator<Span>::difference_type n,
411 const const_span_iterator<Span>& rhs) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700412 {
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700413 return rhs - n;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700414 }
415
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700416 template <typename Span>
417 constexpr span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n,
418 const span_iterator<Span>& rhs) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700419 {
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700420 return rhs + n;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700421 }
422
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700423 template <typename Span>
424 constexpr span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n,
425 const span_iterator<Span>& rhs) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700426 {
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700427 return rhs - n;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700428 }
429
Neil MacIntoshc366f442016-07-26 18:34:27 -0700430 template <std::ptrdiff_t Ext>
431 class extent_type
432 {
433 public:
434 using index_type = std::ptrdiff_t;
435
436 static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size.");
437
438 constexpr extent_type() noexcept {}
439
440 template <index_type Other>
441 constexpr extent_type(extent_type<Other> ext) noexcept
442 {
443 static_assert(Other == Ext || Other == dynamic_extent,
444 "Mismatch between fixed-size extent and size of initializing data.");
445 Expects(ext.size() == Ext);
446 }
447
448 constexpr extent_type(index_type size) { Expects(size == Ext); }
449
450 constexpr inline index_type size() const noexcept { return Ext; }
451 };
452
453 template <>
454 class extent_type<dynamic_extent>
455 {
456 public:
457 using index_type = std::ptrdiff_t;
458
459 template <index_type Other>
460 explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
461 {
462 }
463
464 explicit constexpr extent_type(index_type size) : size_(size) { Expects(size >= 0); }
465
466 constexpr inline index_type size() const noexcept { return size_; }
467
468 private:
469 index_type size_;
470 };
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800471} // namespace details
472
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700473// [span], class template span
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800474template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700475class span
476{
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800477public:
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700478 // constants and types
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800479 using element_type = ElementType;
480 using index_type = std::ptrdiff_t;
481 using pointer = element_type*;
482 using reference = element_type&;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700483
484 using iterator = details::span_iterator<span<ElementType, Extent>>;
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700485 using const_iterator = details::const_span_iterator<span>;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800486 using reverse_iterator = std::reverse_iterator<iterator>;
Neil MacIntosh26747242016-06-26 17:00:56 +0300487 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700488
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800489 constexpr static const index_type extent = Extent;
490
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700491 // [span.cons], span constructors, copy, assignment, and destructor
Neil MacIntoshc366f442016-07-26 18:34:27 -0700492 constexpr span() noexcept : storage_(nullptr, details::extent_type<0>()) {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800493
Neil MacIntoshc366f442016-07-26 18:34:27 -0700494 constexpr span(std::nullptr_t) noexcept : span() {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800495
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700496 constexpr span(pointer ptr, index_type count) : storage_(ptr, count) {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800497
Neil MacIntosh502cd662016-02-28 00:50:53 -0800498 constexpr span(pointer firstElem, pointer lastElem)
499 : storage_(firstElem, std::distance(firstElem, lastElem))
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700500 {
501 }
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800502
503 template <size_t N>
Neil MacIntoshc366f442016-07-26 18:34:27 -0700504 constexpr span(element_type (&arr)[N]) noexcept : storage_(&arr[0], details::extent_type<N>())
505 {
506 }
507
508 template <size_t N, class ArrayElementType = std::remove_const_t<element_type>>
509 constexpr span(std::array<ArrayElementType, N>& arr) noexcept
510 : storage_(&arr[0], details::extent_type<N>())
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700511 {
512 }
513
514 template <size_t N>
Neil MacIntoshf2ab3a52016-07-20 09:24:49 -0700515 constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
Neil MacIntoshc366f442016-07-26 18:34:27 -0700516 : storage_(&arr[0], details::extent_type<N>())
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700517 {
518 }
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800519
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800520 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
521 // on Container to be a contiguous sequence container.
522 template <class Container,
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700523 class = std::enable_if_t<
524 !details::is_span<Container>::value &&
Neil MacIntoshc366f442016-07-26 18:34:27 -0700525 !details::is_std_array<Container>::value &&
526 std::is_convertible<typename Container::pointer, pointer>::value &&
527 std::is_convertible<typename Container::pointer,
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700528 decltype(std::declval<Container>().data())>::value>>
529 constexpr span(Container& cont) : span(cont.data(), cont.size())
530 {
531 }
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800532
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800533 template <class Container,
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700534 class = std::enable_if_t<
535 std::is_const<element_type>::value && !details::is_span<Container>::value &&
Neil MacIntoshc366f442016-07-26 18:34:27 -0700536 std::is_convertible<typename Container::pointer, pointer>::value &&
537 std::is_convertible<typename Container::pointer,
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700538 decltype(std::declval<Container>().data())>::value>>
539 constexpr span(const Container& cont) : span(cont.data(), cont.size())
540 {
541 }
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700542
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800543 constexpr span(const span& other) noexcept = default;
544 constexpr span(span&& other) noexcept = default;
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700545
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700546 template <
547 class OtherElementType, std::ptrdiff_t OtherExtent,
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700548 class = std::enable_if_t<
549 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700550 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
551 constexpr span(const span<OtherElementType, OtherExtent>& other)
Neil MacIntoshc366f442016-07-26 18:34:27 -0700552 : storage_(reinterpret_cast<pointer>(other.data()), details::extent_type<OtherExtent>(other.size()))
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700553 {
554 }
555
556 template <
557 class OtherElementType, std::ptrdiff_t OtherExtent,
558 class = std::enable_if_t<
559 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
560 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700561 constexpr span(span<OtherElementType, OtherExtent>&& other)
Neil MacIntoshc366f442016-07-26 18:34:27 -0700562 : storage_(reinterpret_cast<pointer>(other.data()), details::extent_type<OtherExtent>(other.size()))
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700563 {
564 }
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700565
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800566 ~span() noexcept = default;
567 constexpr span& operator=(const span& other) noexcept = default;
568 constexpr span& operator=(span&& other) noexcept = default;
569
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700570 // [span.sub], span subviews
Neil MacIntoshc366f442016-07-26 18:34:27 -0700571 template <std::ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700572 constexpr span<element_type, Count> first() const
573 {
574 Expects(Count >= 0 && Count <= size());
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700575 return {data(), Count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700576 }
577
Neil MacIntoshc366f442016-07-26 18:34:27 -0700578 template <std::ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700579 constexpr span<element_type, Count> last() const
580 {
581 Expects(Count >= 0 && Count <= size());
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700582 return {data() + (size() - Count), Count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700583 }
584
Neil MacIntoshc366f442016-07-26 18:34:27 -0700585 template <std::ptrdiff_t Offset, std::ptrdiff_t Count = dynamic_extent>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700586 constexpr span<element_type, Count> subspan() const
587 {
Neil MacIntoshc366f442016-07-26 18:34:27 -0700588 Expects((Offset == 0 || (Offset > 0 && Offset <= size())) &&
589 (Count == dynamic_extent || (Count >= 0 && Offset + Count <= size())));
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700590 return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700591 }
592
593 constexpr span<element_type, dynamic_extent> first(index_type count) const
594 {
595 Expects(count >= 0 && count <= size());
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700596 return {data(), count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700597 }
598
599 constexpr span<element_type, dynamic_extent> last(index_type count) const
600 {
601 Expects(count >= 0 && count <= size());
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700602 return {data() + (size() - count), count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700603 }
604
605 constexpr span<element_type, dynamic_extent> subspan(index_type offset,
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700606 index_type count = dynamic_extent) const
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700607 {
Neil MacIntoshc366f442016-07-26 18:34:27 -0700608 Expects((offset == 0 || (offset > 0 && offset <= size())) &&
609 (count == dynamic_extent || (count >= 0 && offset + count <= size())));
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700610 return {data() + offset, count == dynamic_extent ? size() - offset : count};
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700611 }
612
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700613 // [span.obs], span observers
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800614 constexpr index_type length() const noexcept { return size(); }
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700615 constexpr index_type size() const noexcept { return storage_.size(); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800616 constexpr index_type length_bytes() const noexcept { return size_bytes(); }
617 constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); }
618 constexpr bool empty() const noexcept { return size() == 0; }
619
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700620 // [span.elem], span element access
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800621 constexpr reference operator[](index_type idx) const
622 {
Neil MacIntosh502cd662016-02-28 00:50:53 -0800623 Expects(idx >= 0 && idx < storage_.size());
Neil MacIntoshf2ab3a52016-07-20 09:24:49 -0700624 return data()[idx];
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800625 }
626 constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800627 constexpr pointer data() const noexcept { return storage_.data(); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700628
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700629 // [span.iter], span iterator support
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700630 iterator begin() const noexcept { return {this, 0}; }
631 iterator end() const noexcept { return {this, length()}; }
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700632
633 const_iterator cbegin() const noexcept { return {this, 0}; }
634 const_iterator cend() const noexcept { return {this, length()}; }
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700635
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700636 reverse_iterator rbegin() const noexcept { return reverse_iterator{{this, length()}}; }
637 reverse_iterator rend() const noexcept { return reverse_iterator{{this, 0}}; }
638
639 const_reverse_iterator crbegin() const noexcept { return reverse_iterator{{this, length()}}; }
640 const_reverse_iterator crend() const noexcept { return reverse_iterator{{this, 0}}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800641
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800642private:
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700643 // this implementation detail class lets us take advantage of the
Neil MacIntosh502cd662016-02-28 00:50:53 -0800644 // empty base class optimization to pay for only storage of a single
645 // pointer in the case of fixed-size spans
646 template <class ExtentType>
647 class storage_type : public ExtentType
648 {
649 public:
650 template <class OtherExtentType>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700651 constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
652 {
Neil MacIntosh4de3d4e2016-07-26 18:44:13 -0700653 Expects((!data && ExtentType::size() == 0) || (data && ExtentType::size() >= 0));
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700654 }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800655
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700656 constexpr inline pointer data() const noexcept { return data_; }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800657
658 private:
659 pointer data_;
660 };
661
Neil MacIntoshc366f442016-07-26 18:34:27 -0700662 storage_type<details::extent_type<Extent>> storage_;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800663};
664
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700665// [span.comparison], span comparison operators
Neil MacIntoshc366f442016-07-26 18:34:27 -0700666template <class ElementType, std::ptrdiff_t FirstExtent, std::ptrdiff_t SecondExtent>
667constexpr bool operator==(const span<ElementType, FirstExtent>& l, const span<ElementType, SecondExtent>& r)
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700668{
669 return std::equal(l.begin(), l.end(), r.begin(), r.end());
670}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800671
Neil MacIntoshc366f442016-07-26 18:34:27 -0700672template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700673constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
674{
675 return !(l == r);
676}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800677
Neil MacIntoshc366f442016-07-26 18:34:27 -0700678template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700679constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
680{
681 return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
682}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800683
Neil MacIntoshc366f442016-07-26 18:34:27 -0700684template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700685constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
686{
687 return !(l > r);
688}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800689
Neil MacIntoshc366f442016-07-26 18:34:27 -0700690template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700691constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
692{
693 return r < l;
694}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800695
Neil MacIntoshc366f442016-07-26 18:34:27 -0700696template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700697constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
698{
699 return !(l < r);
700}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800701
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700702namespace details
703{
704 // if we only supported compilers with good constexpr support then
705 // this pair of classes could collapse down to a constexpr function
706
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700707 // we should use a narrow_cast<> to go to size_t, but older compilers may not see it as
708 // constexpr
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700709 // and so will fail compilation of the template
Neil MacIntoshc366f442016-07-26 18:34:27 -0700710 template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700711 struct calculate_byte_size
712 : std::integral_constant<std::ptrdiff_t,
Neil MacIntoshc366f442016-07-26 18:34:27 -0700713 static_cast<std::ptrdiff_t>(sizeof(ElementType) *
714 static_cast<std::size_t>(Extent))>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700715 {
716 };
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700717
718 template <class ElementType>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700719 struct calculate_byte_size<ElementType, dynamic_extent>
720 : std::integral_constant<std::ptrdiff_t, dynamic_extent>
721 {
722 };
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700723}
724
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700725// [span.objectrep], views of object representation
Neil MacIntoshc366f442016-07-26 18:34:27 -0700726template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700727span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
728as_bytes(span<ElementType, Extent> s) noexcept
729{
730 return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
731}
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800732
Neil MacIntoshc366f442016-07-26 18:34:27 -0700733template <class ElementType, std::ptrdiff_t Extent,
Neil MacIntoshb03b04b2016-07-20 13:17:47 -0700734 class = std::enable_if_t<!std::is_const<ElementType>::value>>
735span<byte, details::calculate_byte_size<ElementType, Extent>::value>
736as_writeable_bytes(span<ElementType, Extent> s) noexcept
737{
738 return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
739}
Neil MacIntoshcec26a22016-02-24 11:26:28 -0800740
741} // namespace gsl
742
743#ifdef _MSC_VER
744
745#undef constexpr
746#pragma pop_macro("constexpr")
747
748#if _MSC_VER <= 1800
749#pragma warning(pop)
750
751#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
752#undef noexcept
753#pragma pop_macro("noexcept")
754#endif // GSL_THROW_ON_CONTRACT_VIOLATION
755
756#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
757
758#endif // _MSC_VER <= 1800
759
760#endif // _MSC_VER
761
762#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
763
764#undef noexcept
765
766#ifdef _MSC_VER
767#pragma warning(pop)
768#pragma pop_macro("noexcept")
769#endif
770
771#endif // GSL_THROW_ON_CONTRACT_VIOLATION
772
773#endif // GSL_SPAN_H