prom 0.1.0
Client-independent C++23 Prometheus/OpenMetrics metric abstraction
Loading...
Searching...
No Matches
dimval.hpp
Go to the documentation of this file.
1#pragma once
2
12
13#include <prom/unit.hpp>
14
15#include <concepts>
16#include <type_traits>
17
18namespace prom {
19
24template <class V>
26 !std::is_arithmetic_v<std::remove_cvref_t<V>> &&
27 requires(const std::remove_cvref_t<V>& value) {
28 typename std::remove_cvref_t<V>::value_t;
29 { value.numeric_as_double() } -> std::convertible_to<double>;
30 } &&
31 (requires(const std::remove_cvref_t<V>& value) { value.unit_descriptor(); } ||
32 requires(const std::remove_cvref_t<V>& value) { value.descriptor(); });
33
37 double value = 0.0;
39};
40
46template <DimensionalValue V>
47[[nodiscard]] NormalizedValue normalize(const V& value) {
49 out.value = static_cast<double>(value.numeric_as_double());
50 if constexpr (requires { value.unit_descriptor(); }) {
51 const auto desc = value.unit_descriptor();
52 out.unit = Unit{desc.long_name, desc.kind, desc.symbol, true};
53 } else {
54 const auto desc = value.descriptor();
55 out.unit = Unit{desc.long_name, desc.kind, desc.symbol, true};
56 }
57 return out;
58}
59
61template <class T>
62 requires std::is_arithmetic_v<T>
63[[nodiscard]] NormalizedValue normalize(T value) noexcept {
64 return NormalizedValue{static_cast<double>(value), Unit{}};
65}
66
67} // namespace prom
Satisfied by a dimval-like value: it exposes a value_t, reports its magnitude through numeric_as_doub...
Definition dimval.hpp:25
Definition adapter.hpp:24
NormalizedValue normalize(const V &value)
Reduce a dimval value to a NormalizedValue.
Definition dimval.hpp:47
A plain magnitude paired with the unit it was carrying (empty for raw arithmetic).
Definition dimval.hpp:36
double value
Definition dimval.hpp:37
Unit unit
Definition dimval.hpp:38
OpenMetrics unit suffix plus optional dimensional metadata.
Definition unit.hpp:53
std::string_view kind
Dimensional compatibility group (e.g. "time").
Definition unit.hpp:55
Metric kinds (MetricType) and the OpenMetrics Unit descriptor.