userver: userver/utils/span.hpp Source File
Loading...
Searching...
No Matches
span.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/span.hpp
4/// @brief @copybrief utils::span
5
6#include <cstddef>
7#include <iterator>
8#include <type_traits>
9
10#include <userver/utils/assert.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace utils {
15
16namespace impl {
17
18template <typename T>
19struct TypeIdentityImpl final {
20 using type = T;
21};
22
23template <typename T>
24using TypeIdentity = typename TypeIdentityImpl<T>::type;
25
26} // namespace impl
27
28/// A polyfill for std::span from C++20
29template <typename T>
30class span final { // NOLINT(readability-identifier-naming)
31public:
32 using iterator = T*;
33 using value_type = std::remove_cv_t<T>;
34
35 constexpr span() noexcept : span(nullptr, nullptr) {}
36
37 constexpr span(T* begin, T* end) noexcept : begin_(begin), end_(end) {
38 UASSERT((begin != nullptr && end != nullptr && begin <= end) || (begin == nullptr && end == nullptr));
39 }
40
41 constexpr span(T* begin, std::size_t size) noexcept : begin_(begin), end_(begin + size) {
42 UASSERT(begin != nullptr || size == 0);
43 }
44
45#if defined(__GNUC__) && !defined(__clang__)
46#if __GNUC__ >= 9
47#pragma GCC diagnostic push
48#pragma GCC diagnostic ignored "-Winit-list-lifetime"
49#endif
50#endif
51 template <typename Void = void, typename = std::enable_if_t<std::is_const_v<T> && std::is_void_v<Void>>>
52 constexpr /*implicit*/ span(std::initializer_list<value_type> il)
53 : begin_(il.begin()),
54 end_(il.end())
55 {}
56#if defined(__GNUC__) && !defined(__clang__)
57#if __GNUC__ >= 9
58#pragma GCC diagnostic pop
59#endif
60#endif
61
62 template <
63 typename Container,
64 typename = std::enable_if_t<
65 // Either Container is lvalue, or this span's elements are const
66 (std::is_lvalue_reference_v<Container> || std::is_const_v<T>)&&
67 // Copy and move constructor fix
68 !std::is_same_v<std::remove_cv_t<std::remove_reference_t<Container>>, span> &&
69 // Container is a range of T
70 std::is_convertible_v<decltype(std::data(std::declval<Container&>())), T*>>>
71 // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
72 constexpr /*implicit*/ span(Container&& cont) noexcept : span(std::data(cont), std::size(cont)) {}
73
74 template <std::size_t Size>
75 constexpr /*implicit*/ span(impl::TypeIdentity<T> (&array)[Size]) noexcept
76 : span(std::data(array), std::size(array)) {}
77
78 constexpr T* begin() const noexcept { return begin_; }
79 constexpr T* end() const noexcept { return end_; }
80
81 constexpr T* data() const noexcept { return begin_; }
82 constexpr std::size_t size() const noexcept { return end_ - begin_; }
83 constexpr bool empty() const noexcept { return size() == 0; }
84
85 constexpr span<T> first(std::size_t count) const noexcept {
86 UASSERT(count <= size());
87 return span{begin_, begin_ + count};
88 }
89
90 constexpr span<T> last(std::size_t count) const noexcept {
91 UASSERT(count <= size());
92 return span{end_ - count, end_};
93 }
94
95 constexpr span<T> subspan(std::size_t offset) const noexcept {
96 UASSERT(offset <= size());
97 return span{begin_ + offset, end_};
98 }
99
100 constexpr span<T> subspan(
101 std::size_t offset, //
102 std::size_t count
103 ) const noexcept {
104 UASSERT(offset + count <= size());
105 return span{begin_ + offset, begin_ + offset + count};
106 }
107
108 constexpr T& operator[](std::size_t index) const noexcept {
109 UASSERT(index < size());
110 return begin_[index];
111 }
112
113private:
114 T* begin_;
115 T* end_;
116};
117
118template <typename Container>
119span(Container&& cont) -> span<std::remove_reference_t<decltype(*std::begin(cont))>>;
120
121/// A polyfill for std::as_bytes from C++20
122template <typename T>
123span<const std::byte> as_bytes(span<T> s) noexcept { // NOLINT(readability-identifier-naming)
124 const auto* const data = reinterpret_cast<const std::byte*>(s.data());
125 return {data, data + s.size() * sizeof(T)};
126}
127
128/// A polyfill for std::as_writable_bytes from C++20
129template <typename T, typename = std::enable_if_t<!std::is_const_v<T>>>
130span<std::byte> as_writable_bytes(span<T> s) noexcept { // NOLINT(readability-identifier-naming)
131 auto* const data = reinterpret_cast<std::byte*>(s.data());
132 return {data, data + s.size() * sizeof(T)};
133}
134
135} // namespace utils
136
137USERVER_NAMESPACE_END
138
139/// @cond
140
141// Boost requires ranges to have a nested constant_iterator alias,
142// but utils::span does not have one.
143namespace boost {
144
145template <typename T, typename Enabler>
146struct range_const_iterator;
147
148template <typename T>
149struct range_const_iterator<USERVER_NAMESPACE::utils::span<T>, void> {
150 using type = typename USERVER_NAMESPACE::utils::span<T>::iterator;
151};
152
153} // namespace boost
154
155/// @endcond