userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
labels.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/utils/statistics/labels.hpp
4
/// @brief Owning and non owning labels representations.
5
6
#
include
<
compare
>
7
#
include
<
string
>
8
#
include
<
string_view
>
9
#
include
<
type_traits
>
10
11
#
include
<
fmt
/
core
.
h
>
12
13
#
include
<
userver
/
utils
/
assert
.
hpp
>
14
15
USERVER_NAMESPACE_BEGIN
16
17
namespace
utils
::
statistics
{
18
19
class
Label;
20
21
/// @ingroup userver_universal
22
///
23
/// @brief Non owning label name+value storage.
24
class
LabelView
final
{
25
public
:
26
LabelView() =
delete
;
27
LabelView(Label&& label) =
delete
;
28
explicit
LabelView(
const
Label& label);
29
constexpr
LabelView(std::string_view name, std::string_view value)
30
: name_(name),
31
value_(value)
32
{
33
UINVARIANT
(!name_.empty(),
"The label name must not be empty."
);
34
}
35
36
template
<
class
T>
37
requires
std::is_arithmetic_v<T>
38
constexpr
LabelView(std::string_view, T) {
39
static_assert
(!
sizeof
(T),
"Labels should not be arithmetic values, only strings!"
);
40
}
41
42
constexpr
explicit
operator
bool
()
const
{
return
!name_.empty(); }
43
44
constexpr
std::string_view Name()
const
{
return
name_; }
45
constexpr
std::string_view Value()
const
{
return
value_; }
46
47
private
:
48
std::string_view name_{};
49
std::string_view value_{};
50
};
51
52
std::strong_ordering operator<=>(
const
LabelView& x,
const
LabelView& y)
noexcept
;
53
bool
operator==(
const
LabelView& x,
const
LabelView& y)
noexcept
;
54
55
/// @ingroup userver_universal
56
///
57
/// @brief Label name+value storage.
58
class
Label
final
{
59
public
:
60
Label() =
default
;
61
explicit
Label(LabelView view);
62
Label(std::string name, std::string value);
63
64
template
<
class
T>
65
requires
std::is_arithmetic_v<T>
66
Label(std::string, T) {
67
static_assert
(!
sizeof
(T),
"Labels should not be arithmetic values, only strings!"
);
68
}
69
70
explicit
operator
bool
()
const
{
return
!name_.empty(); }
71
explicit
operator LabelView()
const
{
return
{name_, value_}; }
72
73
const
std::string& Name()
const
{
return
name_; }
74
const
std::string& Value()
const
& {
return
value_; }
75
std::string& Value() & {
return
value_; }
76
std::string&& Value() && {
return
std::move(value_); }
77
78
private
:
79
std::string name_;
80
std::string value_;
81
};
82
83
std::strong_ordering operator<=>(
const
Label& x,
const
Label& y)
noexcept
;
84
bool
operator==(
const
Label& x,
const
Label& y)
noexcept
;
85
86
/// @ingroup userver_universal
87
///
88
/// @brief View over a continuous range of LabelView.
89
class
LabelsSpan
final
{
90
public
:
91
using
iterator =
const
LabelView*;
92
using
const_iterator =
const
LabelView*;
93
94
LabelsSpan() =
default
;
95
LabelsSpan(
const
LabelView* begin,
const
LabelView* end)
noexcept
;
96
LabelsSpan(std::initializer_list<LabelView> il)
noexcept
: LabelsSpan(il.begin(), il.end()) {}
97
98
template
<
class
Container>
99
requires
std::is_same_v<
100
decltype
(*(std::declval<
const
Container&>().data() + std::declval<
const
Container&>().size())),
101
const
LabelView&>
102
/*implicit*/
LabelsSpan(
const
Container& cont)
noexcept
: LabelsSpan(cont.data(), cont.data() + cont.size()) {}
103
104
const
LabelView* begin()
const
noexcept
{
return
begin_; }
105
const
LabelView* end()
const
noexcept
{
return
end_; }
106
std::size_t size()
const
noexcept
{
return
end_ - begin_; }
107
bool
empty()
const
noexcept
{
return
end_ == begin_; }
108
109
private
:
110
const
LabelView* begin_{
nullptr
};
111
const
LabelView* end_{
nullptr
};
112
};
113
114
}
// namespace utils::statistics
115
116
USERVER_NAMESPACE_END
117
118
template
<>
119
struct
fmt
::
formatter
<USERVER_NAMESPACE::
utils
::
statistics
::LabelView> {
120
constexpr
static
auto
parse(format_parse_context& ctx) {
return
ctx.begin(); }
121
122
template
<
typename
FormatContext>
123
auto
format(USERVER_NAMESPACE::
utils
::
statistics
::LabelView value, FormatContext& ctx)
const
{
124
return
fmt
::format_to(ctx.out(),
"{}={}"
, value.Name(), value.Value());
125
}
126
};
127
128
template
<>
129
struct
fmt
::
formatter
<USERVER_NAMESPACE::
utils
::
statistics
::Label>
130
:
public
fmt
::formatter<USERVER_NAMESPACE::
utils
::
statistics
::LabelView> {
131
template
<
typename
FormatContext>
132
auto
format(
const
USERVER_NAMESPACE::
utils
::
statistics
::Label& value, FormatContext& ctx)
const
{
133
return
formatter<USERVER_NAMESPACE::
utils
::
statistics
::LabelView>::format(
134
USERVER_NAMESPACE::
utils
::
statistics
::LabelView{value},
135
ctx
136
);
137
}
138
};
139
140
template
<>
141
struct
fmt
::
formatter
<USERVER_NAMESPACE::
utils
::
statistics
::LabelsSpan> {
142
constexpr
static
auto
parse(format_parse_context& ctx) {
return
ctx.begin(); }
143
144
fmt
::format_context::iterator format(
145
USERVER_NAMESPACE::
utils
::
statistics
::LabelsSpan value,
146
fmt
::format_context& ctx
147
)
const
;
148
};
userver
utils
statistics
labels.hpp
Generated on
for userver by
Doxygen
1.17.0