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