Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SBError.cpp ---------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/API/SBError.h" |
| 11 | #include "lldb/Core/Error.h" |
Eli Friedman | 7a62c8b | 2010-06-09 07:44:37 +0000 | [diff] [blame] | 12 | #include <cstdarg> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace lldb; |
| 15 | using namespace lldb_private; |
| 16 | |
| 17 | |
| 18 | SBError::SBError () : |
| 19 | m_lldb_object_ap () |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | SBError::SBError (const SBError &rhs) : |
| 24 | m_lldb_object_ap () |
| 25 | { |
| 26 | if (rhs.IsValid()) |
| 27 | m_lldb_object_ap.reset (new Error(*rhs)); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | SBError::~SBError() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | const SBError & |
| 36 | SBError::operator = (const SBError &rhs) |
| 37 | { |
| 38 | if (rhs.IsValid()) |
| 39 | { |
| 40 | if (m_lldb_object_ap.get()) |
| 41 | *m_lldb_object_ap = *rhs; |
| 42 | else |
| 43 | m_lldb_object_ap.reset (new Error(*rhs)); |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | m_lldb_object_ap.reset(); |
| 48 | } |
| 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | const char * |
| 54 | SBError::GetCString () const |
| 55 | { |
| 56 | if (m_lldb_object_ap.get()) |
| 57 | return m_lldb_object_ap->AsCString(); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | SBError::Clear () |
| 63 | { |
| 64 | if (m_lldb_object_ap.get()) |
| 65 | m_lldb_object_ap->Clear(); |
| 66 | } |
| 67 | |
| 68 | bool |
| 69 | SBError::Fail () const |
| 70 | { |
| 71 | if (m_lldb_object_ap.get()) |
| 72 | return m_lldb_object_ap->Fail(); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | bool |
| 77 | SBError::Success () const |
| 78 | { |
| 79 | if (m_lldb_object_ap.get()) |
| 80 | return m_lldb_object_ap->Success(); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | uint32_t |
| 85 | SBError::GetError () const |
| 86 | { |
| 87 | if (m_lldb_object_ap.get()) |
| 88 | return m_lldb_object_ap->GetError(); |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | ErrorType |
| 93 | SBError::GetType () const |
| 94 | { |
| 95 | if (m_lldb_object_ap.get()) |
| 96 | return m_lldb_object_ap->GetType(); |
| 97 | return eErrorTypeInvalid; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | SBError::SetError (uint32_t err, ErrorType type) |
| 102 | { |
| 103 | CreateIfNeeded (); |
| 104 | m_lldb_object_ap->SetError (err, type); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | SBError::SetError (const Error &lldb_error) |
| 109 | { |
| 110 | CreateIfNeeded (); |
| 111 | *m_lldb_object_ap = lldb_error; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void |
| 116 | SBError::SetErrorToErrno () |
| 117 | { |
| 118 | CreateIfNeeded (); |
| 119 | m_lldb_object_ap->SetErrorToErrno (); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | SBError::SetErrorToGenericError () |
| 124 | { |
| 125 | CreateIfNeeded (); |
| 126 | m_lldb_object_ap->SetErrorToErrno (); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | SBError::SetErrorString (const char *err_str) |
| 131 | { |
| 132 | CreateIfNeeded (); |
| 133 | m_lldb_object_ap->SetErrorString (err_str); |
| 134 | } |
| 135 | |
| 136 | int |
| 137 | SBError::SetErrorStringWithFormat (const char *format, ...) |
| 138 | { |
| 139 | CreateIfNeeded (); |
| 140 | va_list args; |
| 141 | va_start (args, format); |
| 142 | int num_chars = m_lldb_object_ap->SetErrorStringWithVarArg (format, args); |
| 143 | va_end (args); |
| 144 | return num_chars; |
| 145 | } |
| 146 | |
| 147 | bool |
| 148 | SBError::IsValid () const |
| 149 | { |
| 150 | return m_lldb_object_ap.get() != NULL; |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | SBError::CreateIfNeeded () |
| 155 | { |
| 156 | if (m_lldb_object_ap.get() == NULL) |
| 157 | m_lldb_object_ap.reset(new Error ()); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | lldb_private::Error * |
| 162 | SBError::operator->() |
| 163 | { |
| 164 | return m_lldb_object_ap.get(); |
| 165 | } |
| 166 | |
| 167 | lldb_private::Error * |
| 168 | SBError::get() |
| 169 | { |
| 170 | return m_lldb_object_ap.get(); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | const lldb_private::Error & |
| 175 | SBError::operator*() const |
| 176 | { |
| 177 | // Be sure to call "IsValid()" before calling this function or it will crash |
| 178 | return *m_lldb_object_ap; |
| 179 | } |
| 180 | |