userver: userver/utils/statistics/labels.hpp Source File
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 <string>
7#include <string_view>
8#include <type_traits>
9
10#include <userver/utils/assert.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace utils::statistics {
15
16class Label;
17
18/// @brief Non owning label name+value storage.
19class LabelView final {
20public:
21 LabelView() = delete;
22 LabelView(Label&& label) = delete;
23 explicit LabelView(const Label& label);
24 constexpr LabelView(std::string_view name, std::string_view value)
25 : name_(name),
26 value_(value)
27 {
28 UINVARIANT(!name_.empty(), "The label name must not be empty.");
29 }
30
31 template <class T, std::enable_if_t<std::is_arithmetic_v<T>>* = nullptr>
32 constexpr LabelView(std::string_view, T) {
33 static_assert(sizeof(T) && false, "Labels should not be arithmetic values, only strings!");
34 }
35
36 constexpr explicit operator bool() const { return !name_.empty(); }
37
38 constexpr std::string_view Name() const { return name_; }
39 constexpr std::string_view Value() const { return value_; }
40
41private:
42 std::string_view name_{};
43 std::string_view value_{};
44};
45
46bool operator<(const LabelView& x, const LabelView& y) noexcept;
47bool operator==(const LabelView& x, const LabelView& y) noexcept;
48
49/// @brief Label name+value storage.
50class Label final {
51public:
52 Label() = default;
53 explicit Label(LabelView view);
54 Label(std::string name, std::string value);
55
56 template <class T, std::enable_if_t<std::is_arithmetic_v<T>>* = nullptr>
57 Label(std::string, T) {
58 static_assert(sizeof(T) && false, "Labels should not be arithmetic values, only strings!");
59 }
60
61 explicit operator bool() const { return !name_.empty(); }
62 explicit operator LabelView() const { return {name_, value_}; }
63
64 const std::string& Name() const { return name_; }
65 const std::string& Value() const& { return value_; }
66 std::string& Value() & { return value_; }
67 std::string&& Value() && { return std::move(value_); }
68
69private:
70 std::string name_;
71 std::string value_;
72};
73
74bool operator<(const Label& x, const Label& y) noexcept;
75bool operator==(const Label& x, const Label& y) noexcept;
76
77/// @brief View over a continuous range of LabelView.
78class LabelsSpan final {
79public:
80 using iterator = const LabelView*;
81 using const_iterator = const LabelView*;
82
83 LabelsSpan() = default;
84 LabelsSpan(const LabelView* begin, const LabelView* end) noexcept;
85 LabelsSpan(std::initializer_list<LabelView> il) noexcept : LabelsSpan(il.begin(), il.end()) {}
86
87 template <
88 class Container,
89 std::enable_if_t<
90 std::is_same_v<
91 decltype(*(std::declval<const Container&>().data() + std::declval<const Container&>().size())),
92 const LabelView&>,
93 int> = 0>
94 /*implicit*/ LabelsSpan(const Container& cont) noexcept : LabelsSpan(cont.data(), cont.data() + cont.size()) {}
95
96 const LabelView* begin() const noexcept { return begin_; }
97 const LabelView* end() const noexcept { return end_; }
98 std::size_t size() const noexcept { return end_ - begin_; }
99 bool empty() const noexcept { return end_ == begin_; }
100
101private:
102 const LabelView* begin_{nullptr};
103 const LabelView* end_{nullptr};
104};
105
106} // namespace utils::statistics
107
108USERVER_NAMESPACE_END