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
10USERVER_NAMESPACE_BEGIN
11
12namespace utils::statistics {
13
14class Label;
15
16/// @brief Non owning label name+value storage.
17class LabelView final {
18public:
19 LabelView() = default;
20 LabelView(Label&& label) = delete;
21 explicit LabelView(const Label& label) noexcept;
22 LabelView(std::string_view name, std::string_view value) noexcept : name_(name), value_(value) {}
23
24 explicit operator bool() const { return !name_.empty(); }
25
26 std::string_view Name() const { return name_; }
27 std::string_view Value() const { return value_; }
28
29private:
30 std::string_view name_;
31 std::string_view value_;
32};
33
34bool operator<(const LabelView& x, const LabelView& y) noexcept;
35bool operator==(const LabelView& x, const LabelView& y) noexcept;
36
37/// @brief Label name+value storage.
38class Label final {
39public:
40 Label() = default;
41 explicit Label(LabelView view);
42 Label(std::string name, std::string value);
43
44 explicit operator bool() const { return !name_.empty(); }
45 explicit operator LabelView() const { return {name_, value_}; }
46
47 const std::string& Name() const { return name_; }
48 const std::string& Value() const& { return value_; }
49 std::string& Value() & { return value_; }
50 std::string&& Value() && { return std::move(value_); }
51
52private:
53 std::string name_;
54 std::string value_;
55};
56
57bool operator<(const Label& x, const Label& y) noexcept;
58bool operator==(const Label& x, const Label& y) noexcept;
59
60/// @brief View over a continuous range of LabelView.
61class LabelsSpan final {
62public:
63 using iterator = const LabelView*;
64 using const_iterator = const LabelView*;
65
66 LabelsSpan() = default;
67 LabelsSpan(const LabelView* begin, const LabelView* end) noexcept;
68 LabelsSpan(std::initializer_list<LabelView> il) noexcept : LabelsSpan(il.begin(), il.end()) {}
69
70 template <
71 class Container,
72 std::enable_if_t<
73 std::is_same_v<
74 decltype(*(std::declval<const Container&>().data() + std::declval<const Container&>().size())),
75 const LabelView&>,
76 int> = 0>
77 /*implicit*/ LabelsSpan(const Container& cont) noexcept : LabelsSpan(cont.data(), cont.data() + cont.size()) {}
78
79 const LabelView* begin() const noexcept { return begin_; }
80 const LabelView* end() const noexcept { return end_; }
81 std::size_t size() const noexcept { return end_ - begin_; }
82 bool empty() const noexcept { return end_ == begin_; }
83
84private:
85 const LabelView* begin_{nullptr};
86 const LabelView* end_{nullptr};
87};
88
89} // namespace utils::statistics
90
91USERVER_NAMESPACE_END