deadbeef | 52f9034 | 2017-02-17 07:31:33 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include "webrtc/api/rtcerror.h" |
| 12 | |
| 13 | #include "webrtc/base/arraysize.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | static 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 | }; |
| 30 | static_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 | |
| 37 | namespace webrtc { |
| 38 | |
| 39 | RTCError::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 | |
| 48 | RTCError& 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 | |
| 58 | RTCError::~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 |
| 67 | RTCError RTCError::OK() { |
| 68 | return RTCError(); |
| 69 | } |
| 70 | |
| 71 | const char* RTCError::message() const { |
| 72 | if (have_string_message_) { |
| 73 | return string_message_.c_str(); |
| 74 | } else { |
| 75 | return static_message_; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void 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 | |
| 87 | void 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 | |
| 96 | std::ostream& operator<<(std::ostream& stream, RTCErrorType error) { |
| 97 | int index = static_cast<int>(error); |
| 98 | return stream << kRTCErrorTypeNames[index]; |
| 99 | } |
| 100 | |
| 101 | } // namespace webrtc |