blob: 0bffb4bc700a4a6fa1260f63f4ac6723014ca81a [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
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
42#pragma warning(disable: 26481 26482 26483 26485 26490 26491 26492 26493 26495)
43
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
54// noexcept is not understood
55#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
56#pragma push_macro("noexcept")
57#define noexcept /* nothing */
58#endif
59
60// turn off some misguided warnings
61#pragma warning(push)
62#pragma warning(disable : 4351) // warns about newly introduced aggregate initializer behavior
63#pragma warning(disable : 4512) // warns that assignment op could not be generated
64
65#endif // _MSC_VER <= 1800
66
67#endif // _MSC_VER
68
69#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
70
71#ifdef _MSC_VER
72#pragma push_macro("noexcept")
73#endif
74
75#define noexcept /* nothing */
76
77#endif // GSL_THROW_ON_CONTRACT_VIOLATION
78
79namespace gsl
80{
81
Neil MacIntoshc40094a2016-03-01 12:11:41 -080082template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
83class span;
84
85
Neil MacIntoshc94a66f2016-06-12 18:28:19 -070086// [views.constants], constants
87constexpr const std::ptrdiff_t dynamic_extent = -1;
88
89
90// implementation details
Neil MacIntoshc40094a2016-03-01 12:11:41 -080091namespace details
92{
93template <class T>
94struct is_span_oracle : std::false_type
95{
96};
97
98template <class ElementType, std::ptrdiff_t Extent>
99struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
100{
101};
102
103template <class T>
104struct is_span : is_span_oracle<std::remove_cv_t<T>>
105{
106};
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700107
108template <class From, class To>
109struct is_allowed_pointer_conversion
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700110 : std::bool_constant<
111 std::is_pointer<From>::value &&
112 std::is_pointer<To>::value &&
113 std::is_convertible<From, To>::value
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700114 >
115{
116};
117
118template <class From, class To>
119struct is_allowed_integral_conversion
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700120 : std::bool_constant<
121 std::is_integral<From>::value &&
122 std::is_integral<To>::value &&
123 sizeof(From) == sizeof(To) &&
124 alignof(From) == alignof(To) &&
125 std::is_convertible<From, To>::value
126 >
127{
128};
129
130template <std::ptrdiff_t From, std::ptrdiff_t To>
131struct is_allowed_extent_conversion
132 : std::bool_constant<
133 From == To ||
134 From == gsl::dynamic_extent ||
135 To == gsl::dynamic_extent
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700136 >
137{
138};
139
140template <class From, class To>
141struct is_allowed_element_type_conversion
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700142 : std::bool_constant<
143 std::is_same<From, std::remove_cv_t<To>>::value ||
144 is_allowed_pointer_conversion<From, To>::value ||
145 is_allowed_integral_conversion<From, To>::value
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700146 >
147{
148};
149
150template <class From>
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700151struct is_allowed_element_type_conversion<From, byte>
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700152 : std::bool_constant<!std::is_const<From>::value>
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700153{
154};
155
156template <class From>
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700157struct is_allowed_element_type_conversion<From, const byte>
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700158 : std::true_type
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700159{
160};
161
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700162template <class Span>
Neil MacIntosh26747242016-06-26 17:00:56 +0300163class const_span_iterator
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700164{
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700165public:
Neil MacIntosh26747242016-06-26 17:00:56 +0300166 using iterator_category = std::random_access_iterator_tag;
167 using value_type = typename Span::element_type;
168 using difference_type = std::ptrdiff_t;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700169
Neil MacIntosh26747242016-06-26 17:00:56 +0300170 using const_pointer = std::add_const_t<value_type*>;
171 using pointer = const_pointer;
172
173 using const_reference = std::add_const_t<value_type&>;
174 using reference = const_reference;
175
176 constexpr const_span_iterator() : const_span_iterator(nullptr, 0) {}
177 constexpr const_span_iterator(const Span* span, typename Span::index_type index) : span_(span), index_(index)
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700178 {
179 Expects(span == nullptr || (index_ >= 0 && index <= span_->length()));
180 }
181
Neil MacIntosh7b001722016-06-20 01:41:49 -0700182 constexpr reference operator*() const { Expects(span_); return (*span_)[index_]; }
183 constexpr pointer operator->() const { Expects(span_); return &((*span_)[index_]); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700184
Neil MacIntosh26747242016-06-26 17:00:56 +0300185 constexpr const_span_iterator& operator++() noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700186 {
187 Expects(span_ && index_ >= 0 && index_ < span_->length());
188 ++index_;
189 return *this;
190 }
191
Neil MacIntosh26747242016-06-26 17:00:56 +0300192 constexpr const_span_iterator operator++(int) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700193 {
194 auto ret = *this;
195 ++(*this);
196 return ret;
197 }
198
Neil MacIntosh26747242016-06-26 17:00:56 +0300199 constexpr const_span_iterator& operator--() noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700200 {
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700201 Expects(span_ && index_ > 0 && index_ <= span_->length());
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700202 --index_;
203 return *this;
204 }
205
Neil MacIntosh26747242016-06-26 17:00:56 +0300206 constexpr const_span_iterator operator--(int) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700207 {
208 auto ret = *this;
209 --(*this);
210 return ret;
211 }
212
Neil MacIntosh26747242016-06-26 17:00:56 +0300213 constexpr const_span_iterator operator+(difference_type n) const noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700214 {
215 auto ret{*this};
216 return ret += n;
217 }
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700218
Neil MacIntosh26747242016-06-26 17:00:56 +0300219 constexpr const_span_iterator& operator+=(difference_type n) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700220 {
Neil MacIntosh520c72d2016-07-18 12:00:33 -0700221 Expects(span_ && (index_ + n) >= 0 && (index_ + n) <= span_->length());
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700222 index_ += n;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700223 return *this;
224 }
225
Neil MacIntosh26747242016-06-26 17:00:56 +0300226 constexpr const_span_iterator operator-(difference_type n) const noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700227 {
228 auto ret{*this};
229 return ret -= n;
230 }
231
Neil MacIntosh26747242016-06-26 17:00:56 +0300232 constexpr const_span_iterator& operator-=(difference_type n) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700233 {
234 return *this += -n;
235 }
236
Neil MacIntosh26747242016-06-26 17:00:56 +0300237 constexpr difference_type operator-(const const_span_iterator& rhs) const noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700238 {
239 Expects(span_ == rhs.span_);
240 return index_ - rhs.index_;
241 }
242
Neil MacIntosh7b001722016-06-20 01:41:49 -0700243 constexpr reference operator[](difference_type n) const noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700244 {
245 return *(*this + n);
246 }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700247
Neil MacIntosh26747242016-06-26 17:00:56 +0300248 constexpr bool operator==(const const_span_iterator& rhs) const noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700249 {
250 return span_ == rhs.span_ && index_ == rhs.index_;
251 }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700252
Neil MacIntosh26747242016-06-26 17:00:56 +0300253 constexpr bool operator!=(const const_span_iterator& rhs) const noexcept { return !(*this == rhs); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700254
Neil MacIntosh26747242016-06-26 17:00:56 +0300255 constexpr bool operator<(const const_span_iterator& rhs) const noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700256 {
257 Expects(span_ == rhs.span_);
258 return index_ < rhs.index_;
259 }
260
Neil MacIntosh26747242016-06-26 17:00:56 +0300261 constexpr bool operator<=(const const_span_iterator& rhs) const noexcept { return !(rhs < *this); }
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700262
Neil MacIntosh26747242016-06-26 17:00:56 +0300263 constexpr bool operator>(const const_span_iterator& rhs) const noexcept { return rhs < *this; }
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700264
Neil MacIntosh26747242016-06-26 17:00:56 +0300265 constexpr bool operator>=(const const_span_iterator& rhs) const noexcept { return !(rhs > *this); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700266
Neil MacIntosh26747242016-06-26 17:00:56 +0300267 void swap(const_span_iterator& rhs) noexcept
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700268 {
269 std::swap(index_, rhs.index_);
270 std::swap(m_span, rhs.m_span);
271 }
272
273private:
274 const Span* span_;
275 ptrdiff_t index_;
276};
277
Neil MacIntosh26747242016-06-26 17:00:56 +0300278
279template <class Span>
280class span_iterator : public const_span_iterator<Span>
281{
282 using base_type = const_span_iterator<Span>;
283
284public:
285 using iterator_category = std::random_access_iterator_tag;
286 using value_type = typename Span::element_type;
287 using difference_type = std::ptrdiff_t;
288
289 using pointer = value_type*;
290 using reference = value_type&;
291
292 constexpr span_iterator() : base_type() {}
293 constexpr span_iterator(const Span* span, typename Span::index_type index) : base_type(span, index) {}
294
Neil MacIntosh0c1b6712016-07-20 08:52:09 -0700295 constexpr reference operator*() const { return const_cast<reference>(base_type::operator*()); }
296 constexpr pointer operator->() const { return const_cast<pointer>(base_type::operator->()); }
Neil MacIntosh26747242016-06-26 17:00:56 +0300297
298 constexpr span_iterator& operator++() noexcept { base_type::operator++(); return *this; }
299
300 constexpr span_iterator operator++(int) noexcept { return base_type::operator++(1); }
301
302 constexpr span_iterator& operator--() noexcept { base_type::operator--(); return *this; }
303
304 constexpr span_iterator operator--(int) noexcept { return base_type::operator--(1); }
305
306 constexpr span_iterator operator+(difference_type n) const noexcept { return base_type::operator+(n); }
307
308 constexpr span_iterator& operator+=(difference_type n) noexcept { return base_type::operator+=(n); }
309
310 constexpr span_iterator operator-(difference_type n) const noexcept { return base_type::operator-(n); }
311
312 constexpr span_iterator& operator-=(difference_type n) noexcept { return base_type::operator-=(n); }
313
314 constexpr difference_type operator-(const span_iterator& rhs) const noexcept { return base_type::operator-(rhs); }
315
316 constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); }
317
318 constexpr bool operator==(const span_iterator& rhs) const noexcept { return base_type::operator==(rhs); }
319
320 constexpr bool operator!=(const span_iterator& rhs) const noexcept { return !(*this == rhs); }
321
322 constexpr bool operator<(const span_iterator& rhs) const noexcept { return base_type::operator<(rhs); }
323
324 constexpr bool operator<=(const span_iterator& rhs) const noexcept { return !(rhs < *this); }
325
326 constexpr bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; }
327
328 constexpr bool operator>=(const span_iterator& rhs) const noexcept { return !(rhs > *this); }
329
330 void swap(span_iterator& rhs) noexcept { base_type::swap(rhs); }
331};
332
333template <typename Span>
334constexpr const_span_iterator<Span> operator+(typename const_span_iterator<Span>::difference_type n,
335 const const_span_iterator<Span>& rhs) noexcept
336{
337 return rhs + n;
338}
339
340template <typename Span>
341constexpr const_span_iterator<Span> operator-(typename const_span_iterator<Span>::difference_type n,
342 const const_span_iterator<Span>& rhs) noexcept
343{
344 return rhs - n;
345}
346
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700347template <typename Span>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700348constexpr span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n,
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700349 const span_iterator<Span>& rhs) noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700350{
351 return rhs + n;
352}
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700353
354template <typename Span>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700355constexpr span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n,
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700356 const span_iterator<Span>& rhs) noexcept
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700357{
358 return rhs - n;
359}
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700360
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800361} // namespace details
362
363
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800364// [span], class template span
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800365template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800366class span {
367public:
368 // constants and types
369 using element_type = ElementType;
370 using index_type = std::ptrdiff_t;
371 using pointer = element_type*;
372 using reference = element_type&;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700373
374 using iterator = details::span_iterator<span<ElementType, Extent>>;
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700375 using const_iterator = details::const_span_iterator<span>;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800376 using reverse_iterator = std::reverse_iterator<iterator>;
Neil MacIntosh26747242016-06-26 17:00:56 +0300377 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700378
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800379 constexpr static const index_type extent = Extent;
380
381 // [span.cons], span constructors, copy, assignment, and destructor
Neil MacIntosh502cd662016-02-28 00:50:53 -0800382 constexpr span() noexcept : storage_(nullptr, extent_type<0>())
383 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800384
385 constexpr span(nullptr_t) noexcept : span()
386 {}
387
Neil MacIntosh502cd662016-02-28 00:50:53 -0800388 constexpr span(pointer ptr, index_type count) : storage_(ptr, count)
Neil MacIntosh5e7771f2016-07-20 09:00:10 -0700389 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800390
Neil MacIntosh502cd662016-02-28 00:50:53 -0800391 constexpr span(pointer firstElem, pointer lastElem)
392 : storage_(firstElem, std::distance(firstElem, lastElem))
393 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800394
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800395 template <size_t N>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700396 constexpr span(element_type(&arr)[N]) noexcept
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800397 : storage_(&arr[0], extent_type<N>())
398 {}
399
400 template <size_t N>
Neil MacIntoshf2ab3a52016-07-20 09:24:49 -0700401 constexpr span(std::array<element_type, N>& arr) noexcept
Neil MacIntosh62f30202016-06-14 20:14:17 -0700402 : storage_(&arr[0], extent_type<N>())
403 {}
404
405 template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
Neil MacIntoshf2ab3a52016-07-20 09:24:49 -0700406 constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) noexcept
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800407 : storage_(&arr[0], extent_type<N>())
408 {}
409
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700410 template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
Neil MacIntoshf2ab3a52016-07-20 09:24:49 -0700411 constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800412 : storage_(&arr[0], extent_type<N>())
Neil MacIntosh502cd662016-02-28 00:50:53 -0800413 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800414
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800415 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
416 // on Container to be a contiguous sequence container.
417 template <class Container,
418 class = std::enable_if_t<!details::is_span<Container>::value &&
419 std::is_convertible<Container::pointer, pointer>::value &&
420 std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
421 >
422 constexpr span(Container& cont) : span(cont.data(), cont.size()) {}
423
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800424 template <class Container,
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700425 class = std::enable_if_t<std::is_const<element_type>::value &&
426 !details::is_span<Container>::value &&
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800427 std::is_convertible<Container::pointer, pointer>::value &&
428 std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
429 >
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700430 constexpr span(const Container& cont) : span(cont.data(), cont.size()) {}
431
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800432 constexpr span(const span& other) noexcept = default;
433 constexpr span(span&& other) noexcept = default;
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700434
435 template <class OtherElementType, std::ptrdiff_t OtherExtent,
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700436 class = std::enable_if_t<
437 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700438 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
439 >
440 >
441 constexpr span(const span<OtherElementType, OtherExtent>& other)
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700442 : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700443 {}
444
445 template <class OtherElementType, std::ptrdiff_t OtherExtent,
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700446 class = std::enable_if_t<
447 details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700448 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
449 >
450 >
451 constexpr span(span<OtherElementType, OtherExtent>&& other)
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700452 : storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700453 {}
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700454
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800455 ~span() noexcept = default;
456 constexpr span& operator=(const span& other) noexcept = default;
457 constexpr span& operator=(span&& other) noexcept = default;
458
459 // [span.sub], span subviews
460 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700461 constexpr span<element_type, Count> first() const
462 {
463 Expects(Count >= 0 && Count <= size());
464 return { data(), Count };
465 }
466
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800467 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700468 constexpr span<element_type, Count> last() const
469 {
470 Expects(Count >= 0 && Count <= size());
Neil MacIntosh64598bc2016-06-14 20:20:09 -0700471 return{ data() + (size() - Count), Count };
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700472 }
473
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800474 template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700475 constexpr span<element_type, Count> subspan() const
476 {
477 Expects((Offset == 0 || Offset > 0 && Offset <= size()) &&
478 (Count == dynamic_extent || Count >= 0 && Offset + Count <= size()));
479 return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };
480 }
481
482 constexpr span<element_type, dynamic_extent> first(index_type count) const
483 {
484 Expects(count >= 0 && count <= size());
485 return { data(), count };
486 }
487
488 constexpr span<element_type, dynamic_extent> last(index_type count) const
489 {
490 Expects(count >= 0 && count <= size());
Neil MacIntosh64598bc2016-06-14 20:20:09 -0700491 return { data() + (size() - count), count };
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700492 }
493
494 constexpr span<element_type, dynamic_extent> subspan(index_type offset,
495 index_type count = dynamic_extent) const
496 {
497 Expects((offset == 0 || offset > 0 && offset <= size()) &&
498 (count == dynamic_extent || count >= 0 && offset + count <= size()));
499 return { data() + offset, count == dynamic_extent ? size() - offset : count };
500 }
501
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800502 // [span.obs], span observers
503 constexpr index_type length() const noexcept { return size(); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800504 constexpr index_type size() const noexcept { return storage_.size(); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800505 constexpr index_type length_bytes() const noexcept { return size_bytes(); }
506 constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); }
507 constexpr bool empty() const noexcept { return size() == 0; }
508
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800509 // [span.elem], span element access
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800510 constexpr reference operator[](index_type idx) const
511 {
Neil MacIntosh502cd662016-02-28 00:50:53 -0800512 Expects(idx >= 0 && idx < storage_.size());
Neil MacIntoshf2ab3a52016-07-20 09:24:49 -0700513 return data()[idx];
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800514 }
515 constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800516 constexpr pointer data() const noexcept { return storage_.data(); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700517
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800518 // [span.iter], span iterator support
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700519 iterator begin() const noexcept { return {this, 0}; }
520 iterator end() const noexcept { return {this, length()}; }
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700521
522 const_iterator cbegin() const noexcept { return {this, 0}; }
523 const_iterator cend() const noexcept { return {this, length()}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800524
Neil MacIntosh30a038c2016-07-18 11:38:01 -0700525 reverse_iterator rbegin() const noexcept { return reverse_iterator{{this, length()}}; }
526 reverse_iterator rend() const noexcept { return reverse_iterator{{this, 0}}; }
527
528 const_reverse_iterator crbegin() const noexcept { return reverse_iterator{{this, length()}}; }
529 const_reverse_iterator crend() const noexcept { return reverse_iterator{{this, 0}}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800530
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800531private:
Neil MacIntosh502cd662016-02-28 00:50:53 -0800532 template <index_type Extent>
533 class extent_type;
534
535 template <index_type Extent>
536 class extent_type
537 {
538 public:
539 static_assert(Extent >= 0, "A fixed-size span must be >= 0 in size.");
540
541 constexpr extent_type() noexcept {}
542
543 template <index_type Other>
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700544 constexpr extent_type(extent_type<Other> ext) noexcept
Neil MacIntosh502cd662016-02-28 00:50:53 -0800545 {
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700546 static_assert(Other == Extent || Other == dynamic_extent,
Neil MacIntosh502cd662016-02-28 00:50:53 -0800547 "Mismatch between fixed-size extent and size of initializing data.");
Neil MacIntoshc94a66f2016-06-12 18:28:19 -0700548 Expects(ext.size() == Extent);
Neil MacIntosh502cd662016-02-28 00:50:53 -0800549 }
550
551 constexpr extent_type(index_type size)
552 { Expects(size == Extent); }
553
554 constexpr inline index_type size() const noexcept { return Extent; }
555 };
556
557 template <>
558 class extent_type<dynamic_extent>
559 {
560 public:
561 template <index_type Other>
562 explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
563 {}
564
565 explicit constexpr extent_type(index_type size) : size_(size)
566 { Expects(size >= 0); }
567
568 constexpr inline index_type size() const noexcept
569 { return size_; }
570
571 private:
572 index_type size_;
573 };
574
575 // this implementation detail class lets us take advantage of the
576 // empty base class optimization to pay for only storage of a single
577 // pointer in the case of fixed-size spans
578 template <class ExtentType>
579 class storage_type : public ExtentType
580 {
581 public:
582 template <class OtherExtentType>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700583 constexpr storage_type(pointer data, OtherExtentType ext)
Neil MacIntosh5e7771f2016-07-20 09:00:10 -0700584 : ExtentType(ext), data_(data)
585 { Expects((!data && size() == 0) || (data && size() >= 0)); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800586
Neil MacIntosh502cd662016-02-28 00:50:53 -0800587 constexpr inline pointer data() const noexcept
588 { return data_; }
589
590 private:
591 pointer data_;
592 };
593
594 storage_type<extent_type<Extent>> storage_;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800595};
596
597
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800598// [span.comparison], span comparison operators
599template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700600constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700601{ return std::equal(l.begin(), l.end(), r.begin(), r.end()); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800602
603template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700604constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700605{ return !(l == r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800606
607template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700608constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700609{ return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800610
611template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700612constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700613{ return !(l > r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800614
615template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700616constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700617{ return r < l; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800618
619template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700620constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r)
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700621{ return !(l < r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800622
623
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700624namespace details
625{
626 // if we only supported compilers with good constexpr support then
627 // this pair of classes could collapse down to a constexpr function
628
629 // we should use a narrow_cast<> to go to size_t, but older compilers may not see it as constexpr
630 // and so will fail compilation of the template
631 template <class ElementType, ptrdiff_t Extent>
632 struct calculate_byte_size :
633 std::integral_constant<std::ptrdiff_t, static_cast<ptrdiff_t>(sizeof(ElementType) * static_cast<size_t>(Extent))>
634 {};
635
636 template <class ElementType>
637 struct calculate_byte_size<ElementType, dynamic_extent> :
638 std::integral_constant<std::ptrdiff_t, dynamic_extent>
639 {};
640}
641
642
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800643// [span.objectrep], views of object representation
644template <class ElementType, ptrdiff_t Extent>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700645span<const byte, details::calculate_byte_size<ElementType, Extent>::value> as_bytes(span<ElementType, Extent> s) noexcept
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700646{ return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800647
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700648template <class ElementType, ptrdiff_t Extent, class = std::enable_if_t<!std::is_const<ElementType>::value>>
Neil MacIntosh7b001722016-06-20 01:41:49 -0700649span<byte, details::calculate_byte_size<ElementType, Extent>::value> as_writeable_bytes(span<ElementType, Extent> s) noexcept
Neil MacIntoshba8ebef2016-05-29 17:06:29 -0700650{ return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; }
Neil MacIntoshcec26a22016-02-24 11:26:28 -0800651
652} // namespace gsl
653
654#ifdef _MSC_VER
655
656#undef constexpr
657#pragma pop_macro("constexpr")
658
659#if _MSC_VER <= 1800
660#pragma warning(pop)
661
662#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
663#undef noexcept
664#pragma pop_macro("noexcept")
665#endif // GSL_THROW_ON_CONTRACT_VIOLATION
666
667#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
668
669#endif // _MSC_VER <= 1800
670
671#endif // _MSC_VER
672
673#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
674
675#undef noexcept
676
677#ifdef _MSC_VER
678#pragma warning(pop)
679#pragma pop_macro("noexcept")
680#endif
681
682#endif // GSL_THROW_ON_CONTRACT_VIOLATION
683
684#endif // GSL_SPAN_H