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