From 348438154abf25bfda544c7e4460ef2258eb1eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Bostr=C3=B6m?= Date: Fri, 19 Jan 2024 15:52:43 +0100 Subject: [PATCH] [Stats] Delete ValueToString/ToJson, ToString does the job. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There can only be one! Bug: webrtc:15164 Change-Id: Ib7265bf2103f24a6dab07737b2caed7f39ba75c3 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/334643 Commit-Queue: Henrik Boström Reviewed-by: Evan Shrubsole Cr-Commit-Position: refs/heads/main@{#41591} --- api/stats/attribute.h | 4 - api/stats/rtc_stats_member.h | 8 -- stats/rtc_stats_member.cc | 239 +++++------------------------------ 3 files changed, 29 insertions(+), 222 deletions(-) diff --git a/api/stats/attribute.h b/api/stats/attribute.h index dd21a985df..09211f469c 100644 --- a/api/stats/attribute.h +++ b/api/stats/attribute.h @@ -74,11 +74,7 @@ class RTC_EXPORT Attribute { bool is_sequence() const; bool is_string() const; - // Converts the attribute to a string that is parseable as a JSON object. std::string ToString() const; - // TODO(https://crbug.com/15164): Use ToString() instead and delete these. - std::string ValueToString() const { return ToString(); } - std::string ValueToJson() const { return ToString(); } bool operator==(const Attribute& other) const; bool operator!=(const Attribute& other) const; diff --git a/api/stats/rtc_stats_member.h b/api/stats/rtc_stats_member.h index 1c9679f2f1..9039569ede 100644 --- a/api/stats/rtc_stats_member.h +++ b/api/stats/rtc_stats_member.h @@ -90,10 +90,6 @@ class RTCStatsMember : public RTCStatsMemberInterface { Type type() const override { return StaticType(); } bool is_sequence() const override; bool is_string() const override; - // TODO(https://crbug.com/webrtc/15164): Delete both in favor of - // Attribute::ToString(). - std::string ValueToString() const; - std::string ValueToJson() const; template inline T value_or(U default_value) const { @@ -164,10 +160,6 @@ typedef std::map MapStringDouble; RTC_EXPORT bool RTCStatsMember::is_sequence() const; \ template <> \ RTC_EXPORT bool RTCStatsMember::is_string() const; \ - template <> \ - RTC_EXPORT std::string RTCStatsMember::ValueToString() const; \ - template <> \ - RTC_EXPORT std::string RTCStatsMember::ValueToJson() const; \ extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) \ RTCStatsMember diff --git a/stats/rtc_stats_member.cc b/stats/rtc_stats_member.cc index a976a20046..3f91988ed0 100644 --- a/stats/rtc_stats_member.cc +++ b/stats/rtc_stats_member.cc @@ -10,234 +10,53 @@ #include "api/stats/rtc_stats_member.h" -#include "rtc_base/arraysize.h" -#include "rtc_base/strings/string_builder.h" - namespace webrtc { -namespace { - -// TODO(https://crbug.com/webrtc/15164): Delete all stringified functions in -// favor of Attribute::ToString(). - -// Produces "[a,b,c]". Works for non-vector `RTCStatsMemberInterface::Type` -// types. -template -std::string VectorToString(const std::vector& vector) { - rtc::StringBuilder sb; - sb << "["; - const char* separator = ""; - for (const T& element : vector) { - sb << separator << rtc::ToString(element); - separator = ","; - } - sb << "]"; - return sb.Release(); -} - -// This overload is required because std::vector range loops don't -// return references but objects, causing -Wrange-loop-analysis diagnostics. -std::string VectorToString(const std::vector& vector) { - rtc::StringBuilder sb; - sb << "["; - const char* separator = ""; - for (bool element : vector) { - sb << separator << rtc::ToString(element); - separator = ","; - } - sb << "]"; - return sb.Release(); -} - -// Produces "[\"a\",\"b\",\"c\"]". Works for vectors of both const char* and -// std::string element types. -template -std::string VectorOfStringsToString(const std::vector& strings) { - rtc::StringBuilder sb; - sb << "["; - const char* separator = ""; - for (const T& element : strings) { - sb << separator << "\"" << rtc::ToString(element) << "\""; - separator = ","; - } - sb << "]"; - return sb.Release(); -} - -template -std::string MapToString(const std::map& map) { - rtc::StringBuilder sb; - sb << "{"; - const char* separator = ""; - for (const auto& element : map) { - sb << separator << rtc::ToString(element.first) << ":" - << rtc::ToString(element.second); - separator = ","; - } - sb << "}"; - return sb.Release(); -} - -template -std::string ToStringAsDouble(const T value) { - // JSON represents numbers as floating point numbers with about 15 decimal - // digits of precision. - char buf[32]; - const int len = std::snprintf(&buf[0], arraysize(buf), "%.16g", - static_cast(value)); - RTC_DCHECK_LE(len, arraysize(buf)); - return std::string(&buf[0], len); -} - -template -std::string VectorToStringAsDouble(const std::vector& vector) { - rtc::StringBuilder sb; - sb << "["; - const char* separator = ""; - for (const T& element : vector) { - sb << separator << ToStringAsDouble(element); - separator = ","; - } - sb << "]"; - return sb.Release(); -} - -template -std::string MapToStringAsDouble(const std::map& map) { - rtc::StringBuilder sb; - sb << "{"; - const char* separator = ""; - for (const auto& element : map) { - sb << separator << "\"" << rtc::ToString(element.first) - << "\":" << ToStringAsDouble(element.second); - separator = ","; - } - sb << "}"; - return sb.Release(); -} - -} // namespace - -#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_json) \ - template <> \ - RTCStatsMemberInterface::Type RTCStatsMember::StaticType() { \ - return type; \ - } \ - template <> \ - bool RTCStatsMember::is_sequence() const { \ - return is_seq; \ - } \ - template <> \ - bool RTCStatsMember::is_string() const { \ - return is_str; \ - } \ - template <> \ - std::string RTCStatsMember::ValueToString() const { \ - RTC_DCHECK(value_.has_value()); \ - return to_str; \ - } \ - template <> \ - std::string RTCStatsMember::ValueToJson() const { \ - RTC_DCHECK(value_.has_value()); \ - return to_json; \ - } \ +#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str) \ + template <> \ + RTCStatsMemberInterface::Type RTCStatsMember::StaticType() { \ + return type; \ + } \ + template <> \ + bool RTCStatsMember::is_sequence() const { \ + return is_seq; \ + } \ + template <> \ + bool RTCStatsMember::is_string() const { \ + return is_str; \ + } \ template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) RTCStatsMember -WEBRTC_DEFINE_RTCSTATSMEMBER(bool, - kBool, - false, - false, - rtc::ToString(*value_), - rtc::ToString(*value_)); -WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, - kInt32, - false, - false, - rtc::ToString(*value_), - rtc::ToString(*value_)); -WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, - kUint32, - false, - false, - rtc::ToString(*value_), - rtc::ToString(*value_)); -WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, - kInt64, - false, - false, - rtc::ToString(*value_), - ToStringAsDouble(*value_)); -WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, - kUint64, - false, - false, - rtc::ToString(*value_), - ToStringAsDouble(*value_)); -WEBRTC_DEFINE_RTCSTATSMEMBER(double, - kDouble, - false, - false, - rtc::ToString(*value_), - ToStringAsDouble(*value_)); -WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, - kString, - false, - true, - *value_, - *value_); -WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, - kSequenceBool, - true, - false, - VectorToString(*value_), - VectorToString(*value_)); -WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, - kSequenceInt32, - true, - false, - VectorToString(*value_), - VectorToString(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(bool, kBool, false, false); +WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, kInt32, false, false); +WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, kUint32, false, false); +WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, kInt64, false, false); +WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, kUint64, false, false); +WEBRTC_DEFINE_RTCSTATSMEMBER(double, kDouble, false, false); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, kSequenceBool, true, false); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, kSequenceInt32, true, false); WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, kSequenceUint32, true, - false, - VectorToString(*value_), - VectorToString(*value_)); -WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, - kSequenceInt64, - true, - false, - VectorToString(*value_), - VectorToStringAsDouble(*value_)); + false); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, kSequenceInt64, true, false); WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, kSequenceUint64, true, - false, - VectorToString(*value_), - VectorToStringAsDouble(*value_)); -WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, - kSequenceDouble, - true, - false, - VectorToString(*value_), - VectorToStringAsDouble(*value_)); + false); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, kSequenceDouble, true, false); WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, kSequenceString, true, - false, - VectorOfStringsToString(*value_), - VectorOfStringsToString(*value_)); + false); WEBRTC_DEFINE_RTCSTATSMEMBER(rtc_stats_internal::MapStringUint64, kMapStringUint64, false, - false, - MapToString(*value_), - MapToStringAsDouble(*value_)); + false); WEBRTC_DEFINE_RTCSTATSMEMBER(rtc_stats_internal::MapStringDouble, kMapStringDouble, false, - false, - MapToString(*value_), - MapToStringAsDouble(*value_)); + false); } // namespace webrtc