Remove all remaining non-test uses of std::stringstream.

Bug: webrtc:8982
Change-Id: I635a8545c46dc8c89663d64af351e22e65cbcb33
Reviewed-on: https://webrtc-review.googlesource.com/98880
Commit-Queue: Jonas Olsson <jonasolsson@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24715}
This commit is contained in:
Jonas Olsson 2018-09-13 10:07:07 +02:00 committed by Commit Bot
parent 096193395b
commit 941a07cca3
25 changed files with 74 additions and 126 deletions

View File

@ -36,32 +36,8 @@ static_assert(static_cast<int>(webrtc::RTCErrorType::INTERNAL_ERROR) ==
namespace webrtc { namespace webrtc {
RTCError::RTCError(RTCError&& other) RTCError::RTCError(RTCError&& other) = default;
: type_(other.type_), have_string_message_(other.have_string_message_) { RTCError& RTCError::operator=(RTCError&& other) = default;
if (have_string_message_) {
new (&string_message_) std::string(std::move(other.string_message_));
} else {
static_message_ = other.static_message_;
}
}
RTCError& RTCError::operator=(RTCError&& other) {
type_ = other.type_;
if (other.have_string_message_) {
set_message(std::move(other.string_message_));
} else {
set_message(other.static_message_);
}
return *this;
}
RTCError::~RTCError() {
// If we hold a message string that was built, rather than a static string,
// we need to delete it.
if (have_string_message_) {
string_message_.~basic_string();
}
}
// static // static
RTCError RTCError::OK() { RTCError RTCError::OK() {
@ -69,28 +45,11 @@ RTCError RTCError::OK() {
} }
const char* RTCError::message() const { const char* RTCError::message() const {
if (have_string_message_) { return message_.c_str();
return string_message_.c_str();
} else {
return static_message_;
}
} }
void RTCError::set_message(const char* message) { void RTCError::set_message(std::string message) {
if (have_string_message_) { message_ = std::move(message);
string_message_.~basic_string();
have_string_message_ = false;
}
static_message_ = message;
}
void RTCError::set_message(std::string&& message) {
if (!have_string_message_) {
new (&string_message_) std::string(std::move(message));
have_string_message_ = true;
} else {
string_message_ = message;
}
} }
// TODO(jonasolsson): Change to use absl::string_view when it's available. // TODO(jonasolsson): Change to use absl::string_view when it's available.

View File

@ -86,12 +86,9 @@ class RTCError {
// Creates a "no error" error. // Creates a "no error" error.
RTCError() {} RTCError() {}
explicit RTCError(RTCErrorType type) : type_(type) {} explicit RTCError(RTCErrorType type) : type_(type) {}
// For performance, prefer using the constructor that takes a const char* if
// the message is a static string. RTCError(RTCErrorType type, std::string message)
RTCError(RTCErrorType type, const char* message) : type_(type), message_(std::move(message)) {}
: type_(type), static_message_(message), have_string_message_(false) {}
RTCError(RTCErrorType type, std::string&& message)
: type_(type), string_message_(message), have_string_message_(true) {}
// Delete the copy constructor and assignment operator; there aren't any use // Delete the copy constructor and assignment operator; there aren't any use
// cases where you should need to copy an RTCError, as opposed to moving it. // cases where you should need to copy an RTCError, as opposed to moving it.
@ -103,8 +100,6 @@ class RTCError {
RTCError(RTCError&& other); RTCError(RTCError&& other);
RTCError& operator=(RTCError&& other); RTCError& operator=(RTCError&& other);
~RTCError();
// Identical to default constructed error. // Identical to default constructed error.
// //
// Preferred over the default constructor for code readability. // Preferred over the default constructor for code readability.
@ -118,10 +113,8 @@ class RTCError {
// anything but logging/diagnostics, since messages are not guaranteed to be // anything but logging/diagnostics, since messages are not guaranteed to be
// stable. // stable.
const char* message() const; const char* message() const;
// For performance, prefer using the method that takes a const char* if the
// message is a static string. void set_message(std::string message);
void set_message(const char* message);
void set_message(std::string&& message);
// Convenience method for situations where you only care whether or not an // Convenience method for situations where you only care whether or not an
// error occurred. // error occurred.
@ -129,16 +122,7 @@ class RTCError {
private: private:
RTCErrorType type_ = RTCErrorType::NONE; RTCErrorType type_ = RTCErrorType::NONE;
// For performance, we use static strings wherever possible. But in some std::string message_;
// cases the error string may need to be constructed, in which case an
// std::string is used.
union {
const char* static_message_ = "";
std::string string_message_;
};
// Whether or not |static_message_| or |string_message_| is being used in the
// above union.
bool have_string_message_ = false;
}; };
// Outputs the error as a friendly string. Update this method when adding a new // Outputs the error as a friendly string. Update this method when adding a new

View File

@ -222,7 +222,8 @@ TEST(RTCErrorOrTest, MoveValue) {
#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST(RTCErrorOrDeathTest, ConstructWithOkError) { TEST(RTCErrorOrDeathTest, ConstructWithOkError) {
EXPECT_DEATH(RTCErrorOr<int> err = RTCError::OK(), ""); RTCErrorOr<int> err;
EXPECT_DEATH(err = RTCError::OK(), "");
} }
TEST(RTCErrorOrDeathTest, DereferenceErrorValue) { TEST(RTCErrorOrDeathTest, DereferenceErrorValue) {

View File

@ -17,6 +17,7 @@
#include <stdint.h> #include <stdint.h>
#include <cmath> #include <cmath>
#include <cstdlib>
#include <limits> #include <limits>
#include <string> #include <string>

View File

@ -14,6 +14,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <sstream> // no-presubmit-check TODO(webrtc:8982)
#include "p2p/base/basicpacketsocketfactory.h" #include "p2p/base/basicpacketsocketfactory.h"
#include "p2p/stunprober/stunprober.h" #include "p2p/stunprober/stunprober.h"

View File

@ -11,6 +11,7 @@
#define LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_PARSER_H_ #define LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_PARSER_H_
#include <map> #include <map>
#include <sstream> // no-presubmit-check TODO(webrtc:8982)
#include <string> #include <string>
#include <utility> // pair #include <utility> // pair
#include <vector> #include <vector>

View File

@ -13,6 +13,7 @@
#include <iterator> #include <iterator>
#include <map> #include <map>
#include <set> #include <set>
#include <sstream> // no-presubmit-check TODO(webrtc:8982)
#include <string> #include <string>
#include <utility> // pair #include <utility> // pair
#include <vector> #include <vector>

View File

@ -34,6 +34,7 @@
#include "modules/video_coding/include/video_error_codes.h" #include "modules/video_coding/include/video_error_codes.h"
#include "rtc_base/copyonwritebuffer.h" #include "rtc_base/copyonwritebuffer.h"
#include "rtc_base/logging.h" #include "rtc_base/logging.h"
#include "rtc_base/strings/string_builder.h"
#include "rtc_base/stringutils.h" #include "rtc_base/stringutils.h"
#include "rtc_base/timeutils.h" #include "rtc_base/timeutils.h"
#include "rtc_base/trace_event.h" #include "rtc_base/trace_event.h"
@ -168,15 +169,15 @@ std::vector<VideoCodec> AssignPayloadTypesAndDefaultCodecs(
} }
static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) { static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) {
std::stringstream out; rtc::StringBuilder out;
out << '{'; out << "{";
for (size_t i = 0; i < codecs.size(); ++i) { for (size_t i = 0; i < codecs.size(); ++i) {
out << codecs[i].ToString(); out << codecs[i].ToString();
if (i != codecs.size() - 1) { if (i != codecs.size() - 1) {
out << ", "; out << ", ";
} }
} }
out << '}'; out << "}";
return out.str(); return out.str();
} }
@ -940,15 +941,15 @@ bool WebRtcVideoChannel::SetRecvParameters(const VideoRecvParameters& params) {
std::string WebRtcVideoChannel::CodecSettingsVectorToString( std::string WebRtcVideoChannel::CodecSettingsVectorToString(
const std::vector<VideoCodecSettings>& codecs) { const std::vector<VideoCodecSettings>& codecs) {
std::stringstream out; rtc::StringBuilder out;
out << '{'; out << "{";
for (size_t i = 0; i < codecs.size(); ++i) { for (size_t i = 0; i < codecs.size(); ++i) {
out << codecs[i].codec.ToString(); out << codecs[i].codec.ToString();
if (i != codecs.size() - 1) { if (i != codecs.size() - 1) {
out << ", "; out << ", ";
} }
} }
out << '}'; out << "}";
return out.str(); return out.str();
} }

View File

@ -11,6 +11,7 @@
#include "modules/audio_processing/aec3/aec3_fft.h" #include "modules/audio_processing/aec3/aec3_fft.h"
#include <algorithm> #include <algorithm>
#include <functional>
#include "rtc_base/checks.h" #include "rtc_base/checks.h"

View File

@ -11,6 +11,7 @@
#include "modules/audio_processing/aec3/render_buffer.h" #include "modules/audio_processing/aec3/render_buffer.h"
#include <algorithm> #include <algorithm>
#include <functional>
#include "modules/audio_processing/aec3/aec3_common.h" #include "modules/audio_processing/aec3/aec3_common.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"

View File

@ -15,6 +15,7 @@
#include <iterator> #include <iterator>
#include <limits> #include <limits>
#include <memory> #include <memory>
#include <sstream> // no-presubmit-check TODO(webrtc:8982)
#include <string> #include <string>
#include <vector> #include <vector>

View File

@ -12,7 +12,6 @@
#include <algorithm> // For std::find. #include <algorithm> // For std::find.
#include <set> #include <set>
#include <sstream>
#include <utility> // For std::move. #include <utility> // For std::move.
#include "absl/memory/memory.h" #include "absl/memory/memory.h"

View File

@ -12,7 +12,6 @@
#include <algorithm> // For "remove", "find". #include <algorithm> // For "remove", "find".
#include <set> #include <set>
#include <sstream>
#include <unordered_map> #include <unordered_map>
#include <utility> // For std::move. #include <utility> // For std::move.
@ -25,6 +24,7 @@
#include "pc/rtpmediautils.h" #include "pc/rtpmediautils.h"
#include "pc/rtpparametersconversion.h" #include "pc/rtpparametersconversion.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "rtc_base/strings/string_builder.h"
namespace webrtc { namespace webrtc {
@ -39,7 +39,7 @@ static RTCError CheckForIdConflicts(
const std::vector<C2>& codecs_b, const std::vector<C2>& codecs_b,
const cricket::RtpHeaderExtensions& extensions_b, const cricket::RtpHeaderExtensions& extensions_b,
const cricket::StreamParamsVec& streams_b) { const cricket::StreamParamsVec& streams_b) {
std::ostringstream oss; rtc::StringBuilder oss;
// Since it's assumed that C1 and C2 are different types, codecs_a and // Since it's assumed that C1 and C2 are different types, codecs_a and
// codecs_b should never contain the same payload type, and thus we can just // codecs_b should never contain the same payload type, and thus we can just
// use a set. // use a set.

View File

@ -22,6 +22,7 @@
#include "rtc_base/constructormagic.h" #include "rtc_base/constructormagic.h"
#include "rtc_base/sslstreamadapter.h" #include "rtc_base/sslstreamadapter.h"
#include "rtc_base/stream.h" #include "rtc_base/stream.h"
#include "rtc_base/strings/string_builder.h"
#include "rtc_base/thread_checker.h" #include "rtc_base/thread_checker.h"
namespace rtc { namespace rtc {
@ -180,12 +181,12 @@ class DtlsTransport : public DtlsTransportInternal {
int SetOption(rtc::Socket::Option opt, int value) override; int SetOption(rtc::Socket::Option opt, int value) override;
std::string ToString() const { std::string ToString() const {
const char RECEIVING_ABBREV[2] = {'_', 'R'}; const absl::string_view RECEIVING_ABBREV[2] = {"_", "R"};
const char WRITABLE_ABBREV[2] = {'_', 'W'}; const absl::string_view WRITABLE_ABBREV[2] = {"_", "W"};
std::stringstream ss; rtc::StringBuilder sb;
ss << "DtlsTransport[" << transport_name_ << "|" << component_ << "|" sb << "DtlsTransport[" << transport_name_ << "|" << component_ << "|"
<< RECEIVING_ABBREV[receiving()] << WRITABLE_ABBREV[writable()] << "]"; << RECEIVING_ABBREV[receiving()] << WRITABLE_ABBREV[writable()] << "]";
return ss.str(); return sb.str();
} }
private: private:

View File

@ -41,6 +41,7 @@
#include "rtc_base/asyncinvoker.h" #include "rtc_base/asyncinvoker.h"
#include "rtc_base/asyncpacketsocket.h" #include "rtc_base/asyncpacketsocket.h"
#include "rtc_base/constructormagic.h" #include "rtc_base/constructormagic.h"
#include "rtc_base/strings/string_builder.h"
#include "rtc_base/third_party/sigslot/sigslot.h" #include "rtc_base/third_party/sigslot/sigslot.h"
namespace webrtc { namespace webrtc {
@ -171,9 +172,9 @@ class P2PTransportChannel : public IceTransportInternal {
} }
std::string ToString() const { std::string ToString() const {
const char RECEIVING_ABBREV[2] = {'_', 'R'}; const std::string RECEIVING_ABBREV[2] = {"_", "R"};
const char WRITABLE_ABBREV[2] = {'_', 'W'}; const std::string WRITABLE_ABBREV[2] = {"_", "W"};
std::stringstream ss; rtc::StringBuilder ss;
ss << "Channel[" << transport_name_ << "|" << component_ << "|" ss << "Channel[" << transport_name_ << "|" << component_ << "|"
<< RECEIVING_ABBREV[receiving_] << WRITABLE_ABBREV[writable_] << "]"; << RECEIVING_ABBREV[receiving_] << WRITABLE_ABBREV[writable_] << "]";
return ss.str(); return ss.str();

View File

@ -207,9 +207,9 @@ static std::string ComputeFoundation(const std::string& type,
const std::string& protocol, const std::string& protocol,
const std::string& relay_protocol, const std::string& relay_protocol,
const rtc::SocketAddress& base_address) { const rtc::SocketAddress& base_address) {
std::ostringstream ost; rtc::StringBuilder sb;
ost << type << base_address.ipaddr().ToString() << protocol << relay_protocol; sb << type << base_address.ipaddr().ToString() << protocol << relay_protocol;
return rtc::ToString(rtc::ComputeCrc32(ost.str())); return rtc::ToString(rtc::ComputeCrc32(sb.Release()));
} }
CandidateStats::CandidateStats() = default; CandidateStats::CandidateStats() = default;
@ -905,10 +905,10 @@ void Port::OnNetworkTypeChanged(const rtc::Network* network) {
} }
std::string Port::ToString() const { std::string Port::ToString() const {
std::stringstream ss; rtc::StringBuilder ss;
ss << "Port[" << std::hex << this << std::dec << ":" << content_name_ << ":" ss << "Port[" << rtc::ToHex(reinterpret_cast<uintptr_t>(this)) << ":"
<< component_ << ":" << generation_ << ":" << type_ << ":" << content_name_ << ":" << component_ << ":" << generation_ << ":" << type_
<< network_->ToString() << "]"; << ":" << network_->ToString() << "]";
return ss.str(); return ss.str();
} }
@ -1398,8 +1398,7 @@ void Connection::FailAndPrune() {
} }
void Connection::PrintPingsSinceLastResponse(std::string* s, size_t max) { void Connection::PrintPingsSinceLastResponse(std::string* s, size_t max) {
std::ostringstream oss; rtc::StringBuilder oss;
oss << std::boolalpha;
if (pings_since_last_response_.size() > max) { if (pings_since_last_response_.size() > max) {
for (size_t i = 0; i < max; i++) { for (size_t i = 0; i < max; i++) {
const SentPing& ping = pings_since_last_response_[i]; const SentPing& ping = pings_since_last_response_[i];
@ -1561,9 +1560,7 @@ bool Connection::stable(int64_t now) const {
} }
std::string Connection::ToDebugId() const { std::string Connection::ToDebugId() const {
std::stringstream ss; return rtc::ToHex(reinterpret_cast<uintptr_t>(this));
ss << std::hex << this;
return ss.str();
} }
uint32_t Connection::ComputeNetworkCost() const { uint32_t Connection::ComputeNetworkCost() const {
@ -1572,33 +1569,33 @@ uint32_t Connection::ComputeNetworkCost() const {
} }
std::string Connection::ToString() const { std::string Connection::ToString() const {
const char CONNECT_STATE_ABBREV[2] = { const absl::string_view CONNECT_STATE_ABBREV[2] = {
'-', // not connected (false) "-", // not connected (false)
'C', // connected (true) "C", // connected (true)
}; };
const char RECEIVE_STATE_ABBREV[2] = { const absl::string_view RECEIVE_STATE_ABBREV[2] = {
'-', // not receiving (false) "-", // not receiving (false)
'R', // receiving (true) "R", // receiving (true)
}; };
const char WRITE_STATE_ABBREV[4] = { const absl::string_view WRITE_STATE_ABBREV[4] = {
'W', // STATE_WRITABLE "W", // STATE_WRITABLE
'w', // STATE_WRITE_UNRELIABLE "w", // STATE_WRITE_UNRELIABLE
'-', // STATE_WRITE_INIT "-", // STATE_WRITE_INIT
'x', // STATE_WRITE_TIMEOUT "x", // STATE_WRITE_TIMEOUT
}; };
const std::string ICESTATE[4] = { const absl::string_view ICESTATE[4] = {
"W", // STATE_WAITING "W", // STATE_WAITING
"I", // STATE_INPROGRESS "I", // STATE_INPROGRESS
"S", // STATE_SUCCEEDED "S", // STATE_SUCCEEDED
"F" // STATE_FAILED "F" // STATE_FAILED
}; };
const std::string SELECTED_STATE_ABBREV[2] = { const absl::string_view SELECTED_STATE_ABBREV[2] = {
"-", // candidate pair not selected (false) "-", // candidate pair not selected (false)
"S", // selected (true) "S", // selected (true)
}; };
const Candidate& local = local_candidate(); const Candidate& local = local_candidate();
const Candidate& remote = remote_candidate(); const Candidate& remote = remote_candidate();
std::stringstream ss; rtc::StringBuilder ss;
ss << "Conn[" << ToDebugId() << ":" << port_->content_name() << ":" ss << "Conn[" << ToDebugId() << ":" << port_->content_name() << ":"
<< port_->Network()->ToString() << ":" << local.id() << ":" << port_->Network()->ToString() << ":" << local.id() << ":"
<< local.component() << ":" << local.generation() << ":" << local.type() << local.component() << ":" << local.generation() << ":" << local.type()

View File

@ -5393,7 +5393,7 @@ bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) {
SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
} }
// TODO(bemasc): If state is Completed, go back to Connected. // TODO(bemasc): If state is Completed, go back to Connected.
} else if (error.message()) { } else {
RTC_LOG(LS_WARNING) << error.message(); RTC_LOG(LS_WARNING) << error.message();
} }
return true; return true;

View File

@ -689,6 +689,9 @@ rtc_source_set("rtc_json") {
"json.cc", "json.cc",
"json.h", "json.h",
] ]
deps = [
":stringutils",
]
all_dependent_configs = [ "//third_party/jsoncpp:jsoncpp_config" ] all_dependent_configs = [ "//third_party/jsoncpp:jsoncpp_config" ]
if (rtc_build_json) { if (rtc_build_json) {
public_deps = [ public_deps = [

View File

@ -40,7 +40,6 @@ RTC_NORETURN void rtc_FatalMessage(const char* file, int line, const char* msg);
#ifdef __cplusplus #ifdef __cplusplus
// C++ version. // C++ version.
#include <sstream> // no-presubmit-check TODO(webrtc:8982)
#include <string> #include <string>
#include "rtc_base/numerics/safe_compare.h" #include "rtc_base/numerics/safe_compare.h"

View File

@ -14,25 +14,23 @@
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include <sstream> #include "rtc_base/stringencode.h"
namespace rtc { namespace rtc {
bool GetStringFromJson(const Json::Value& in, std::string* out) { bool GetStringFromJson(const Json::Value& in, std::string* out) {
if (!in.isString()) { if (!in.isString()) {
std::ostringstream s;
if (in.isBool()) { if (in.isBool()) {
s << std::boolalpha << in.asBool(); *out = rtc::ToString(in.asBool());
} else if (in.isInt()) { } else if (in.isInt()) {
s << in.asInt(); *out = rtc::ToString(in.asInt());
} else if (in.isUInt()) { } else if (in.isUInt()) {
s << in.asUInt(); *out = rtc::ToString(in.asUInt());
} else if (in.isDouble()) { } else if (in.isDouble()) {
s << in.asDouble(); *out = rtc::ToString(in.asDouble());
} else { } else {
return false; return false;
} }
*out = s.str();
} else { } else {
*out = in.asString(); *out = in.asString();
} }

View File

@ -31,8 +31,6 @@ static const int kMaxLogLineSize = 1024 - 60;
#include <algorithm> #include <algorithm>
#include <cstdarg> #include <cstdarg>
#include <iomanip>
#include <ostream>
#include <vector> #include <vector>
#include "rtc_base/criticalsection.h" #include "rtc_base/criticalsection.h"

View File

@ -47,7 +47,7 @@
#include <errno.h> #include <errno.h>
#include <list> #include <list>
#include <sstream> #include <sstream> // no-presubmit-check TODO(webrtc:8982)
#include <string> #include <string>
#include <utility> #include <utility>

View File

@ -11,7 +11,6 @@
#ifndef RTC_BASE_SOCKETADDRESS_H_ #ifndef RTC_BASE_SOCKETADDRESS_H_
#define RTC_BASE_SOCKETADDRESS_H_ #define RTC_BASE_SOCKETADDRESS_H_
#include <iosfwd>
#include <string> #include <string>
#ifdef UNIT_TEST #ifdef UNIT_TEST
#include <ostream> // no-presubmit-check TODO(webrtc:8982) #include <ostream> // no-presubmit-check TODO(webrtc:8982)

View File

@ -11,7 +11,6 @@
#ifndef RTC_BASE_STRINGENCODE_H_ #ifndef RTC_BASE_STRINGENCODE_H_
#define RTC_BASE_STRINGENCODE_H_ #define RTC_BASE_STRINGENCODE_H_
#include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>

View File

@ -8,6 +8,8 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
#include <bitset>
#include "modules/rtp_rtcp/include/rtp_header_extension_map.h" #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
#include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h" #include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
#include "modules/rtp_rtcp/source/rtp_header_extensions.h" #include "modules/rtp_rtcp/source/rtp_header_extensions.h"