| 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 <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "absl/types/optional.h" |
| 20 | #include "absl/types/variant.h" |
| 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 absl::optional<T>. |
| 28 | class RTC_EXPORT Attribute { |
| 29 | public: |
| 30 | // All supported attribute types. |
| 31 | typedef absl::variant<const absl::optional<bool>*, |
| 32 | const absl::optional<int32_t>*, |
| 33 | const absl::optional<uint32_t>*, |
| 34 | const absl::optional<int64_t>*, |
| 35 | const absl::optional<uint64_t>*, |
| 36 | const absl::optional<double>*, |
| 37 | const absl::optional<std::string>*, |
| 38 | const absl::optional<std::vector<bool>>*, |
| 39 | const absl::optional<std::vector<int32_t>>*, |
| 40 | const absl::optional<std::vector<uint32_t>>*, |
| 41 | const absl::optional<std::vector<int64_t>>*, |
| 42 | const absl::optional<std::vector<uint64_t>>*, |
| 43 | const absl::optional<std::vector<double>>*, |
| 44 | const absl::optional<std::vector<std::string>>*, |
| 45 | const absl::optional<std::map<std::string, uint64_t>>*, |
| 46 | const absl::optional<std::map<std::string, double>>*> |
| 47 | StatVariant; |
| 48 |
| 49 | template <typename T> |
| 50 | Attribute(const char* name, const absl::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 absl::holds_alternative<const absl::optional<T>*>(attribute_); |
| 60 | } |
| 61 | template <typename T> |
| 62 | const absl::optional<T>& as_optional() const { |
| 63 | RTC_CHECK(holds_alternative<T>()); |
| 64 | return *absl::get<const absl::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 absl::get<const absl::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_ |
| 95 |