Add ToString() methods to classes with << operators, preparing for deprecations.

Bug: webrtc:8982
Change-Id: I9b8792a229539dd9848f4d9936fe343f4bf9ad49
Reviewed-on: https://webrtc-review.googlesource.com/63200
Commit-Queue: Jonas Olsson <jonasolsson@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22705}
This commit is contained in:
Jonas Olsson
2018-04-03 12:22:07 +02:00
committed by Commit Bot
parent cf06a53152
commit 74395345e8
13 changed files with 147 additions and 27 deletions

View File

@ -432,7 +432,7 @@ def CheckNoStreamUsageIsAdded(input_api, output_api,
include_re = input_api.re.compile(r'#include <(i|o|s)stream>')
usage_re = input_api.re.compile(r'std::(w|i|o|io|wi|wo|wio)(string)*stream')
no_presubmit_re = input_api.re.compile(
r' // no-presubmit-check TODO\(webrtc:8982\)')
r'// no-presubmit-check TODO\(webrtc:8982\)')
for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
if f.LocalPath() == 'PRESUBMIT.py':
continue

View File

@ -94,8 +94,13 @@ void RTCError::set_message(std::string&& message) {
}
std::ostream& operator<<(std::ostream& stream, RTCErrorType error) {
return stream << ToString(error);
}
// TODO(jonasolsson): Change to use absl::string_view when it's available.
std::string ToString(RTCErrorType error) {
int index = static_cast<int>(error);
return stream << kRTCErrorTypeNames[index];
return std::string(kRTCErrorTypeNames[index]);
}
} // namespace webrtc

View File

@ -145,6 +145,8 @@ class RTCError {
// Only intended to be used for logging/disagnostics.
std::ostream& operator<<(std::ostream& stream, RTCErrorType error);
std::string ToString(RTCErrorType error);
// Helper macro that can be used by implementations to create an error with a
// message and log it. |message| should be a string literal or movable
// std::string.

View File

@ -50,5 +50,4 @@ DataSize operator*(const TimeDelta& duration, const DataRate& rate) {
return rate * duration;
}
} // namespace webrtc

View File

