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 {
RTCError::RTCError(RTCError&& other)
: type_(other.type_), have_string_message_(other.have_string_message_) {
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();
}
}
RTCError::RTCError(RTCError&& other) = default;
RTCError& RTCError::operator=(RTCError&& other) = default;
// static
RTCError RTCError::OK() {
@ -69,28 +45,11 @@ RTCError RTCError::OK() {
}
const char* RTCError::message() const {
if (have_string_message_) {
return string_message_.c_str();
} else {
return static_message_;
}
return message_.c_str();
}
void RTCError::set_message(const char* message) {
if (have_string_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;
}
void RTCError::set_message(std::string message) {
message_ = std::move(message);
}
// 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.
RTCError() {}
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, const char* 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) {}
RTCError(RTCErrorType type, std::string message)
: type_(type), message_(std::move(message)) {}
// 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.
@ -103,8 +100,6 @@ class RTCError {
RTCError(RTCError&& other);
RTCError& operator=(RTCError&& other);
~RTCError();
// Identical to default constructed error.
//
// 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
// stable.
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(const char* message);
void set_message(std::string&& message);
void set_message(std::string message);
// Convenience method for situations where you only care whether or not an
// error occurred.
@ -129,16 +122,7 @@ class RTCError {
private:
RTCErrorType type_ = RTCErrorType::NONE;
// For performance, we use static strings wherever possible. But in some
// 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;
std::string message_;
};
// 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)
TEST(RTCErrorOrDeathTest, ConstructWithOkError) {
EXPECT_DEATH(RTCErrorOr<int> err = RTCError::OK(), "");
RTCErrorOr<int> err;
EXPECT_DEATH(err = RTCError::OK(), "");
}
TEST(RTCErrorOrDeathTest, DereferenceErrorValue) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,7 +12,6 @@
#include <algorithm> // For "remove", "find".
#include <set>
#include <sstream>
#include <unordered_map>
#include <utility> // For std::move.
@ -25,6 +24,7 @@
#include "pc/rtpmediautils.h"
#include "pc/rtpparametersconversion.h"
#include "rtc_base/checks.h"
#include "rtc_base/strings/string_builder.h"
namespace webrtc {
@ -39,7 +39,7 @@ static RTCError CheckForIdConflicts(
const std::vector<C2>& codecs_b,
const cricket::RtpHeaderExtensions& extensions_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
// codecs_b should never contain the same payload type, and thus we can just
// use a set.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -8,6 +8,8 @@
* 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/source/rtp_generic_frame_descriptor_extension.h"
#include "modules/rtp_rtcp/source/rtp_header_extensions.h"