userver
C++ Async Framework
Toggle main menu visibility
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
<
concepts
>
7
#
include
<
cstddef
>
8
#
include
<
iterator
>
9
#
include
<
span
>
10
#
include
<
type_traits
>
11
12
USERVER_NAMESPACE_BEGIN
13
14
namespace
utils
{
15
16
/// A polyfill for std::span with some of the newer features enabled.
17
template
<
typename
T>
18
class
span
:
public
std::span<T> {
// NOLINT(readability-identifier-naming)
19
public
:
20
// std::span gains this alias only in C++23.
21
using
const_iterator =
typename
std::span<T>::iterator;
22
23
using
std::span<T>::span;
24
25
constexpr
explicit
(
false
) span(std::span<T> s)
noexcept
: std::span<T>(s) {}
26
27
// Allows converting utils::span<U> to utils::span<T>, following std::span.
28
template
<
typename
U>
29
requires
std::is_convertible_v<U (*)[], T (*)[]>
30
constexpr
explicit
(
false
) span(
span
<U> other)
noexcept
: std::span<T>(other.data(), other.size()) {}
31
32
// std::span will only gain this constructor in C++29 or later.
33
constexpr
explicit
(
false
) span(std::initializer_list<std::remove_cv_t<T>> il)
noexcept
34
requires
std::is_const_v<T> && (!std::same_as<std::decay_t<T>,
bool
>)
35
: std::span<T>(il.begin(), il.end()) {}
36
};
37
38
template
<
typename
It,
typename
EndOrSize>
39
span(It, EndOrSize) ->
span
<std::remove_reference_t<
decltype
(*std::declval<It&>())>>;
40
41
template
<
typename
R>
42
requires
std::ranges::contiguous_range<R>
43
span(R&&) ->
span
<std::remove_reference_t<
decltype
(*std::begin(std::declval<R&>()))>>;
44
45
/// Reinterprets the elements of a span as bytes.
46
template
<
typename
T>
47
span
<
const
std::byte>
as_bytes
(
span
<T> s)
noexcept
{
// NOLINT(readability-identifier-naming)
48
return
span
<
const
std::byte>{std::as_bytes(std::span<T>{s})};
49
}
50
51
/// Reinterprets the elements of a span as writable bytes.
52
template
<
typename
T>
53
requires
(!std::is_const_v<T>)
54
span
<std::byte>
as_writable_bytes
(
span
<T> s)
noexcept
{
// NOLINT(readability-identifier-naming)
55
return
span
<std::byte>{std::as_writable_bytes(std::span<T>{s})};
56
}
57
58
}
// namespace utils
59
60
USERVER_NAMESPACE_END
61
62
/// @cond
63
// std::span must implement std::ranges::enable_borrowed_range, so <span> will pull it in.
64
template
<
typename
T>
65
inline
constexpr
bool
std::ranges::enable_borrowed_range<USERVER_NAMESPACE::utils::span<T>> =
true
;
66
67
// std::span must implement std::ranges::enable_view, so <span> will pull it in.
68
template
<
typename
T>
69
inline
constexpr
bool
std::ranges::enable_view<USERVER_NAMESPACE::utils::span<T>> =
true
;
70
/// @endcond
userver
utils
span.hpp
Generated on
for userver by
Doxygen
1.17.0