Using absl::string_view to stringify an RTCErrorType.

Bug: webrtc:10198
Change-Id: Ie7fdba08df219a03ebe2ee5521c2840f28571bba
Reviewed-on: https://webrtc-review.googlesource.com/c/117162
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26277}
This commit is contained in:
Mirko Bonadei
2019-01-16 12:14:29 +01:00
committed by Commit Bot
parent 0acffb5b36
commit 254ecffacf
2 changed files with 8 additions and 6 deletions

View File

@ -10,11 +10,12 @@
#include "api/rtc_error.h" #include "api/rtc_error.h"
#include "absl/strings/string_view.h"
#include "rtc_base/arraysize.h" #include "rtc_base/arraysize.h"
namespace { namespace {
static const char* const kRTCErrorTypeNames[] = { const absl::string_view kRTCErrorTypeNames[] = {
"NONE", "NONE",
"UNSUPPORTED_OPERATION", "UNSUPPORTED_OPERATION",
"UNSUPPORTED_PARAMETER", "UNSUPPORTED_PARAMETER",
@ -52,10 +53,9 @@ void RTCError::set_message(std::string message) {
message_ = std::move(message); message_ = std::move(message);
} }
// TODO(jonasolsson): Change to use absl::string_view when it's available. absl::string_view ToString(RTCErrorType error) {
std::string ToString(RTCErrorType error) {
int index = static_cast<int>(error); int index = static_cast<int>(error);
return std::string(kRTCErrorTypeNames[index]); return kRTCErrorTypeNames[index];
} }
} // namespace webrtc } // namespace webrtc

View File

@ -17,6 +17,7 @@
#include <string> #include <string>
#include <utility> // For std::move. #include <utility> // For std::move.
#include "absl/strings/string_view.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "rtc_base/logging.h" #include "rtc_base/logging.h"
@ -128,8 +129,9 @@ class RTCError {
// 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
// error type. // error type.
// //
// Only intended to be used for logging/disagnostics. // Only intended to be used for logging/diagnostics. The string_view points
std::string ToString(RTCErrorType error); // to literal string that lives for the whole duration of the program.
absl::string_view ToString(RTCErrorType error);
#ifdef UNIT_TEST #ifdef UNIT_TEST
inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)