]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/ms-gsl/include/gsl/span
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / ms-gsl / include / gsl / span
1 ///////////////////////////////////////////////////////////////////////////////
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 #ifndef GSL_SPAN_H
18 #define GSL_SPAN_H
19
20 #include <gsl/gsl_assert> // for Expects
21 #include <gsl/gsl_byte> // for byte
22 #include <gsl/gsl_util> // for narrow_cast
23
24 #include <array> // for array
25 #include <cstddef> // for ptrdiff_t, size_t, nullptr_t
26 #include <iterator> // for reverse_iterator, distance, random_access_...
27 #include <type_traits> // for enable_if_t, declval, is_convertible, inte...
28
29 #if defined(_MSC_VER) && !defined(__clang__)
30 #pragma warning(push)
31
32 // turn off some warnings that are noisy about our Expects statements
33 #pragma warning(disable : 4127) // conditional expression is constant
34 #pragma warning( \
35 disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
36 #pragma warning(disable : 4702) // unreachable code
37
38 // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
39 #pragma warning(disable : 26495) // uninitalized member when constructor calls constructor
40 #pragma warning(disable : 26446) // parser bug does not allow attributes on some templates
41
42 #endif // _MSC_VER
43
44 // See if we have enough C++17 power to use a static constexpr data member
45 // without needing an out-of-line definition
46 #if !(defined(__cplusplus) && (__cplusplus >= 201703L))
47 #define GSL_USE_STATIC_CONSTEXPR_WORKAROUND
48 #endif // !(defined(__cplusplus) && (__cplusplus >= 201703L))
49
50 // GCC 7 does not like the signed unsigned missmatch (size_t ptrdiff_t)
51 // While there is a conversion from signed to unsigned, it happens at
52 // compiletime, so the compiler wouldn't have to warn indiscriminately, but
53 // could check if the source value actually doesn't fit into the target type
54 // and only warn in those cases.
55 #if defined(__GNUC__) && __GNUC__ > 6
56 #pragma GCC diagnostic push
57 #pragma GCC diagnostic ignored "-Wsign-conversion"
58 #endif
59
60 namespace gsl
61 {
62
63 // [views.constants], constants
64 constexpr const std::size_t dynamic_extent = narrow_cast<std::size_t>(-1);
65
66 template <class ElementType, std::size_t Extent = dynamic_extent>
67 class span;
68
69 // implementation details
70 namespace details
71 {
72 template <class T>
73 struct is_span_oracle : std::false_type
74 {
75 };
76
77 template <class ElementType, std::size_t Extent>
78 struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
79 {
80 };
81
82 template <class T>
83 struct is_span : public is_span_oracle<std::remove_cv_t<T>>
84 {
85 };
86
87 template <class T>
88 struct is_std_array_oracle : std::false_type
89 {
90 };
91
92 template <class ElementType, std::size_t Extent>
93 struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type
94 {
95 };
96
97 template <class T>
98 struct is_std_array : is_std_array_oracle<std::remove_cv_t<T>>
99 {
100 };
101
102 template <std::size_t From, std::size_t To>
103 struct is_allowed_extent_conversion
104 : std::integral_constant<bool, From == To || To == dynamic_extent>
105 {
106 };
107
108 template <class From, class To>
109 struct is_allowed_element_type_conversion
110 : std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
111 {
112 };
113
114 template <class Type>
115 class span_iterator
116 {
117 public:
118 using iterator_category = std::random_access_iterator_tag;
119 using value_type = std::remove_cv_t<Type>;
120 using difference_type = std::ptrdiff_t;
121 using pointer = Type*;
122 using reference = Type&;
123
124 #ifdef _MSC_VER
125 using _Unchecked_type = pointer;
126 #endif // _MSC_VER
127 constexpr span_iterator() = default;
128
129 constexpr span_iterator(pointer begin, pointer end, pointer current)
130 : begin_(begin), end_(end), current_(current)
131 {}
132
133 constexpr operator span_iterator<const Type>() const noexcept
134 {
135 return {begin_, end_, current_};
136 }
137
138 constexpr reference operator*() const noexcept
139 {
140 Expects(begin_ && end_);
141 Expects(begin_ <= current_ && current_ < end_);
142 return *current_;
143 }
144
145 constexpr pointer operator->() const noexcept
146 {
147 Expects(begin_ && end_);
148 Expects(begin_ <= current_ && current_ < end_);
149 return current_;
150 }
151 constexpr span_iterator& operator++() noexcept
152 {
153 Expects(begin_ && current_ && end_);
154 Expects(current_ < end_);
155 ++current_;
156 return *this;
157 }
158
159 constexpr span_iterator operator++(int) noexcept
160 {
161 span_iterator ret = *this;
162 ++*this;
163 return ret;
164 }
165
166 constexpr span_iterator& operator--() noexcept
167 {
168 Expects(begin_ && end_);
169 Expects(begin_ < current_);
170 --current_;
171 return *this;
172 }
173
174 constexpr span_iterator operator--(int) noexcept
175 {
176 span_iterator ret = *this;
177 --*this;
178 return ret;
179 }
180
181 constexpr span_iterator& operator+=(const difference_type n) noexcept
182 {
183 if (n != 0) Expects(begin_ && current_ && end_);
184 if (n > 0) Expects(end_ - current_ >= n);
185 if (n < 0) Expects(current_ - begin_ >= -n);
186 current_ += n;
187 return *this;
188 }
189
190 constexpr span_iterator operator+(const difference_type n) const noexcept
191 {
192 span_iterator ret = *this;
193 ret += n;
194 return ret;
195 }
196
197 friend constexpr span_iterator operator+(const difference_type n,
198 const span_iterator& rhs) noexcept
199 {
200 return rhs + n;
201 }
202
203 constexpr span_iterator& operator-=(const difference_type n) noexcept
204 {
205 if (n != 0) Expects(begin_ && current_ && end_);
206 if (n > 0) Expects(current_ - begin_ >= n);
207 if (n < 0) Expects(end_ - current_ >= -n);
208 current_ -= n;
209 return *this;
210 }
211
212 constexpr span_iterator operator-(const difference_type n) const noexcept
213 {
214 span_iterator ret = *this;
215 ret -= n;
216 return ret;
217 }
218
219 template <
220 class Type2,
221 std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
222 constexpr difference_type operator-(const span_iterator<Type2>& rhs) const noexcept
223 {
224 Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
225 return current_ - rhs.current_;
226 }
227
228 constexpr reference operator[](const difference_type n) const noexcept
229 {
230 return *(*this + n);
231 }
232
233 template <
234 class Type2,
235 std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
236 constexpr bool operator==(const span_iterator<Type2>& rhs) const noexcept
237 {
238 Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
239 return current_ == rhs.current_;
240 }
241
242 template <
243 class Type2,
244 std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
245 constexpr bool operator!=(const span_iterator<Type2>& rhs) const noexcept
246 {
247 return !(*this == rhs);
248 }
249
250 template <
251 class Type2,
252 std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
253 constexpr bool operator<(const span_iterator<Type2>& rhs) const noexcept
254 {
255 Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
256 return current_ < rhs.current_;
257 }
258
259 template <
260 class Type2,
261 std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
262 constexpr bool operator>(const span_iterator<Type2>& rhs) const noexcept
263 {
264 return rhs < *this;
265 }
266
267 template <
268 class Type2,
269 std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
270 constexpr bool operator<=(const span_iterator<Type2>& rhs) const noexcept
271 {
272 return !(rhs < *this);
273 }
274
275 template <
276 class Type2,
277 std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
278 constexpr bool operator>=(const span_iterator<Type2>& rhs) const noexcept
279 {
280 return !(*this < rhs);
281 }
282
283 #ifdef _MSC_VER
284 // MSVC++ iterator debugging support; allows STL algorithms in 15.8+
285 // to unwrap span_iterator to a pointer type after a range check in STL
286 // algorithm calls
287 friend constexpr void _Verify_range(span_iterator lhs, span_iterator rhs) noexcept
288 { // test that [lhs, rhs) forms a valid range inside an STL algorithm
289 Expects(lhs.begin_ == rhs.begin_ // range spans have to match
290 && lhs.end_ == rhs.end_ &&
291 lhs.current_ <= rhs.current_); // range must not be transposed
292 }
293
294 constexpr void _Verify_offset(const difference_type n) const noexcept
295 { // test that *this + n is within the range of this call
296 if (n != 0) Expects(begin_ && current_ && end_);
297 if (n > 0) Expects(end_ - current_ >= n);
298 if (n < 0) Expects(current_ - begin_ >= -n);
299 }
300
301 // clang-format off
302 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
303 // clang-format on
304 constexpr pointer _Unwrapped() const noexcept
305 { // after seeking *this to a high water mark, or using one of the
306 // _Verify_xxx functions above, unwrap this span_iterator to a raw
307 // pointer
308 return current_;
309 }
310
311 // Tell the STL that span_iterator should not be unwrapped if it can't
312 // validate in advance, even in release / optimized builds:
313 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
314 static constexpr const bool _Unwrap_when_unverified = false;
315 #else
316 static constexpr bool _Unwrap_when_unverified = false;
317 #endif
318 // clang-format off
319 GSL_SUPPRESS(con.3) // NO-FORMAT: attribute // TODO: false positive
320 // clang-format on
321 constexpr void _Seek_to(const pointer p) noexcept
322 { // adjust the position of *this to previously verified location p
323 // after _Unwrapped
324 current_ = p;
325 }
326 #endif
327
328 pointer begin_ = nullptr;
329 pointer end_ = nullptr;
330 pointer current_ = nullptr;
331 };
332
333 template <std::size_t Ext>
334 class extent_type
335 {
336 public:
337 using size_type = std::size_t;
338
339 constexpr extent_type() noexcept = default;
340
341 constexpr explicit extent_type(extent_type<dynamic_extent>);
342
343 constexpr explicit extent_type(size_type size) { Expects(size == Ext); }
344
345 constexpr size_type size() const noexcept { return Ext; }
346
347 private:
348 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
349 static constexpr const size_type size_ = Ext; // static size equal to Ext
350 #else
351 static constexpr size_type size_ = Ext; // static size equal to Ext
352 #endif
353 };
354
355 template <>
356 class extent_type<dynamic_extent>
357 {
358 public:
359 using size_type = std::size_t;
360
361 template <size_type Other>
362 constexpr explicit extent_type(extent_type<Other> ext) : size_(ext.size())
363 {}
364
365 constexpr explicit extent_type(size_type size) : size_(size)
366 {
367 Expects(size != dynamic_extent);
368 }
369
370 constexpr size_type size() const noexcept { return size_; }
371
372 private:
373 size_type size_;
374 };
375
376 template <std::size_t Ext>
377 constexpr extent_type<Ext>::extent_type(extent_type<dynamic_extent> ext)
378 {
379 Expects(ext.size() == Ext);
380 }
381
382 template <class ElementType, std::size_t Extent, std::size_t Offset, std::size_t Count>
383 struct calculate_subspan_type
384 {
385 using type = span<ElementType, Count != dynamic_extent
386 ? Count
387 : (Extent != dynamic_extent ? Extent - Offset : Extent)>;
388 };
389 } // namespace details
390
391 // [span], class template span
392 template <class ElementType, std::size_t Extent>
393 class span
394 {
395 public:
396 // constants and types
397 using element_type = ElementType;
398 using value_type = std::remove_cv_t<ElementType>;
399 using size_type = std::size_t;
400 using pointer = element_type*;
401 using const_pointer = const element_type*;
402 using reference = element_type&;
403 using const_reference = const element_type&;
404 using difference_type = std::ptrdiff_t;
405
406 using iterator = details::span_iterator<ElementType>;
407 using reverse_iterator = std::reverse_iterator<iterator>;
408
409 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
410 static constexpr const size_type extent{Extent};
411 #else
412 static constexpr size_type extent{Extent};
413 #endif
414
415 // [span.cons], span constructors, copy, assignment, and destructor
416 template <bool Dependent = false,
417 // "Dependent" is needed to make "std::enable_if_t<Dependent || Extent == 0 || Extent
418 // == dynamic_extent>" SFINAE, since "std::enable_if_t<Extent == 0 || Extent ==
419 // dynamic_extent>" is ill-formed when Extent is greater than 0.
420 class = std::enable_if_t<(Dependent ||
421 details::is_allowed_extent_conversion<0, Extent>::value)>>
422 constexpr span() noexcept : storage_(nullptr, details::extent_type<0>())
423 {}
424
425 template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent != dynamic_extent, int> = 0>
426 constexpr explicit span(pointer ptr, size_type count) noexcept : storage_(ptr, count)
427 {
428 Expects(count == Extent);
429 }
430
431 template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent == dynamic_extent, int> = 0>
432 constexpr span(pointer ptr, size_type count) noexcept : storage_(ptr, count)
433 {}
434
435 template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent != dynamic_extent, int> = 0>
436 constexpr explicit span(pointer firstElem, pointer lastElem) noexcept
437 : storage_(firstElem, narrow_cast<std::size_t>(lastElem - firstElem))
438 {
439 Expects(lastElem - firstElem == static_cast<difference_type>(Extent));
440 }
441
442 template <std::size_t MyExtent = Extent, std::enable_if_t<MyExtent == dynamic_extent, int> = 0>
443 constexpr span(pointer firstElem, pointer lastElem) noexcept
444 : storage_(firstElem, narrow_cast<std::size_t>(lastElem - firstElem))
445 {}
446
447 template <std::size_t N,
448 std::enable_if_t<details::is_allowed_extent_conversion<N, Extent>::value, int> = 0>
449 constexpr span(element_type (&arr)[N]) noexcept
450 : storage_(KnownNotNull{arr + 0}, details::extent_type<N>())
451 {}
452
453 template <
454 class T, std::size_t N,
455 std::enable_if_t<(details::is_allowed_extent_conversion<N, Extent>::value &&
456 details::is_allowed_element_type_conversion<T, element_type>::value),
457 int> = 0>
458 constexpr span(std::array<T, N>& arr) noexcept
459 : storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
460 {}
461
462 template <class T, std::size_t N,
463 std::enable_if_t<
464 (details::is_allowed_extent_conversion<N, Extent>::value &&
465 details::is_allowed_element_type_conversion<const T, element_type>::value),
466 int> = 0>
467 constexpr span(const std::array<T, N>& arr) noexcept
468 : storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
469 {}
470
471 // NB: the SFINAE on these constructors uses .data() as an incomplete/imperfect proxy for the
472 // requirement on Container to be a contiguous sequence container.
473 template <std::size_t MyExtent = Extent, class Container,
474 std::enable_if_t<
475 MyExtent != dynamic_extent &&
476 !details::is_span<Container>::value && !details::is_std_array<Container>::value &&
477 std::is_pointer<decltype(std::declval<Container&>().data())>::value &&
478 std::is_convertible<
479 std::remove_pointer_t<decltype(std::declval<Container&>().data())> (*)[],
480 element_type (*)[]>::value, int> = 0>
481 constexpr explicit span(Container& cont) noexcept : span(cont.data(), cont.size())
482 {}
483
484 template <std::size_t MyExtent = Extent, class Container,
485 std::enable_if_t<
486 MyExtent == dynamic_extent &&
487 !details::is_span<Container>::value && !details::is_std_array<Container>::value &&
488 std::is_pointer<decltype(std::declval<Container&>().data())>::value &&
489 std::is_convertible<
490 std::remove_pointer_t<decltype(std::declval<Container&>().data())> (*)[],
491 element_type (*)[]>::value, int> = 0>
492 constexpr span(Container& cont) noexcept : span(cont.data(), cont.size())
493 {}
494
495 template <std::size_t MyExtent = Extent, class Container,
496 std::enable_if_t<
497 MyExtent != dynamic_extent &&
498 std::is_const<element_type>::value && !details::is_span<Container>::value &&
499 !details::is_std_array<Container>::value &&
500 std::is_pointer<decltype(std::declval<const Container&>().data())>::value &&
501 std::is_convertible<std::remove_pointer_t<
502 decltype(std::declval<const Container&>().data())> (*)[],
503 element_type (*)[]>::value, int> = 0>
504 constexpr explicit span(const Container& cont) noexcept : span(cont.data(), cont.size())
505 {}
506
507 template <std::size_t MyExtent = Extent, class Container,
508 std::enable_if_t<
509 MyExtent == dynamic_extent &&
510 std::is_const<element_type>::value && !details::is_span<Container>::value &&
511 !details::is_std_array<Container>::value &&
512 std::is_pointer<decltype(std::declval<const Container&>().data())>::value &&
513 std::is_convertible<std::remove_pointer_t<
514 decltype(std::declval<const Container&>().data())> (*)[],
515 element_type (*)[]>::value, int> = 0>
516 constexpr span(const Container& cont) noexcept : span(cont.data(), cont.size())
517 {}
518
519 constexpr span(const span& other) noexcept = default;
520
521 template <
522 class OtherElementType, std::size_t OtherExtent, std::size_t MyExtent = Extent,
523 std::enable_if_t<
524 (MyExtent == dynamic_extent || MyExtent == OtherExtent) &&
525 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value, int> = 0>
526 constexpr span(const span<OtherElementType, OtherExtent>& other) noexcept
527 : storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
528 {}
529
530 template <
531 class OtherElementType, std::size_t OtherExtent, std::size_t MyExtent = Extent,
532 std::enable_if_t<
533 MyExtent != dynamic_extent && OtherExtent == dynamic_extent &&
534 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value, int> = 0>
535 constexpr explicit span(const span<OtherElementType, OtherExtent>& other) noexcept
536 : storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
537 {}
538
539 ~span() noexcept = default;
540 constexpr span& operator=(const span& other) noexcept = default;
541
542 // [span.sub], span subviews
543 template <std::size_t Count>
544 constexpr span<element_type, Count> first() const noexcept
545 {
546 Expects(Count <= size());
547 return span<element_type, Count>{data(), Count};
548 }
549
550 template <std::size_t Count>
551 // clang-format off
552 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
553 // clang-format on
554 constexpr span<element_type, Count> last() const noexcept
555 {
556 Expects(Count <= size());
557 return span<element_type, Count>{data() + (size() - Count), Count};
558 }
559
560 template <std::size_t Offset, std::size_t Count = dynamic_extent>
561 // clang-format off
562 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
563 // clang-format on
564 constexpr auto subspan() const noexcept ->
565 typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
566 {
567 Expects((size() >= Offset) && (Count == dynamic_extent || (Count <= size() - Offset)));
568 using type = typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type;
569 return type{data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
570 }
571
572 constexpr span<element_type, dynamic_extent> first(size_type count) const noexcept
573 {
574 Expects(count <= size());
575 return {data(), count};
576 }
577
578 constexpr span<element_type, dynamic_extent> last(size_type count) const noexcept
579 {
580 Expects(count <= size());
581 return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{});
582 }
583
584 constexpr span<element_type, dynamic_extent> subspan(size_type offset,
585 size_type count = dynamic_extent) const
586 noexcept
587 {
588 return make_subspan(offset, count, subspan_selector<Extent>{});
589 }
590
591 // [span.obs], span observers
592 constexpr size_type size() const noexcept { return storage_.size(); }
593
594 constexpr size_type size_bytes() const noexcept
595 {
596 Expects(size() < dynamic_extent / sizeof(element_type));
597 return size() * sizeof(element_type);
598 }
599
600 constexpr bool empty() const noexcept { return size() == 0; }
601
602 // [span.elem], span element access
603 // clang-format off
604 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
605 // clang-format on
606 constexpr reference operator[](size_type idx) const noexcept
607 {
608 Expects(idx < size());
609 return data()[idx];
610 }
611
612 constexpr reference front() const noexcept
613 {
614 Expects(size() > 0);
615 return data()[0];
616 }
617
618 constexpr reference back() const noexcept
619 {
620 Expects(size() > 0);
621 return data()[size() - 1];
622 }
623
624 constexpr pointer data() const noexcept { return storage_.data(); }
625
626 // [span.iter], span iterator support
627 constexpr iterator begin() const noexcept
628 {
629 const auto data = storage_.data();
630 // clang-format off
631 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
632 // clang-format on
633 return {data, data + size(), data};
634 }
635
636 constexpr iterator end() const noexcept
637 {
638 const auto data = storage_.data();
639 // clang-format off
640 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
641 // clang-format on
642 const auto endData = data + storage_.size();
643 return {data, endData, endData};
644 }
645
646 constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
647 constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
648
649 #ifdef _MSC_VER
650 // Tell MSVC how to unwrap spans in range-based-for
651 constexpr pointer _Unchecked_begin() const noexcept { return data(); }
652 constexpr pointer _Unchecked_end() const noexcept
653 {
654 // clang-format off
655 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
656 // clang-format on
657 return data() + size();
658 }
659 #endif // _MSC_VER
660
661 private:
662 // Needed to remove unnecessary null check in subspans
663 struct KnownNotNull
664 {
665 pointer p;
666 };
667
668 // this implementation detail class lets us take advantage of the
669 // empty base class optimization to pay for only storage of a single
670 // pointer in the case of fixed-size spans
671 template <class ExtentType>
672 class storage_type : public ExtentType
673 {
674 public:
675 // KnownNotNull parameter is needed to remove unnecessary null check
676 // in subspans and constructors from arrays
677 template <class OtherExtentType>
678 constexpr storage_type(KnownNotNull data, OtherExtentType ext)
679 : ExtentType(ext), data_(data.p)
680 {
681 Expects(ExtentType::size() != dynamic_extent);
682 }
683
684 template <class OtherExtentType>
685 constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
686 {
687 Expects(ExtentType::size() != dynamic_extent);
688 Expects(data || ExtentType::size() == 0);
689 }
690
691 constexpr pointer data() const noexcept { return data_; }
692
693 private:
694 pointer data_;
695 };
696
697 storage_type<details::extent_type<Extent>> storage_;
698
699 // The rest is needed to remove unnecessary null check
700 // in subspans and constructors from arrays
701 constexpr span(KnownNotNull ptr, size_type count) noexcept : storage_(ptr, count) {}
702
703 template <std::size_t CallerExtent>
704 class subspan_selector
705 {
706 };
707
708 template <std::size_t CallerExtent>
709 constexpr span<element_type, dynamic_extent> make_subspan(size_type offset, size_type count,
710 subspan_selector<CallerExtent>) const
711 noexcept
712 {
713 const span<element_type, dynamic_extent> tmp(*this);
714 return tmp.subspan(offset, count);
715 }
716
717 // clang-format off
718 GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
719 // clang-format on
720 constexpr span<element_type, dynamic_extent>
721 make_subspan(size_type offset, size_type count, subspan_selector<dynamic_extent>) const noexcept
722 {
723 Expects(size() >= offset);
724
725 if (count == dynamic_extent) { return {KnownNotNull{data() + offset}, size() - offset}; }
726
727 Expects(size() - offset >= count);
728 return {KnownNotNull{data() + offset}, count};
729 }
730 };
731
732 #if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L))
733
734 // Deduction Guides
735 template <class Type, std::size_t Extent>
736 span(Type (&)[Extent])->span<Type, Extent>;
737
738 template <class Type, std::size_t Size>
739 span(std::array<Type, Size>&)->span<Type, Size>;
740
741 template <class Type, std::size_t Size>
742 span(const std::array<Type, Size>&)->span<const Type, Size>;
743
744 template <class Container,
745 class Element = std::remove_pointer_t<decltype(std::declval<Container&>().data())>>
746 span(Container&)->span<Element>;
747
748 template <class Container,
749 class Element = std::remove_pointer_t<decltype(std::declval<const Container&>().data())>>
750 span(const Container&)->span<Element>;
751
752 #endif // ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
753
754 #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
755 template <class ElementType, std::size_t Extent>
756 constexpr const typename span<ElementType, Extent>::size_type span<ElementType, Extent>::extent;
757 #endif
758
759 namespace details
760 {
761 // if we only supported compilers with good constexpr support then
762 // this pair of classes could collapse down to a constexpr function
763
764 // we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
765 // constexpr
766 // and so will fail compilation of the template
767 template <class ElementType, std::size_t Extent>
768 struct calculate_byte_size : std::integral_constant<std::size_t, sizeof(ElementType) * Extent>
769 {
770 static_assert(Extent < dynamic_extent / sizeof(ElementType), "Size is too big.");
771 };
772
773 template <class ElementType>
774 struct calculate_byte_size<ElementType, dynamic_extent>
775 : std::integral_constant<std::size_t, dynamic_extent>
776 {
777 };
778 } // namespace details
779
780 // [span.objectrep], views of object representation
781 template <class ElementType, std::size_t Extent>
782 span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
783 as_bytes(span<ElementType, Extent> s) noexcept
784 {
785 using type = span<const byte, details::calculate_byte_size<ElementType, Extent>::value>;
786
787 // clang-format off
788 GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
789 // clang-format on
790 return type{reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
791 }
792
793 template <class ElementType, std::size_t Extent,
794 std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
795 span<byte, details::calculate_byte_size<ElementType, Extent>::value>
796 as_writable_bytes(span<ElementType, Extent> s) noexcept
797 {
798 using type = span<byte, details::calculate_byte_size<ElementType, Extent>::value>;
799
800 // clang-format off
801 GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
802 // clang-format on
803 return type{reinterpret_cast<byte*>(s.data()), s.size_bytes()};
804 }
805
806 } // namespace gsl
807
808 #if defined(_MSC_VER) && !defined(__clang__)
809
810 #pragma warning(pop)
811 #endif // _MSC_VER
812
813 #if defined(__GNUC__) && __GNUC__ > 6
814 #pragma GCC diagnostic pop
815 #endif // __GNUC__ > 6
816
817 #endif // GSL_SPAN_H