1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <string> 18 #include <utility> 19 #include <variant> 20 21 #include "HistogramValue.h" 22 23 #pragma once 24 25 namespace android { 26 namespace os { 27 namespace statsd { 28 29 // Used to store aggregations in NumericValueMetricProducer for ValueMetric. 30 // The aggregations are either int64 or double. 31 class NumericValue { 32 public: 33 NumericValue() noexcept = default; 34 35 // Copy constructor 36 constexpr NumericValue(const NumericValue& other) = default; NumericValue(NumericValue & other)37 constexpr NumericValue(NumericValue& other) : NumericValue(std::as_const(other)) { 38 } 39 40 // Copy constructor for contained types 41 template <typename V> NumericValue(const V & value)42 constexpr NumericValue(const V& value) : mData(std::in_place_type<V>, value) { 43 } 44 45 // Move constructor 46 constexpr NumericValue(NumericValue&& other) noexcept = default; 47 48 // Move constructor for contained types 49 template <typename V> NumericValue(V && value)50 constexpr NumericValue(V&& value) noexcept 51 : mData(std::in_place_type<V>, std::forward<V>(value)) { 52 } 53 54 // Copy assignment 55 NumericValue& operator=(const NumericValue& rhs) = default; 56 57 // Move assignment 58 NumericValue& operator=(NumericValue&& rhs) noexcept = default; 59 60 ~NumericValue() = default; 61 62 std::string toString() const; 63 64 void reset(); 65 66 template <typename V> 67 bool is() const; 68 69 bool hasValue() const; 70 71 template <typename V> 72 V& getValue(); 73 74 template <typename V> 75 const V& getValue() const; 76 77 template <typename V> 78 V& getValueOrDefault(V& defaultValue); 79 80 template <typename V> 81 const V& getValueOrDefault(const V& defaultValue) const; 82 83 bool isZero() const; 84 85 size_t getSize() const; 86 87 NumericValue& operator+=(const NumericValue& rhs); 88 89 friend NumericValue operator+(NumericValue lhs, const NumericValue& rhs); 90 91 NumericValue& operator-=(const NumericValue& rhs); 92 93 friend NumericValue operator-(NumericValue lhs, const NumericValue& rhs); 94 95 friend bool operator==(const NumericValue& lhs, const NumericValue& rhs); 96 97 friend bool operator!=(const NumericValue& lhs, const NumericValue& rhs); 98 99 friend bool operator<(const NumericValue& lhs, const NumericValue& rhs); 100 101 friend bool operator>(const NumericValue& lhs, const NumericValue& rhs); 102 103 friend bool operator<=(const NumericValue& lhs, const NumericValue& rhs); 104 105 friend bool operator>=(const NumericValue& lhs, const NumericValue& rhs); 106 107 private: 108 // std::monostate represents "empty" or default value. 109 std::variant<std::monostate, int64_t, double, HistogramValue> mData; 110 }; 111 112 } // namespace statsd 113 } // namespace os 114 } // namespace android 115