10#include <userver/utils/assert.hpp>
12USERVER_NAMESPACE_BEGIN
14namespace utils::statistics {
19class LabelView
final {
22 LabelView(Label&& label) =
delete;
23 explicit LabelView(
const Label& label);
24 constexpr LabelView(std::string_view name, std::string_view value) : name_(name), value_(value) {
25 UINVARIANT(!name_.empty(),
"The label name must not be empty.");
28 template <
class T, std::enable_if_t<std::is_arithmetic_v<T>>* =
nullptr>
29 constexpr LabelView(std::string_view, T) {
30 static_assert(
sizeof(T) &&
false,
"Labels should not be arithmetic values, only strings!");
33 constexpr explicit operator
bool()
const {
return !name_.empty(); }
35 constexpr std::string_view Name()
const {
return name_; }
36 constexpr std::string_view Value()
const {
return value_; }
39 std::string_view name_{};
40 std::string_view value_{};
43bool operator<(
const LabelView& x,
const LabelView& y)
noexcept;
44bool operator==(
const LabelView& x,
const LabelView& y)
noexcept;
50 explicit Label(LabelView view);
51 Label(std::string name, std::string value);
53 template <
class T, std::enable_if_t<std::is_arithmetic_v<T>>* =
nullptr>
54 Label(std::string, T) {
55 static_assert(
sizeof(T) &&
false,
"Labels should not be arithmetic values, only strings!");
58 explicit operator
bool()
const {
return !name_.empty(); }
59 explicit operator LabelView()
const {
return {name_, value_}; }
61 const std::string& Name()
const {
return name_; }
62 const std::string& Value()
const& {
return value_; }
63 std::string& Value() & {
return value_; }
64 std::string&& Value() && {
return std::move(value_); }
71bool operator<(
const Label& x,
const Label& y)
noexcept;
72bool operator==(
const Label& x,
const Label& y)
noexcept;
75class LabelsSpan
final {
77 using iterator =
const LabelView*;
78 using const_iterator =
const LabelView*;
80 LabelsSpan() =
default;
81 LabelsSpan(
const LabelView* begin,
const LabelView* end)
noexcept;
82 LabelsSpan(std::initializer_list<LabelView> il)
noexcept : LabelsSpan(il.begin(), il.end()) {}
88 decltype(*(std::declval<
const Container&>().data() + std::declval<
const Container&>().size())),
91 LabelsSpan(
const Container& cont)
noexcept : LabelsSpan(cont.data(), cont.data() + cont.size()) {}
93 const LabelView* begin()
const noexcept {
return begin_; }
94 const LabelView* end()
const noexcept {
return end_; }
95 std::size_t size()
const noexcept {
return end_ - begin_; }
96 bool empty()
const noexcept {
return end_ == begin_; }
99 const LabelView* begin_{
nullptr};
100 const LabelView* end_{
nullptr};