Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 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 #include "webrtc/api/rtcstats.h" | |
| 12 | |
| 13 #include "webrtc/base/stringencode.h" | |
| 14 | |
| 15 namespace webrtc { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Produces "{ a, b, c }". Works for non-vector |RTCStatsMemberInterface::Type| | |
| 20 // types. | |
| 21 template<typename T> | |
| 22 std::string VectorToString(const std::vector<T>& vector) { | |
| 23 if (vector.empty()) | |
| 24 return "{}"; | |
| 25 std::ostringstream oss; | |
| 26 oss << "{ "; | |
| 27 bool is_first = true; | |
| 28 for (const T& element : vector) { | |
| 29 if (!is_first) | |
|
nisse-webrtc
2016/08/23 08:27:28
I think the is_first logic is unnecessary state.
hbos
2016/08/23 16:21:18
Done.
| |
| 30 oss << ", "; | |
| 31 oss << rtc::ToString<T>(element); | |
| 32 is_first = false; | |
| 33 } | |
| 34 oss << " }"; | |
| 35 return oss.str(); | |
| 36 } | |
| 37 | |
| 38 // Produces "{ \"a\", \"b\", \"c\" }". Works for vectors of both const char* and | |
| 39 // std::string element types. | |
| 40 template<typename T> | |
| 41 std::string VectorOfStringsToString(const std::vector<T>& strings) { | |
| 42 if (strings.empty()) | |
| 43 return "{}"; | |
| 44 std::ostringstream oss; | |
| 45 oss << "{ "; | |
| 46 bool is_first = true; | |
| 47 for (const T& string : strings) { | |
| 48 if (!is_first) | |
| 49 oss << ", "; | |
| 50 oss << '"' << string << '"'; | |
| 51 is_first = false; | |
| 52 } | |
| 53 oss << " }"; | |
| 54 return oss.str(); | |
| 55 } | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 std::string RTCStats::ToString() const { | |
| 60 std::ostringstream oss; | |
| 61 oss << type_name() << " {\n id: \"" << id_ << "\"\n timestamp: " | |
| 62 << timestamp_ << '\n'; | |
| 63 for (const RTCStatsMemberInterface* member : Members()) { | |
| 64 oss << " " << member->name() << ": "; | |
| 65 if (member->is_defined()) { | |
| 66 if (member->is_string() && !member->is_sequence()) | |
| 67 oss << '"' << member->ValueToString() << "\"\n"; | |
| 68 else | |
| 69 oss << member->ValueToString() << '\n'; | |
| 70 } else { | |
| 71 oss << "undefined\n"; | |
| 72 } | |
| 73 } | |
| 74 oss << '}'; | |
| 75 return oss.str(); | |
| 76 } | |
| 77 | |
| 78 std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const { | |
| 79 return MembersOfThisObjectAndAncestors(0); | |
| 80 } | |
| 81 | |
| 82 std::vector<const RTCStatsMemberInterface*> | |
| 83 RTCStats::MembersOfThisObjectAndAncestors( | |
| 84 size_t additional_capacity) const { | |
| 85 std::vector<const RTCStatsMemberInterface*> members; | |
| 86 members.reserve(additional_capacity); | |
| 87 return members; | |
| 88 } | |
| 89 | |
| 90 // int32_t (kInt32) | |
|
nisse-webrtc
2016/08/23 08:27:28
I'd consider using a local macro to reduce code du
hbos
2016/08/23 16:21:18
Done.
| |
| 91 template<> | |
| 92 const RTCStatsMemberInterface::Type RTCStatsMember<int32_t>::kType = | |
| 93 RTCStatsMemberInterface::kInt32; | |
| 94 | |
| 95 template<> | |
| 96 bool RTCStatsMember<int32_t>::is_sequence() const { return false; } | |
| 97 | |
| 98 template<> | |
| 99 bool RTCStatsMember<int32_t>::is_string() const { return false; } | |
| 100 | |
| 101 template<> | |
| 102 std::string RTCStatsMember<int32_t>::ValueToString() const { | |
| 103 RTC_DCHECK(is_defined_); | |
| 104 return rtc::ToString<int32_t>(value_); | |
| 105 } | |
| 106 | |
| 107 // uint32_t (kUint32) | |
| 108 template<> | |
| 109 const RTCStatsMemberInterface::Type RTCStatsMember<uint32_t>::kType = | |
| 110 RTCStatsMemberInterface::kUint32; | |
| 111 | |
| 112 template<> | |
| 113 bool RTCStatsMember<uint32_t>::is_sequence() const { return false; } | |
| 114 | |
| 115 template<> | |
| 116 bool RTCStatsMember<uint32_t>::is_string() const { return false; } | |
| 117 | |
| 118 template<> | |
| 119 std::string RTCStatsMember<uint32_t>::ValueToString() const { | |
| 120 RTC_DCHECK(is_defined_); | |
| 121 return rtc::ToString<uint32_t>(value_); | |
| 122 } | |
| 123 | |
| 124 // int64_t (kInt64) | |
| 125 template<> | |
| 126 const RTCStatsMemberInterface::Type RTCStatsMember<int64_t>::kType = | |
| 127 RTCStatsMemberInterface::kInt64; | |
| 128 | |
| 129 template<> | |
| 130 bool RTCStatsMember<int64_t>::is_sequence() const { return false; } | |
| 131 | |
| 132 template<> | |
| 133 bool RTCStatsMember<int64_t>::is_string() const { return false; } | |
| 134 | |
| 135 template<> | |
| 136 std::string RTCStatsMember<int64_t>::ValueToString() const { | |
| 137 RTC_DCHECK(is_defined_); | |
| 138 return rtc::ToString<int64_t>(value_); | |
| 139 } | |
| 140 | |
| 141 // uint64_t (kUint64) | |
| 142 template<> | |
| 143 const RTCStatsMemberInterface::Type RTCStatsMember<uint64_t>::kType = | |
| 144 RTCStatsMemberInterface::kUint64; | |
| 145 | |
| 146 template<> | |
| 147 bool RTCStatsMember<uint64_t>::is_sequence() const { return false; } | |
| 148 | |
| 149 template<> | |
| 150 bool RTCStatsMember<uint64_t>::is_string() const { return false; } | |
| 151 | |
| 152 template<> | |
| 153 std::string RTCStatsMember<uint64_t>::ValueToString() const { | |
| 154 RTC_DCHECK(is_defined_); | |
| 155 return rtc::ToString<uint64_t>(value_); | |
| 156 } | |
| 157 | |
| 158 // double (kDouble) | |
| 159 template<> | |
| 160 const RTCStatsMemberInterface::Type RTCStatsMember<double>::kType = | |
| 161 RTCStatsMemberInterface::kDouble; | |
| 162 | |
| 163 template<> | |
| 164 bool RTCStatsMember<double>::is_sequence() const { return false; } | |
| 165 | |
| 166 template<> | |
| 167 bool RTCStatsMember<double>::is_string() const { return false; } | |
| 168 | |
| 169 template<> | |
| 170 std::string RTCStatsMember<double>::ValueToString() const { | |
| 171 RTC_DCHECK(is_defined_); | |
| 172 return rtc::ToString<double>(value_); | |
| 173 } | |
| 174 | |
| 175 // const char* (kStaticString) | |
| 176 template<> | |
| 177 const RTCStatsMemberInterface::Type RTCStatsMember<const char*>::kType = | |
| 178 RTCStatsMemberInterface::kStaticString; | |
| 179 | |
| 180 template<> | |
| 181 bool RTCStatsMember<const char*>::is_sequence() const { return false; } | |
| 182 | |
| 183 template<> | |
| 184 bool RTCStatsMember<const char*>::is_string() const { return true; } | |
| 185 | |
| 186 template<> | |
| 187 std::string RTCStatsMember<const char*>::ValueToString() const { | |
| 188 RTC_DCHECK(is_defined_); | |
| 189 return value_; | |
| 190 } | |
| 191 | |
| 192 // std::string (kString) | |
| 193 template<> | |
| 194 const RTCStatsMemberInterface::Type RTCStatsMember<std::string>::kType = | |
| 195 RTCStatsMemberInterface::kString; | |
| 196 | |
| 197 template<> | |
| 198 bool RTCStatsMember<std::string>::is_sequence() const { return false; } | |
| 199 | |
| 200 template<> | |
| 201 bool RTCStatsMember<std::string>::is_string() const { return true; } | |
| 202 | |
| 203 template<> | |
| 204 std::string RTCStatsMember<std::string>::ValueToString() const { | |
| 205 RTC_DCHECK(is_defined_); | |
| 206 return value_; | |
| 207 } | |
| 208 | |
| 209 // std::vector<int32_t> (kSequenceInt32) | |
| 210 template<> | |
| 211 const RTCStatsMemberInterface::Type | |
| 212 RTCStatsMember<std::vector<int32_t>>::kType = | |
| 213 RTCStatsMemberInterface::kSequenceInt32; | |
| 214 | |
| 215 template<> | |
| 216 bool RTCStatsMember<std::vector<int32_t>>::is_sequence() const { return true; } | |
| 217 | |
| 218 template<> | |
| 219 bool RTCStatsMember<std::vector<int32_t>>::is_string() const { return false; } | |
| 220 | |
| 221 template<> | |
| 222 std::string RTCStatsMember<std::vector<int32_t>>::ValueToString() const { | |
| 223 RTC_DCHECK(is_defined_); | |
| 224 return VectorToString<int32_t>(value_); | |
| 225 } | |
| 226 | |
| 227 // std::vector<uint32_t> (kSequenceUint32) | |
| 228 template<> | |
| 229 const RTCStatsMemberInterface::Type | |
| 230 RTCStatsMember<std::vector<uint32_t>>::kType = | |
| 231 RTCStatsMemberInterface::kSequenceUint32; | |
| 232 | |
| 233 template<> | |
| 234 bool RTCStatsMember<std::vector<uint32_t>>::is_sequence() const { return true; } | |
| 235 | |
| 236 template<> | |
| 237 bool RTCStatsMember<std::vector<uint32_t>>::is_string() const { return false; } | |
| 238 | |
| 239 template<> | |
| 240 std::string RTCStatsMember<std::vector<uint32_t>>::ValueToString() const { | |
| 241 RTC_DCHECK(is_defined_); | |
| 242 return VectorToString<uint32_t>(value_); | |
| 243 } | |
| 244 | |
| 245 // std::vector<int64_t> (kSequenceInt64) | |
| 246 template<> | |
| 247 const RTCStatsMemberInterface::Type | |
| 248 RTCStatsMember<std::vector<int64_t>>::kType = | |
| 249 RTCStatsMemberInterface::kSequenceInt64; | |
| 250 | |
| 251 template<> | |
| 252 bool RTCStatsMember<std::vector<int64_t>>::is_sequence() const { return true; } | |
| 253 | |
| 254 template<> | |
| 255 bool RTCStatsMember<std::vector<int64_t>>::is_string() const { return false; } | |
| 256 | |
| 257 template<> | |
| 258 std::string RTCStatsMember<std::vector<int64_t>>::ValueToString() const { | |
| 259 RTC_DCHECK(is_defined_); | |
| 260 return VectorToString<int64_t>(value_); | |
| 261 } | |
| 262 | |
| 263 // std::vector<uint64_t> (kSequenceUint64) | |
| 264 template<> | |
| 265 const RTCStatsMemberInterface::Type | |
| 266 RTCStatsMember<std::vector<uint64_t>>::kType = | |
| 267 RTCStatsMemberInterface::kSequenceUint64; | |
| 268 | |
| 269 template<> | |
| 270 bool RTCStatsMember<std::vector<uint64_t>>::is_sequence() const { return true; } | |
| 271 | |
| 272 template<> | |
| 273 bool RTCStatsMember<std::vector<uint64_t>>::is_string() const { return false; } | |
| 274 | |
| 275 template<> | |
| 276 std::string RTCStatsMember<std::vector<uint64_t>>::ValueToString() const { | |
| 277 RTC_DCHECK(is_defined_); | |
| 278 return VectorToString<uint64_t>(value_); | |
| 279 } | |
| 280 | |
| 281 // std::vector<double> (kSequenceDouble) | |
| 282 template<> | |
| 283 const RTCStatsMemberInterface::Type | |
| 284 RTCStatsMember<std::vector<double>>::kType = | |
| 285 RTCStatsMemberInterface::kSequenceDouble; | |
| 286 | |
| 287 template<> | |
| 288 bool RTCStatsMember<std::vector<double>>::is_sequence() const { return true; } | |
| 289 | |
| 290 template<> | |
| 291 bool RTCStatsMember<std::vector<double>>::is_string() const { return false; } | |
| 292 | |
| 293 template<> | |
| 294 std::string RTCStatsMember<std::vector<double>>::ValueToString() const { | |
| 295 RTC_DCHECK(is_defined_); | |
| 296 return VectorToString<double>(value_); | |
| 297 } | |
| 298 | |
| 299 // std::vector<const char*> (kSequenceStaticString) | |
| 300 template<> | |
| 301 const RTCStatsMemberInterface::Type | |
| 302 RTCStatsMember<std::vector<const char*>>::kType = | |
| 303 RTCStatsMemberInterface::kSequenceStaticString; | |
| 304 | |
| 305 template<> | |
| 306 bool RTCStatsMember<std::vector<const char*>>::is_sequence() const { | |
| 307 return true; | |
| 308 } | |
| 309 | |
| 310 template<> | |
| 311 bool RTCStatsMember<std::vector<const char*>>::is_string() const { | |
| 312 return true; | |
| 313 } | |
| 314 | |
| 315 template<> | |
| 316 std::string RTCStatsMember<std::vector<const char*>>::ValueToString() const { | |
| 317 RTC_DCHECK(is_defined_); | |
| 318 return VectorOfStringsToString<const char*>(value_); | |
| 319 } | |
| 320 | |
| 321 // std::vector<std::string> (kSequenceString) | |
| 322 template<> | |
| 323 const RTCStatsMemberInterface::Type | |
| 324 RTCStatsMember<std::vector<std::string>>::kType = | |
| 325 RTCStatsMemberInterface::kSequenceString; | |
| 326 | |
| 327 template<> | |
| 328 bool RTCStatsMember<std::vector<std::string>>::is_sequence() const { | |
| 329 return true; | |
| 330 } | |
| 331 | |
| 332 template<> | |
| 333 bool RTCStatsMember<std::vector<std::string>>::is_string() const { | |
| 334 return true; | |
| 335 } | |
| 336 | |
| 337 template<> | |
| 338 std::string RTCStatsMember<std::vector<std::string>>::ValueToString() const { | |
| 339 RTC_DCHECK(is_defined_); | |
| 340 return VectorOfStringsToString<std::string>(value_); | |
| 341 } | |
| 342 | |
| 343 } // namespace webrtc | |
| OLD | NEW |