1 // This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
2 // the main distribution directory for license terms and copyright or visit
3 // https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
4 
5 #pragma once
6 
7 #include "caf/detail/comparable.hpp"
8 #include "caf/detail/core_export.hpp"
9 #include "caf/fwd.hpp"
10 #include "caf/hash/fnv.hpp"
11 #include "caf/string_view.hpp"
12 
13 namespace caf::telemetry {
14 
15 class CAF_CORE_EXPORT label_view : detail::comparable<label_view>,
16                                    detail::comparable<label_view, label> {
17 public:
18   // -- constructors, destructors, and assignment operators --------------------
19 
20   label_view() = delete;
21 
22   label_view(const label_view&) = default;
23 
24   label_view& operator=(const label_view&) = default;
25 
26   /// @pre `key` matches the regex `[a-zA-Z_:][a-zA-Z0-9_:]*`
label_view(string_view name,string_view value)27   label_view(string_view name, string_view value) : name_(name), value_(value) {
28     // nop
29   }
30 
31   // -- properties -------------------------------------------------------------
32 
name() const33   string_view name() const noexcept {
34     return name_;
35   }
36 
value() const37   string_view value() const noexcept {
38     return value_;
39   }
40 
41   // -- comparison -------------------------------------------------------------
42 
43   int compare(const label& x) const noexcept;
44 
45   int compare(const label_view& x) const noexcept;
46 
47 private:
48   string_view name_;
49   string_view value_;
50 };
51 
52 /// Returns the @ref label_view in `name=value` notation.
53 /// @relates label_view
54 CAF_CORE_EXPORT std::string to_string(const label_view& x);
55 
56 } // namespace caf::telemetry
57 
58 namespace std {
59 
60 template <>
61 struct hash<caf::telemetry::label_view> {
operator ()std::hash62   size_t operator()(const caf::telemetry::label_view& x) const noexcept {
63     return caf::hash::fnv<size_t>::compute(x.name(), '=', x.value());
64   }
65 };
66 
67 } // namespace std
68