@ -14,6 +14,8 @@
#ifndef MODULES_VIDEO_CODING_CODECS_H264_INCLUDE_H264_GLOBALS_H_
#define MODULES_VIDEO_CODING_CODECS_H264_INCLUDE_H264_GLOBALS_H_
#include <string>
namespace webrtc {
// The packetization types that we support: single, aggregated, and fragmented.
@ -40,17 +42,20 @@ enum class H264PacketizationMode {
// This function is declared inline because it is not clear which
// .cc file it should belong to.
// TODO(hta): Refactor. https://bugs.webrtc.org/6842
// TODO(jonasolsson): Use absl::string_view instead when that's available.
inline std::string ToString(H264PacketizationMode mode) {
if (mode == H264PacketizationMode::NonInterleaved) {
return "NonInterleaved";
} else if (mode == H264PacketizationMode::SingleNalUnit) {
return "SingleNalUnit";
}
RTC_NOTREACHED();
return "";
}
inline std::ostream& operator<<(std::ostream& stream,
H264PacketizationMode mode) {
switch (mode) {
case H264PacketizationMode::NonInterleaved:
stream << "NonInterleaved";
break;
case H264PacketizationMode::SingleNalUnit:
stream << "SingleNalUnit";
break;
}
return stream;
return stream << ToString(mode);
}
struct NaluInfo {

View File

@ -334,6 +334,17 @@ rtc_source_set("stringutils") {
]
}
rtc_source_set("audio_format_to_string") {
sources = [
"strings/audio_format_to_string.cc",
"strings/audio_format_to_string.h",
]
deps = [
":stringutils",
"../api/audio_codecs:audio_codecs_api",
]
}
rtc_source_set("type_traits") {
sources = [
"type_traits.h",

View File

@ -217,12 +217,16 @@ const InterfaceAddress& InterfaceAddress::operator=(
}
std::ostream& operator<<(std::ostream& os, const InterfaceAddress& ip) {
os << static_cast<const IPAddress&>(ip);
return os << ip.ToString();
}
if (ip.family() == AF_INET6)
os << "|flags:0x" << std::hex << ip.ipv6_flags();
std::string InterfaceAddress::ToString() const {
std::string result = IPAddress::ToString();
return os;
if (family() == AF_INET6)
result += "|flags:0x" + rtc::ToHex(ipv6_flags());
return result;
}
static bool IPIsPrivateNetworkV4(const IPAddress& ip) {

View File

@ -144,6 +144,8 @@ class InterfaceAddress : public IPAddress {
friend std::ostream& operator<<(std::ostream& os,
const InterfaceAddress& addr);
std::string ToString() const;
private:
int ipv6_flags_;
};

View File

@ -0,0 +1,52 @@
/*
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/strings/audio_format_to_string.h"
#include "rtc_base/strings/string_builder.h"
namespace rtc {
std::string ToString(const webrtc::SdpAudioFormat& saf) {
char sb_buf[1024];
rtc::SimpleStringBuilder sb(sb_buf);
sb << "{name: " << saf.name;
sb << ", clockrate_hz: " << saf.clockrate_hz;
sb << ", num_channels: " << saf.num_channels;
sb << ", parameters: {";
const char* sep = "";
for (const auto& kv : saf.parameters) {
sb << sep << kv.first << ": " << kv.second;
sep = ", ";
}
sb << "}}";
return sb.str();
}
std::string ToString(const webrtc::AudioCodecInfo& aci) {
char sb_buf[1024];
rtc::SimpleStringBuilder sb(sb_buf);
sb << "{sample_rate_hz: " << aci.sample_rate_hz;
sb << ", num_channels: " << aci.num_channels;
sb << ", default_bitrate_bps: " << aci.default_bitrate_bps;
sb << ", min_bitrate_bps: " << aci.min_bitrate_bps;
sb << ", max_bitrate_bps: " << aci.max_bitrate_bps;
sb << ", allow_comfort_noise: " << aci.allow_comfort_noise;
sb << ", supports_network_adaption: " << aci.supports_network_adaption;
sb << "}";
return sb.str();
}
std::string ToString(const webrtc::AudioCodecSpec& acs) {
char sb_buf[1024];
rtc::SimpleStringBuilder sb(sb_buf);
sb << "{format: " << ToString(acs.format);
sb << ", info: " << ToString(acs.info);
sb << "}";
return sb.str();
}
} // namespace rtc

View File

@ -0,0 +1,24 @@
/*
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_STRINGS_AUDIO_FORMAT_TO_STRING_H_
#define RTC_BASE_STRINGS_AUDIO_FORMAT_TO_STRING_H_
#include <string>
#include "api/audio_codecs/audio_format.h"
namespace rtc {
std::string ToString(const webrtc::SdpAudioFormat& saf);
std::string ToString(const webrtc::AudioCodecInfo& saf);
std::string ToString(const webrtc::AudioCodecSpec& acs);
} // namespace rtc
#endif // RTC_BASE_STRINGS_AUDIO_FORMAT_TO_STRING_H_

View File

@ -7,6 +7,8 @@
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <algorithm>
#include <cstdio>
#include "rtc_base/stringutils.h"
#include "rtc_base/checks.h"
@ -130,4 +132,11 @@ std::string string_trim(const std::string& s) {
return s.substr(first, last - first + 1);
}
std::string ToHex(const int i) {
char buffer[50];
snprintf(buffer, sizeof(buffer), "%x", i);
return std::string(buffer);
}
} // namespace rtc

View File

@ -91,9 +91,9 @@ const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
template<class CTYPE>
struct Traits {
// STL string type
//typedef XXX string;
// typedef XXX string;
// Null-terminated string
//inline static const CTYPE* empty_str();
// inline static const CTYPE* empty_str();
};
///////////////////////////////////////////////////////////////////////////////
@ -119,7 +119,7 @@ const CTYPE* strchr(const CTYPE* str, const CTYPE* chs) {
template<class CTYPE>
const CTYPE* strchrn(const CTYPE* str, size_t slen, CTYPE ch) {
for (size_t i=0; i<slen && str[i]; ++i) {
for (size_t i=0; i < slen && str[i]; ++i) {
if (str[i] == ch) {
return str + i;
}
@ -312,6 +312,7 @@ bool ends_with(const char *s1, const char *s2);
// Remove leading and trailing whitespaces.
std::string string_trim(const std::string& s);
std::string ToHex(const int i);
} // namespace rtc
#endif // RTC_BASE_STRINGUTILS_H_

View File

@ -16,10 +16,10 @@ namespace rtc {
// Tests for string_match().
TEST(string_matchTest, Matches) {
EXPECT_TRUE( string_match("A.B.C.D", "a.b.c.d"));
EXPECT_TRUE( string_match("www.TEST.GOOGLE.COM", "www.*.com"));
EXPECT_TRUE( string_match("127.0.0.1", "12*.0.*1"));
EXPECT_TRUE( string_match("127.1.0.21", "12*.0.*1"));
EXPECT_TRUE(string_match("A.B.C.D", "a.b.c.d"));
EXPECT_TRUE(string_match("www.TEST.GOOGLE.COM", "www.*.com"));
EXPECT_TRUE(string_match("127.0.0.1", "12*.0.*1"));
EXPECT_TRUE(string_match("127.1.0.21", "12*.0.*1"));
EXPECT_FALSE(string_match("127.0.0.0", "12*.0.*1"));
EXPECT_FALSE(string_match("127.0.0.0", "12*.0.*1"));
EXPECT_FALSE(string_match("127.1.1.21", "12*.0.*1"));
@ -105,4 +105,10 @@ TEST(string_endsTest, EndsWith) {
EXPECT_FALSE(ends_with("", "f"));
}
TEST(string_toHexTest, ToHex) {
EXPECT_EQ(ToHex(0), "0");
EXPECT_EQ(ToHex(0X1243E), "1243e");
EXPECT_EQ(ToHex(-20), "ffffffec");
}
} // namespace rtc