blob: f9a31d0893485b5b8404f10a5ff299aba8687134 [file] [log] [blame]
deadbeef6038e972017-02-16 23:31:33 -08001/*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/rtcerror.h"
deadbeef6038e972017-02-16 23:31:33 -080012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/arraysize.h"
deadbeef6038e972017-02-16 23:31:33 -080014
15namespace {
16
17static const char* const kRTCErrorTypeNames[] = {
18 "NONE",
19 "UNSUPPORTED_OPERATION",
20 "UNSUPPORTED_PARAMETER",
21 "INVALID_PARAMETER",
22 "INVALID_RANGE",
23 "SYNTAX_ERROR",
24 "INVALID_STATE",
25 "INVALID_MODIFICATION",
26 "NETWORK_ERROR",
27 "RESOURCE_EXHAUSTED",
28 "INTERNAL_ERROR",
29};
30static_assert(static_cast<int>(webrtc::RTCErrorType::INTERNAL_ERROR) ==
31 (arraysize(kRTCErrorTypeNames) - 1),
32 "kRTCErrorTypeNames must have as many strings as RTCErrorType "
33 "has values.");
34
35} // namespace
36
37namespace webrtc {
38
39RTCError::RTCError(RTCError&& other)
40 : type_(other.type_), have_string_message_(other.have_string_message_) {
41 if (have_string_message_) {
42 new (&string_message_) std::string(std::move(other.string_message_));
43 } else {
44 static_message_ = other.static_message_;
45 }
46}
47
48RTCError& RTCError::operator=(RTCError&& other) {
49 type_ = other.type_;
50 if (other.have_string_message_) {
51 set_message(std::move(other.string_message_));
52 } else {
53 set_message(other.static_message_);
54 }
55 return *this;
56}
57
58RTCError::~RTCError() {
59 // If we hold a message string that was built, rather than a static string,
60 // we need to delete it.
61 if (have_string_message_) {
62 string_message_.~basic_string();
63 }
64}
65
66// static
67RTCError RTCError::OK() {
68 return RTCError();
69}
70
71const char* RTCError::message() const {
72 if (have_string_message_) {
73 return string_message_.c_str();
74 } else {
75 return static_message_;
76 }
77}
78
79void RTCError::set_message(const char* message) {
80 if (have_string_message_) {
81 string_message_.~basic_string();
82 have_string_message_ = false;
83 }
84 static_message_ = message;
85}
86
87void RTCError::set_message(std::string&& message) {
88 if (!have_string_message_) {
89 new (&string_message_) std::string(std::move(message));
90 have_string_message_ = true;
91 } else {
92 string_message_ = message;
93 }
94}
95
96std::ostream& operator<<(std::ostream& stream, RTCErrorType error) {
97 int index = static_cast<int>(error);
98 return stream << kRTCErrorTypeNames[index];
99}
100
101} // namespace webrtc