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) : name_(name), value_(value) {
25 UINVARIANT(!name_.empty(), "The label name must not be empty.");
26 }
27
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!");
31 }
32
33 constexpr explicit operator bool() const { return !name_.empty(); }
34
35 constexpr std::string_view Name() const { return name_; }
36 constexpr std::string_view Value() const { return value_; }
37
38private:
39 std::string_view name_{};
40 std::string_view value_{};
41};
42
43bool operator<(const LabelView& x, const LabelView& y) noexcept;
44bool operator==(const LabelView& x, const LabelView& y) noexcept;
45
46/// @brief Label name+value storage.
47class Label final {
48public:
49 Label() = default;
50 explicit Label(LabelView view);
51 Label(std::string name, std::string value);
52
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!");
56 }
57
58 explicit operator bool() const { return !name_.empty(); }
59 explicit operator LabelView() const { return {name_, value_}; }
60
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_); }
65
66private:
67 std::string name_;
68 std::string value_;
69};
70
71bool operator<(const Label& x, const Label& y) noexcept;
72bool operator==(const Label& x, const Label& y) noexcept;
73
74/// @brief View over a continuous range of LabelView.
75class LabelsSpan final {
76public:
77 using iterator = const LabelView*;
78 using const_iterator = const LabelView*;
79
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()) {}
83
84 template <
85 class Container,
86 std::enable_if_t<
87 std::is_same_v<
88 decltype(*(std::declval<const Container&>().data() + std::declval<const Container&>().size())),
89 const LabelView&>,
90 int> = 0>
91 /*implicit*/ LabelsSpan(const Container& cont) noexcept : LabelsSpan(cont.data(), cont.data() + cont.size()) {}
92
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_; }
97
98private:
99 const LabelView* begin_{nullptr};
100 const LabelView* end_{nullptr};
101};
102
103} // namespace utils::statistics
104
105USERVER_NAMESPACE_END