|
| 1 | +/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2025-Current Couchbase, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#pragma once |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | +#include <optional> |
| 22 | +#include <string> |
| 23 | +#include <utility> |
| 24 | +#include <variant> |
| 25 | + |
| 26 | +namespace couchbase::core |
| 27 | +{ |
| 28 | +class metric_measurement |
| 29 | +{ |
| 30 | +public: |
| 31 | + template<typename Float, std::enable_if_t<std::is_floating_point_v<Float>, int> = 0> |
| 32 | + metric_measurement(std::string name, Float value) |
| 33 | + : name_{ std::move(name) } |
| 34 | + , value_{ value } |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + template< |
| 39 | + typename Integer, |
| 40 | + std::enable_if_t<std::is_integral_v<Integer> && std::is_signed_v<Integer> && |
| 41 | + !std::is_same_v<Integer, bool> && sizeof(Integer) <= sizeof(std::int64_t), |
| 42 | + int> = 0> |
| 43 | + metric_measurement(std::string name, Integer value) |
| 44 | + : name_{ std::move(name) } |
| 45 | + , value_{ value } |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + metric_measurement(const metric_measurement&) = default; |
| 50 | + metric_measurement(metric_measurement&&) = default; |
| 51 | + ~metric_measurement() = default; |
| 52 | + auto operator=(const metric_measurement&) -> metric_measurement& = default; |
| 53 | + auto operator=(metric_measurement&&) -> metric_measurement& = default; |
| 54 | + |
| 55 | + [[nodiscard]] auto is_double() const noexcept -> bool; |
| 56 | + [[nodiscard]] auto as_double() const -> double; |
| 57 | + [[nodiscard]] auto try_as_double() && -> std::optional<double>; |
| 58 | + explicit operator double() const |
| 59 | + { |
| 60 | + return as_double(); |
| 61 | + } |
| 62 | + |
| 63 | + [[nodiscard]] auto is_int64() const noexcept -> bool; |
| 64 | + [[nodiscard]] auto as_int64() const -> std::int64_t; |
| 65 | + [[nodiscard]] auto try_as_int64() && -> std::optional<std::int64_t>; |
| 66 | + explicit operator std::int64_t() const |
| 67 | + { |
| 68 | + return as_int64(); |
| 69 | + } |
| 70 | + |
| 71 | + friend auto operator==(const metric_measurement& lhs, const metric_measurement& rhs) -> bool; |
| 72 | + |
| 73 | +private: |
| 74 | + std::string name_; |
| 75 | + std::variant<double, std::int64_t> value_; |
| 76 | +}; |
| 77 | + |
| 78 | +inline auto |
| 79 | +operator==(const metric_measurement& lhs, const metric_measurement& rhs) -> bool |
| 80 | +{ |
| 81 | + return lhs.name_ == rhs.name_ && // |
| 82 | + lhs.value_ == rhs.value_; |
| 83 | +} |
| 84 | + |
| 85 | +inline auto |
| 86 | +operator!=(const metric_measurement& lhs, const metric_measurement& rhs) -> bool |
| 87 | +{ |
| 88 | + return !(lhs == rhs); |
| 89 | +} |
| 90 | +} // namespace couchbase::core |
0 commit comments