Skip to content

Commit

Permalink
Remove all remaining non-test uses of std::stringstream.
Browse files Browse the repository at this point in the history
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}
  • Loading branch information
Jonas Olsson authored and Commit Bot committed Sep 13, 2018
1 parent 0961933 commit 941a07c
Show file tree
Hide file tree
Showing 25 changed files with 74 additions and 126 deletions.
51 changes: 5 additions & 46 deletions api/rtcerror.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,61 +36,20 @@ 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() {
return RTCError();
}

const char* RTCError::message() const {
if (have_string_message_) {
return string_message_.c_str();
} else {
return static_message_;
}
}

void RTCError::set_message(const char* message) {
if (have_string_message_) {
string_message_.~basic_string();
have_string_message_ = false;
}
static_message_ = message;
return message_.c_str();
}

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.
Expand Down
28 changes: 6 additions & 22 deletions api/rtcerror.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -118,27 +113,16 @@ 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.
bool ok() const { return type_ == RTCErrorType::NONE; }

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
Expand Down
3 changes: 2 additions & 1 deletion api/rtcerror_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions api/units/time_delta.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <stdint.h>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <string>

Expand Down
1 change: 1 addition & 0 deletions examples/stunprober/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions logging/rtc_event_log/rtc_event_log_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions logging/rtc_event_log/rtc_event_log_parser_new.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
13 changes: 7 additions & 6 deletions media/engine/webrtcvideoengine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions modules/audio_processing/aec3/aec3_fft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "modules/audio_processing/aec3/aec3_fft.h"

#include <algorithm>
#include <functional>

#include "rtc_base/checks.h"

Expand Down
1 change: 1 addition & 0 deletions modules/audio_processing/aec3/render_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions modules/audio_processing/test/test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <iterator>
#include <limits>
#include <memory>
#include <sstream> // no-presubmit-check TODO(webrtc:8982)
#include <string>
#include <vector>

Expand Down
1 change: 0 additions & 1 deletion ortc/rtptransportadapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <algorithm> // For std::find.
#include <set>
#include <sstream>
#include <utility> // For std::move.

#include "absl/memory/memory.h"
Expand Down
4 changes: 2 additions & 2 deletions ortc/rtptransportcontrolleradapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <algorithm> // For "remove", "find".
#include <set>
#include <sstream>
#include <unordered_map>
#include <utility> // For std::move.

Expand All @@ -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 {

Expand All @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions p2p/base/dtlstransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions p2p/base/p2ptransportchannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 941a07c

Please sign in to comment.