blob: 3e91ea07b9b4504210a1f1d69010a7c2205c464c [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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"
Caroline Tice98f930f2010-09-20 05:20:02 +000011#include "lldb/API/SBStream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/Core/Error.h"
Caroline Tice7826c882010-10-26 03:11:13 +000013#include "lldb/Core/Log.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000014
Eli Friedman1501a962010-06-09 08:46:23 +000015#include <stdarg.h>
Chris Lattner24943d22010-06-08 16:52:24 +000016
17using namespace lldb;
18using namespace lldb_private;
19
20
21SBError::SBError () :
Greg Clayton63094e02010-06-23 01:19:29 +000022 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000023{
Caroline Tice7826c882010-10-26 03:11:13 +000024 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API | LIBLLDB_LOG_VERBOSE);
25
26 if (log)
27 log->Printf ("SBError::SBError () ==> this = %p", this);
Chris Lattner24943d22010-06-08 16:52:24 +000028}
29
30SBError::SBError (const SBError &rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +000031 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000032{
Caroline Tice7826c882010-10-26 03:11:13 +000033 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API | LIBLLDB_LOG_VERBOSE);
34
Chris Lattner24943d22010-06-08 16:52:24 +000035 if (rhs.IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000036 m_opaque_ap.reset (new Error(*rhs));
Caroline Tice7826c882010-10-26 03:11:13 +000037
38 if (log)
39 {
40 SBStream sstr;
41 GetDescription (sstr);
42 log->Printf ("SBError::SBError (const SBError &rhs) rhs.m_opaque_ap.get() = %p ==> this = %p (%s)",
43 (rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL), this, sstr.GetData());
44 }
Chris Lattner24943d22010-06-08 16:52:24 +000045}
46
47
48SBError::~SBError()
49{
50}
51
52const SBError &
53SBError::operator = (const SBError &rhs)
54{
Caroline Tice7826c882010-10-26 03:11:13 +000055 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
56
57 if (log)
58 {
59 SBStream sstr;
60 rhs.GetDescription (sstr);
61 log->Printf ("SBError::operator= (const SBError &rhs) rhs.m_opaque_ap.get() = %p (%s)",
62 (rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL), sstr.GetData());
63 }
64
Chris Lattner24943d22010-06-08 16:52:24 +000065 if (rhs.IsValid())
66 {
Greg Clayton63094e02010-06-23 01:19:29 +000067 if (m_opaque_ap.get())
68 *m_opaque_ap = *rhs;
Chris Lattner24943d22010-06-08 16:52:24 +000069 else
Greg Clayton63094e02010-06-23 01:19:29 +000070 m_opaque_ap.reset (new Error(*rhs));
Chris Lattner24943d22010-06-08 16:52:24 +000071 }
72 else
73 {
Greg Clayton63094e02010-06-23 01:19:29 +000074 m_opaque_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +000075 }
Caroline Tice7826c882010-10-26 03:11:13 +000076
77 if (log)
78 log->Printf ("SBError::operator= ==> this = %p", this);
79
Chris Lattner24943d22010-06-08 16:52:24 +000080 return *this;
81}
82
83
84const char *
85SBError::GetCString () const
86{
Greg Clayton63094e02010-06-23 01:19:29 +000087 if (m_opaque_ap.get())
88 return m_opaque_ap->AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000089 return NULL;
90}
91
92void
93SBError::Clear ()
94{
Greg Clayton63094e02010-06-23 01:19:29 +000095 if (m_opaque_ap.get())
96 m_opaque_ap->Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000097}
98
99bool
100SBError::Fail () const
101{
Caroline Tice7826c882010-10-26 03:11:13 +0000102 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
103
104 if (log)
105 log->Printf ("SBError::Fail ()");
106
107 bool ret_value = false;
Greg Clayton63094e02010-06-23 01:19:29 +0000108 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +0000109 ret_value = m_opaque_ap->Fail();
110
111 if (log)
112 log->Printf ("SBError::Fail ==> %s", (ret_value ? "true" : "false"));
113
114 return ret_value;
Chris Lattner24943d22010-06-08 16:52:24 +0000115}
116
117bool
118SBError::Success () const
119{
Greg Clayton63094e02010-06-23 01:19:29 +0000120 if (m_opaque_ap.get())
121 return m_opaque_ap->Success();
Chris Lattner24943d22010-06-08 16:52:24 +0000122 return false;
123}
124
125uint32_t
126SBError::GetError () const
127{
Greg Clayton63094e02010-06-23 01:19:29 +0000128 if (m_opaque_ap.get())
129 return m_opaque_ap->GetError();
Chris Lattner24943d22010-06-08 16:52:24 +0000130 return true;
131}
132
133ErrorType
134SBError::GetType () const
135{
Greg Clayton63094e02010-06-23 01:19:29 +0000136 if (m_opaque_ap.get())
137 return m_opaque_ap->GetType();
Chris Lattner24943d22010-06-08 16:52:24 +0000138 return eErrorTypeInvalid;
139}
140
141void
142SBError::SetError (uint32_t err, ErrorType type)
143{
144 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000145 m_opaque_ap->SetError (err, type);
Chris Lattner24943d22010-06-08 16:52:24 +0000146}
147
148void
149SBError::SetError (const Error &lldb_error)
150{
151 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000152 *m_opaque_ap = lldb_error;
Chris Lattner24943d22010-06-08 16:52:24 +0000153}
154
155
156void
157SBError::SetErrorToErrno ()
158{
159 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000160 m_opaque_ap->SetErrorToErrno ();
Chris Lattner24943d22010-06-08 16:52:24 +0000161}
162
163void
164SBError::SetErrorToGenericError ()
165{
166 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000167 m_opaque_ap->SetErrorToErrno ();
Chris Lattner24943d22010-06-08 16:52:24 +0000168}
169
170void
171SBError::SetErrorString (const char *err_str)
172{
173 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000174 m_opaque_ap->SetErrorString (err_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000175}
176
177int
178SBError::SetErrorStringWithFormat (const char *format, ...)
179{
180 CreateIfNeeded ();
181 va_list args;
182 va_start (args, format);
Greg Clayton63094e02010-06-23 01:19:29 +0000183 int num_chars = m_opaque_ap->SetErrorStringWithVarArg (format, args);
Chris Lattner24943d22010-06-08 16:52:24 +0000184 va_end (args);
185 return num_chars;
186}
187
188bool
189SBError::IsValid () const
190{
Greg Clayton63094e02010-06-23 01:19:29 +0000191 return m_opaque_ap.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000192}
193
194void
195SBError::CreateIfNeeded ()
196{
Greg Clayton63094e02010-06-23 01:19:29 +0000197 if (m_opaque_ap.get() == NULL)
198 m_opaque_ap.reset(new Error ());
Chris Lattner24943d22010-06-08 16:52:24 +0000199}
200
201
202lldb_private::Error *
203SBError::operator->()
204{
Greg Clayton63094e02010-06-23 01:19:29 +0000205 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000206}
207
208lldb_private::Error *
209SBError::get()
210{
Greg Clayton63094e02010-06-23 01:19:29 +0000211 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000212}
213
214
215const lldb_private::Error &
216SBError::operator*() const
217{
218 // Be sure to call "IsValid()" before calling this function or it will crash
Greg Clayton63094e02010-06-23 01:19:29 +0000219 return *m_opaque_ap;
Chris Lattner24943d22010-06-08 16:52:24 +0000220}
221
Caroline Tice98f930f2010-09-20 05:20:02 +0000222bool
223SBError::GetDescription (SBStream &description)
224{
225 if (m_opaque_ap.get())
226 {
227 if (Success())
228 description.Printf ("Status: Success");
229 else
230 {
231 const char * err_string = GetCString();
232 description.Printf ("Status: Error: %s", (err_string != NULL ? err_string : ""));
233 }
234 }
235 else
236 description.Printf ("No value");
237
238 return true;
239}
Caroline Tice7826c882010-10-26 03:11:13 +0000240
241bool
242SBError::GetDescription (SBStream &description) const
243{
244 if (m_opaque_ap.get())
245 {
246 if (Success())
247 description.Printf ("Status: Success");
248 else
249 {
250 const char * err_string = GetCString();
251 description.Printf ("Status: Error: %s", (err_string != NULL ? err_string : ""));
252 }
253 }
254 else
255 description.Printf ("No value");
256
257 return true;
258}