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/API/SBStream.h"
12 #include "lldb/Core/Error.h"
13 #include "lldb/Core/Log.h"
14
15 #include <stdarg.h>
16
17 using namespace lldb;
18 using namespace lldb_private;
19
20
SBError()21 SBError::SBError () :
22 m_opaque_ap ()
23 {
24 }
25
SBError(const SBError & rhs)26 SBError::SBError (const SBError &rhs) :
27 m_opaque_ap ()
28 {
29 if (rhs.IsValid())
30 m_opaque_ap.reset (new Error(*rhs));
31 }
32
33
~SBError()34 SBError::~SBError()
35 {
36 }
37
38 const SBError &
operator =(const SBError & rhs)39 SBError::operator = (const SBError &rhs)
40 {
41 if (rhs.IsValid())
42 {
43 if (m_opaque_ap.get())
44 *m_opaque_ap = *rhs;
45 else
46 m_opaque_ap.reset (new Error(*rhs));
47 }
48 else
49 m_opaque_ap.reset();
50
51 return *this;
52 }
53
54
55 const char *
GetCString() const56 SBError::GetCString () const
57 {
58 if (m_opaque_ap.get())
59 return m_opaque_ap->AsCString();
60 return NULL;
61 }
62
63 void
Clear()64 SBError::Clear ()
65 {
66 if (m_opaque_ap.get())
67 m_opaque_ap->Clear();
68 }
69
70 bool
Fail() const71 SBError::Fail () const
72 {
73 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
74
75 bool ret_value = false;
76 if (m_opaque_ap.get())
77 ret_value = m_opaque_ap->Fail();
78
79 if (log)
80 log->Printf ("SBError(%p)::Fail () => %i", m_opaque_ap.get(), ret_value);
81
82 return ret_value;
83 }
84
85 bool
Success() const86 SBError::Success () const
87 {
88 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
89 bool ret_value = true;
90 if (m_opaque_ap.get())
91 ret_value = m_opaque_ap->Success();
92
93 if (log)
94 log->Printf ("SBError(%p)::Success () => %i", m_opaque_ap.get(), ret_value);
95
96 return ret_value;
97 }
98
99 uint32_t
GetError() const100 SBError::GetError () const
101 {
102 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
103
104 uint32_t err = 0;
105 if (m_opaque_ap.get())
106 err = m_opaque_ap->GetError();
107
108 if (log)
109 log->Printf ("SBError(%p)::GetError () => 0x%8.8x", m_opaque_ap.get(), err);
110
111
112 return err;
113 }
114
115 ErrorType
GetType() const116 SBError::GetType () const
117 {
118 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
119 ErrorType err_type = eErrorTypeInvalid;
120 if (m_opaque_ap.get())
121 err_type = m_opaque_ap->GetType();
122
123 if (log)
124 log->Printf ("SBError(%p)::GetType () => %i", m_opaque_ap.get(), err_type);
125
126 return err_type;
127 }
128
129 void
SetError(uint32_t err,ErrorType type)130 SBError::SetError (uint32_t err, ErrorType type)
131 {
132 CreateIfNeeded ();
133 m_opaque_ap->SetError (err, type);
134 }
135
136 void
SetError(const Error & lldb_error)137 SBError::SetError (const Error &lldb_error)
138 {
139 CreateIfNeeded ();
140 *m_opaque_ap = lldb_error;
141 }
142
143
144 void
SetErrorToErrno()145 SBError::SetErrorToErrno ()
146 {
147 CreateIfNeeded ();
148 m_opaque_ap->SetErrorToErrno ();
149 }
150
151 void
SetErrorToGenericError()152 SBError::SetErrorToGenericError ()
153 {
154 CreateIfNeeded ();
155 m_opaque_ap->SetErrorToErrno ();
156 }
157
158 void
SetErrorString(const char * err_str)159 SBError::SetErrorString (const char *err_str)
160 {
161 CreateIfNeeded ();
162 m_opaque_ap->SetErrorString (err_str);
163 }
164
165 int
SetErrorStringWithFormat(const char * format,...)166 SBError::SetErrorStringWithFormat (const char *format, ...)
167 {
168 CreateIfNeeded ();
169 va_list args;
170 va_start (args, format);
171 int num_chars = m_opaque_ap->SetErrorStringWithVarArg (format, args);
172 va_end (args);
173 return num_chars;
174 }
175
176 bool
IsValid() const177 SBError::IsValid () const
178 {
179 return m_opaque_ap.get() != NULL;
180 }
181
182 void
CreateIfNeeded()183 SBError::CreateIfNeeded ()
184 {
185 if (m_opaque_ap.get() == NULL)
186 m_opaque_ap.reset(new Error ());
187 }
188
189
190 lldb_private::Error *
operator ->()191 SBError::operator->()
192 {
193 return m_opaque_ap.get();
194 }
195
196 lldb_private::Error *
get()197 SBError::get()
198 {
199 return m_opaque_ap.get();
200 }
201
202 lldb_private::Error &
ref()203 SBError::ref()
204 {
205 CreateIfNeeded();
206 return *m_opaque_ap;
207 }
208
209 const lldb_private::Error &
operator *() const210 SBError::operator*() const
211 {
212 // Be sure to call "IsValid()" before calling this function or it will crash
213 return *m_opaque_ap;
214 }
215
216 bool
GetDescription(SBStream & description)217 SBError::GetDescription (SBStream &description)
218 {
219 if (m_opaque_ap.get())
220 {
221 if (m_opaque_ap->Success())
222 description.Printf ("success");
223 else
224 {
225 const char * err_string = GetCString();
226 description.Printf ("error: %s", (err_string != NULL ? err_string : ""));
227 }
228 }
229 else
230 description.Printf ("error: <NULL>");
231
232 return true;
233 }
234