blob: 2760121653e594d356e0434d67c722514ee4810e [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 MacIntoshcec26a22016-02-24 11:26:28 -080024#include <array>
Neil MacIntoshcec26a22016-02-24 11:26:28 -080025#include <limits>
Neil MacIntoshd3929c52016-02-24 16:11:33 -080026#include <iterator>
Neil MacIntoshcec26a22016-02-24 11:26:28 -080027#include <stdexcept>
28#include <type_traits>
29#include <utility>
30
31#ifdef _MSC_VER
32
33// turn off some warnings that are noisy about our Expects statements
34#pragma warning(push)
35#pragma warning(disable : 4127) // conditional expression is constant
36
37// No MSVC does constexpr fully yet
38#pragma push_macro("constexpr")
39#define constexpr
40
41// VS 2013 workarounds
42#if _MSC_VER <= 1800
43
44#define GSL_MSVC_HAS_VARIADIC_CTOR_BUG
45#define GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT
46
47// noexcept is not understood
48#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
49#pragma push_macro("noexcept")
50#define noexcept /* nothing */
51#endif
52
53// turn off some misguided warnings
54#pragma warning(push)
55#pragma warning(disable : 4351) // warns about newly introduced aggregate initializer behavior
56#pragma warning(disable : 4512) // warns that assignment op could not be generated
57
58#endif // _MSC_VER <= 1800
59
60#endif // _MSC_VER
61
62#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
63
64#ifdef _MSC_VER
65#pragma push_macro("noexcept")
66#endif
67
68#define noexcept /* nothing */
69
70#endif // GSL_THROW_ON_CONTRACT_VIOLATION
71
72namespace gsl
73{
74
Neil MacIntoshc40094a2016-03-01 12:11:41 -080075template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
76class span;
77
78
79namespace details
80{
81template <class T>
82struct is_span_oracle : std::false_type
83{
84};
85
86template <class ElementType, std::ptrdiff_t Extent>
87struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
88{
89};
90
91template <class T>
92struct is_span : is_span_oracle<std::remove_cv_t<T>>
93{
94};
Neil MacIntosh717a2e32016-03-16 19:39:55 -070095
96template <class From, class To>
97struct is_allowed_pointer_conversion
98 : std::integral_constant<bool,
99 std::is_pointer<From>::value &&
100 std::is_pointer<To>::value &&
101 std::is_convertible<From, To>::value
102 >
103{
104};
105
106template <class From, class To>
107struct is_allowed_integral_conversion
108 : std::integral_constant<bool,
109 std::is_integral<From>::value &&
110 std::is_integral<To>::value &&
111 sizeof(From) == sizeof(To) &&
112 alignof(From) == alignof(To) &&
113 std::is_convertible<From, To>::value
114 >
115{
116};
117
118template <class From, class To>
119struct is_allowed_element_type_conversion
120 : std::integral_constant<bool,
121 std::is_same<From, std::remove_cv_t<To>>::value ||
122 is_allowed_pointer_conversion<From, To>::value ||
123 is_allowed_integral_conversion<From, To>::value
124 >
125{
126};
127
128template <class From>
129struct is_allowed_element_type_conversion<From, char>
130 : std::integral_constant<bool, !std::is_const<From>::value>
131{
132};
133
134template <class From>
135struct is_allowed_element_type_conversion<From, const char>
136 : std::integral_constant<bool, true>
137{
138};
139
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700140template <class Span>
141class span_iterator
142 : public std::iterator<std::random_access_iterator_tag, typename Span::element_type>
143{
144 using Base = std::iterator<std::random_access_iterator_tag, typename Span::element_type>;
145
146public:
147 using typename Base::reference;
148 using typename Base::pointer;
149 using typename Base::difference_type;
150
151 span_iterator() : span_iterator(nullptr, 0) {}
152 span_iterator(const Span* span, typename Span::index_type index) : span_(span), index_(index)
153 {
154 Expects(span == nullptr || (index_ >= 0 && index <= span_->length()));
155 }
156
157 reference operator*() const { Expects(span_); return (*span_)[index_]; }
158 pointer operator->() const { Expects(span_); return &((*span_)[index_]); }
159
160 span_iterator& operator++() noexcept
161 {
162 Expects(span_ && index_ >= 0 && index_ < span_->length());
163 ++index_;
164 return *this;
165 }
166
167 span_iterator operator++(int) noexcept
168 {
169 auto ret = *this;
170 ++(*this);
171 return ret;
172 }
173
174 span_iterator& operator--() noexcept
175 {
176 Expects(span_ && index > 0 && index_ <= span_->length());
177 --index_;
178 return *this;
179 }
180
181 span_iterator operator--(int) noexcept
182 {
183 auto ret = *this;
184 --(*this);
185 return ret;
186 }
187
188 span_iterator operator+(difference_type n) const noexcept
189 {
190 auto ret{*this};
191 return ret += n;
192 }
193
194 span_iterator& operator+=(difference_type n) noexcept
195 {
196 index_ += n;
197 Expects(span_ && index_ >= 0 && index_ <= span_->length());
198 return *this;
199 }
200
201 span_iterator operator-(difference_type n) const noexcept
202 {
203 auto ret{*this};
204 return ret -= n;
205 }
206
207 span_iterator& operator-=(difference_type n) noexcept
208 {
209 return *this += -n;
210 }
211
212 difference_type operator-(const span_iterator& rhs) const noexcept
213 {
214 Expects(span_ == rhs.span_);
215 return index_ - rhs.index_;
216 }
217
218 reference operator[](difference_type n) const noexcept
219 { return *(*this + n); }
220
221 bool operator==(const span_iterator& rhs) const noexcept
222 { return span_ == rhs.span_ && index_ == rhs.index_; }
223
224 bool operator!=(const span_iterator& rhs) const noexcept { return !(*this == rhs); }
225
226 bool operator<(const span_iterator& rhs) const noexcept
227 {
228 Expects(span_ == rhs.span_);
229 return index_ < rhs.index_;
230 }
231
232 bool operator<=(const span_iterator& rhs) const noexcept { return !(rhs < *this); }
233
234 bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; }
235
236 bool operator>=(const span_iterator& rhs) const noexcept { return !(rhs > *this); }
237
238 void swap(span_iterator& rhs) noexcept
239 {
240 std::swap(index_, rhs.index_);
241 std::swap(m_span, rhs.m_span);
242 }
243
244private:
245 const Span* span_;
246 ptrdiff_t index_;
247};
248
249template <typename Span>
250span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n,
251 const span_iterator<Span>& rhs) noexcept
252{ return rhs + n; }
253
254template <typename Span>
255span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n,
256 const span_iterator<Span>& rhs) noexcept
Neil MacIntosh25ff7ec2016-05-29 13:54:19 -0700257{ return rhs - n; }
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700258
259
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800260} // namespace details
261
262
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800263// [views.constants], constants
264constexpr const std::ptrdiff_t dynamic_extent = -1;
265
266
267// [span], class template span
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800268template <class ElementType, std::ptrdiff_t Extent>
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800269class span {
270public:
271 // constants and types
272 using element_type = ElementType;
273 using index_type = std::ptrdiff_t;
274 using pointer = element_type*;
275 using reference = element_type&;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700276
277 using iterator = details::span_iterator<span<ElementType, Extent>>;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800278 using reverse_iterator = std::reverse_iterator<iterator>;
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700279
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800280 constexpr static const index_type extent = Extent;
281
282 // [span.cons], span constructors, copy, assignment, and destructor
Neil MacIntosh502cd662016-02-28 00:50:53 -0800283 constexpr span() noexcept : storage_(nullptr, extent_type<0>())
284 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800285
286 constexpr span(nullptr_t) noexcept : span()
287 {}
288
Neil MacIntosh502cd662016-02-28 00:50:53 -0800289 constexpr span(pointer ptr, index_type count) : storage_(ptr, count)
Neil MacIntosh25ff7ec2016-05-29 13:54:19 -0700290 { Expects((!ptr && count == 0) || (ptr && count >= 0)); }
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800291
Neil MacIntosh502cd662016-02-28 00:50:53 -0800292 constexpr span(pointer firstElem, pointer lastElem)
293 : storage_(firstElem, std::distance(firstElem, lastElem))
294 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800295
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800296 template <size_t N>
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800297 constexpr span(element_type(&arr)[N])
298 : storage_(&arr[0], extent_type<N>())
299 {}
300
301 template <size_t N>
302 constexpr span(std::array<std::remove_const_t<element_type>, N>& arr)
303 : storage_(&arr[0], extent_type<N>())
304 {}
305
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700306 template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
Neil MacIntoshf61a9bb2016-02-29 13:16:48 -0800307 constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr)
308 : storage_(&arr[0], extent_type<N>())
Neil MacIntosh502cd662016-02-28 00:50:53 -0800309 {}
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800310
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800311 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
312 // on Container to be a contiguous sequence container.
313 template <class Container,
314 class = std::enable_if_t<!details::is_span<Container>::value &&
315 std::is_convertible<Container::pointer, pointer>::value &&
316 std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
317 >
318 constexpr span(Container& cont) : span(cont.data(), cont.size()) {}
319
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800320 template <class Container,
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700321 class = std::enable_if_t<std::is_const<element_type>::value &&
322 !details::is_span<Container>::value &&
Neil MacIntoshc40094a2016-03-01 12:11:41 -0800323 std::is_convertible<Container::pointer, pointer>::value &&
324 std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
325 >
Neil MacIntosh3d4c3492016-03-17 17:20:33 -0700326 constexpr span(const Container& cont) : span(cont.data(), cont.size()) {}
327
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800328 constexpr span(const span& other) noexcept = default;
329 constexpr span(span&& other) noexcept = default;
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700330
331 template <class OtherElementType, std::ptrdiff_t OtherExtent,
332 class = std::enable_if_t<!std::is_same<element_type, OtherElementType>::value &&
333 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
334 >
335 >
336 constexpr span(const span<OtherElementType, OtherExtent>& other)
337 : storage_(reinterpret_cast<pointer>(other.data()), other.length())
338 {}
339
340 template <class OtherElementType, std::ptrdiff_t OtherExtent,
341 class = std::enable_if_t<!std::is_same<element_type, OtherElementType>::value &&
342 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
343 >
344 >
345 constexpr span(span<OtherElementType, OtherExtent>&& other)
346 : storage_(reinterpret_cast<pointer>(other.data()), other.length())
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700347 {}
Neil MacIntosh717a2e32016-03-16 19:39:55 -0700348
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800349 ~span() noexcept = default;
350 constexpr span& operator=(const span& other) noexcept = default;
351 constexpr span& operator=(span&& other) noexcept = default;
352
353 // [span.sub], span subviews
354 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700355 constexpr span<element_type, Count> first() const
356 {
357 Expects(Count >= 0 && Count <= size());
358 return { data(), Count };
359 }
360
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800361 template <ptrdiff_t Count>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700362 constexpr span<element_type, Count> last() const
363 {
364 Expects(Count >= 0 && Count <= size());
365 return{ Count == 0 ? data() : data() + (size() - Count), Count };
366 }
367
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800368 template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
Neil MacIntoshc8a412f2016-03-18 16:49:29 -0700369 constexpr span<element_type, Count> subspan() const
370 {
371 Expects((Offset == 0 || Offset > 0 && Offset <= size()) &&
372 (Count == dynamic_extent || Count >= 0 && Offset + Count <= size()));
373 return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };
374 }
375
376 constexpr span<element_type, dynamic_extent> first(index_type count) const
377 {
378 Expects(count >= 0 && count <= size());
379 return { data(), count };
380 }
381
382 constexpr span<element_type, dynamic_extent> last(index_type count) const
383 {
384 Expects(count >= 0 && count <= size());
385 return { count == 0 ? data() : data() + (size() - count), count };
386 }
387
388 constexpr span<element_type, dynamic_extent> subspan(index_type offset,
389 index_type count = dynamic_extent) const
390 {
391 Expects((offset == 0 || offset > 0 && offset <= size()) &&
392 (count == dynamic_extent || count >= 0 && offset + count <= size()));
393 return { data() + offset, count == dynamic_extent ? size() - offset : count };
394 }
395
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800396 // [span.obs], span observers
397 constexpr index_type length() const noexcept { return size(); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800398 constexpr index_type size() const noexcept { return storage_.size(); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800399 constexpr index_type length_bytes() const noexcept { return size_bytes(); }
400 constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); }
401 constexpr bool empty() const noexcept { return size() == 0; }
402
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800403 // [span.elem], span element access
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800404 constexpr reference operator[](index_type idx) const
405 {
Neil MacIntosh502cd662016-02-28 00:50:53 -0800406 Expects(idx >= 0 && idx < storage_.size());
407 return storage_.data()[idx];
Neil MacIntoshcc22f2b2016-02-25 11:42:26 -0800408 }
409 constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
Neil MacIntosh502cd662016-02-28 00:50:53 -0800410 constexpr pointer data() const noexcept { return storage_.data(); }
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700411
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800412 // [span.iter], span iterator support
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700413 iterator begin() const noexcept { return {this, 0}; }
414 iterator end() const noexcept { return {this, length()}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800415
Neil MacIntoshd9d6ff02016-05-29 13:52:28 -0700416 reverse_iterator rbegin() const noexcept { return {this, length()}; }
417 reverse_iterator rend() const noexcept { return {this, 0}; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800418
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800419private:
Neil MacIntosh502cd662016-02-28 00:50:53 -0800420 template <index_type Extent>
421 class extent_type;
422
423 template <index_type Extent>
424 class extent_type
425 {
426 public:
427 static_assert(Extent >= 0, "A fixed-size span must be >= 0 in size.");
428
429 constexpr extent_type() noexcept {}
430
431 template <index_type Other>
432 constexpr extent_type(extent_type<Other>) noexcept
433 {
434 static_assert(Other == Extent,
435 "Mismatch between fixed-size extent and size of initializing data.");
436 }
437
438 constexpr extent_type(index_type size)
439 { Expects(size == Extent); }
440
441 constexpr inline index_type size() const noexcept { return Extent; }
442 };
443
444 template <>
445 class extent_type<dynamic_extent>
446 {
447 public:
448 template <index_type Other>
449 explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
450 {}
451
452 explicit constexpr extent_type(index_type size) : size_(size)
453 { Expects(size >= 0); }
454
455 constexpr inline index_type size() const noexcept
456 { return size_; }
457
458 private:
459 index_type size_;
460 };
461
462 // this implementation detail class lets us take advantage of the
463 // empty base class optimization to pay for only storage of a single
464 // pointer in the case of fixed-size spans
465 template <class ExtentType>
466 class storage_type : public ExtentType
467 {
468 public:
469 template <class OtherExtentType>
470 storage_type(pointer data, OtherExtentType ext)
471 : ExtentType(ext), data_(data) {}
472
Neil MacIntosh502cd662016-02-28 00:50:53 -0800473 constexpr inline pointer data() const noexcept
474 { return data_; }
475
476 private:
477 pointer data_;
478 };
479
480 storage_type<extent_type<Extent>> storage_;
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800481};
482
483
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800484// [span.comparison], span comparison operators
485template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700486constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
487{ return std::equal(l.begin(), l.end(), r.begin(), r.end()); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800488
489template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700490constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
491{ return !(l == r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800492
493template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700494constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
495{ return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800496
497template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700498constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
499{ return !(l > r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800500
501template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700502constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
503{ return r < l; }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800504
505template <class ElementType, ptrdiff_t Extent>
Neil MacIntoshd63c9802016-05-29 14:05:09 -0700506constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) noexcept
507{ return !(l < r); }
Neil MacIntoshd3929c52016-02-24 16:11:33 -0800508
509
510#if 0 // TODO
511// [span.objectrep], views of object representation
512template <class ElementType, ptrdiff_t Extent>
513constexpr span<const char, ((Extent == dynamic_extent) ? dynamic_extent : (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
514
515template <class ElementType, ptrdiff_t Extent>
516constexpr span<char, ((Extent == dynamic_extent) ? dynamic_extent : (sizeof(ElementType) * Extent))> as_writeable_bytes(span<ElementType, Extent>) noexcept;
517#endif
Neil MacIntoshcec26a22016-02-24 11:26:28 -0800518
519} // namespace gsl
520
521#ifdef _MSC_VER
522
523#undef constexpr
524#pragma pop_macro("constexpr")
525
526#if _MSC_VER <= 1800
527#pragma warning(pop)
528
529#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
530#undef noexcept
531#pragma pop_macro("noexcept")
532#endif // GSL_THROW_ON_CONTRACT_VIOLATION
533
534#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
535
536#endif // _MSC_VER <= 1800
537
538#endif // _MSC_VER
539
540#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
541
542#undef noexcept
543
544#ifdef _MSC_VER
545#pragma warning(pop)
546#pragma pop_macro("noexcept")
547#endif
548
549#endif // GSL_THROW_ON_CONTRACT_VIOLATION
550
551#endif // GSL_SPAN_H