userver: userver/utils/span.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 {
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) : begin_(il.begin()), end_(il.end()) {}
53#if defined(__GNUC__) && !defined(__clang__)
54#if __GNUC__ >= 9
55#pragma GCC diagnostic pop
56#endif
57#endif
58
59 template <
60 typename Container,
61 typename = std::enable_if_t<
62 // Either Container is lvalue, or this span's elements are const
63 (std::is_lvalue_reference_v<Container> || std::is_const_v<T>)&&
64 // Copy and move constructor fix
65 !std::is_same_v<std::remove_cv_t<std::remove_reference_t<Container>>, span> &&
66 // Container is a range of T
67 std::is_convertible_v<decltype(std::data(std::declval<Container&>())), T*>>>
68 // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
69 constexpr /*implicit*/ span(Container&& cont) noexcept : span(std::data(cont), std::size(cont)) {}
70
71 template <std::size_t Size>
72 constexpr /*implicit*/ span(impl::TypeIdentity<T> (&array)[Size]) noexcept
73 : span(std::data(array), std::size(array)) {}
74
75 constexpr T* begin() const noexcept { return begin_; }
76 constexpr T* end() const noexcept { return end_; }
77
78 constexpr T* data() const noexcept { return begin_; }
79 constexpr std::size_t size() const noexcept { return end_ - begin_; }
80 constexpr bool empty() const noexcept { return size() == 0; }
81
82 constexpr span<T> first(std::size_t count) const noexcept {
83 UASSERT(count <= size());
84 return span{begin_, begin_ + count};
85 }
86
87 constexpr span<T> last(std::size_t count) const noexcept {
88 UASSERT(count <= size());
89 return span{end_ - count, end_};
90 }
91
92 constexpr span<T> subspan(std::size_t offset) const noexcept {
93 UASSERT(offset <= size());
94 return span{begin_ + offset, end_};
95 }
96
97 constexpr span<T> subspan(
98 std::size_t offset, //
99 std::size_t count
100 ) const noexcept {
101 UASSERT(offset + count <= size());
102 return span{begin_ + offset, begin_ + offset + count};
103 }
104
105 constexpr T& operator[](std::size_t index) const noexcept {
106 UASSERT(index < size());
107 return begin_[index];
108 }
109
110private:
111 T* begin_;
112 T* end_;
113};
114
115template <typename Container>
116span(Container&& cont) -> span<std::remove_reference_t<decltype(*std::begin(cont))>>;
117
118/// A polyfill for std::as_bytes from C++20
119template <typename T>
120span<const std::byte> as_bytes(span<T> s) noexcept {
121 const auto* const data = reinterpret_cast<const std::byte*>(s.data());
122 return {data, data + s.size() * sizeof(T)};
123}
124
125/// A polyfill for std::as_writable_bytes from C++20
126template <typename T, typename = std::enable_if_t<!std::is_const_v<T>>>
127span<std::byte> as_writable_bytes(span<T> s) noexcept {
128 auto* const data = reinterpret_cast<std::byte*>(s.data());
129 return {data, data + s.size() * sizeof(T)};
130}
131
132} // namespace utils
133
134USERVER_NAMESPACE_END
135
136/// @cond
137
138// Boost requires ranges to have a nested constant_iterator alias,
139// but utils::span does not have one.
140namespace boost {
141
142template <typename T, typename Enabler>
143struct range_const_iterator;
144
145template <typename T>
146struct range_const_iterator<USERVER_NAMESPACE::utils::span<T>, void> {
147 using type = typename USERVER_NAMESPACE::utils::span<T>::iterator;
148};
149
150} // namespace boost
151
152/// @endcond