attribute.h (3080B)
1 /* 2 * Copyright 2024 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef API_STATS_ATTRIBUTE_H_ 12 #define API_STATS_ATTRIBUTE_H_ 13 14 #include <cstdint> 15 #include <map> 16 #include <optional> 17 #include <string> 18 #include <variant> 19 #include <vector> 20 21 #include "rtc_base/checks.h" 22 #include "rtc_base/system/rtc_export.h" 23 24 namespace webrtc { 25 26 // A light-weight wrapper of an RTCStats attribute, i.e. an individual metric of 27 // type std::optional<T>. 28 class RTC_EXPORT Attribute { 29 public: 30 // All supported attribute types. 31 typedef std::variant<const std::optional<bool>*, 32 const std::optional<int32_t>*, 33 const std::optional<uint32_t>*, 34 const std::optional<int64_t>*, 35 const std::optional<uint64_t>*, 36 const std::optional<double>*, 37 const std::optional<std::string>*, 38 const std::optional<std::vector<bool>>*, 39 const std::optional<std::vector<int32_t>>*, 40 const std::optional<std::vector<uint32_t>>*, 41 const std::optional<std::vector<int64_t>>*, 42 const std::optional<std::vector<uint64_t>>*, 43 const std::optional<std::vector<double>>*, 44 const std::optional<std::vector<std::string>>*, 45 const std::optional<std::map<std::string, uint64_t>>*, 46 const std::optional<std::map<std::string, double>>*> 47 StatVariant; 48 49 template <typename T> 50 Attribute(const char* name, const std::optional<T>* attribute) 51 : name_(name), attribute_(attribute) {} 52 53 const char* name() const; 54 const StatVariant& as_variant() const; 55 56 bool has_value() const; 57 template <typename T> 58 bool holds_alternative() const { 59 return std::holds_alternative<const std::optional<T>*>(attribute_); 60 } 61 template <typename T> 62 const std::optional<T>& as_optional() const { 63 RTC_CHECK(holds_alternative<T>()); 64 return *std::get<const std::optional<T>*>(attribute_); 65 } 66 template <typename T> 67 const T& get() const { 68 RTC_CHECK(holds_alternative<T>()); 69 RTC_CHECK(has_value()); 70 return std::get<const std::optional<T>*>(attribute_)->value(); 71 } 72 73 bool is_sequence() const; 74 bool is_string() const; 75 std::string ToString() const; 76 77 bool operator==(const Attribute& other) const; 78 bool operator!=(const Attribute& other) const; 79 80 private: 81 const char* name_; 82 StatVariant attribute_; 83 }; 84 85 struct RTC_EXPORT AttributeInit { 86 AttributeInit(const char* name, const Attribute::StatVariant& variant); 87 88 const char* name; 89 Attribute::StatVariant variant; 90 }; 91 92 } // namespace webrtc 93 94 #endif // API_STATS_ATTRIBUTE_H_