blob: 2f33e7c6bd8d8534b56962978c8b5b263586cb7e [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{
24}
25
26SBError::SBError (const SBError &rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +000027 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000028{
Caroline Tice61ba7ec2010-10-26 23:49:36 +000029 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Tice7826c882010-10-26 03:11:13 +000030
Chris Lattner24943d22010-06-08 16:52:24 +000031 if (rhs.IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000032 m_opaque_ap.reset (new Error(*rhs));
Caroline Tice7826c882010-10-26 03:11:13 +000033
34 if (log)
35 {
36 SBStream sstr;
37 GetDescription (sstr);
Caroline Tice61ba7ec2010-10-26 23:49:36 +000038 log->Printf ("SBError::SBError (const SBError rhs.ap=%p) => this.ap = %p (%s)",
39 (rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL), m_opaque_ap.get(), sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +000040 }
Chris Lattner24943d22010-06-08 16:52:24 +000041}
42
43
44SBError::~SBError()
45{
46}
47
48const SBError &
49SBError::operator = (const SBError &rhs)
50{
Caroline Tice7826c882010-10-26 03:11:13 +000051 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
52
Chris Lattner24943d22010-06-08 16:52:24 +000053 if (rhs.IsValid())
54 {
Greg Clayton63094e02010-06-23 01:19:29 +000055 if (m_opaque_ap.get())
56 *m_opaque_ap = *rhs;
Chris Lattner24943d22010-06-08 16:52:24 +000057 else
Greg Clayton63094e02010-06-23 01:19:29 +000058 m_opaque_ap.reset (new Error(*rhs));
Chris Lattner24943d22010-06-08 16:52:24 +000059 }
60 else
61 {
Greg Clayton63094e02010-06-23 01:19:29 +000062 m_opaque_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +000063 }
Caroline Tice7826c882010-10-26 03:11:13 +000064
65 if (log)
Caroline Tice61ba7ec2010-10-26 23:49:36 +000066 {
67 SBStream sstr;
68 GetDescription (sstr);
69 log->Printf ("SBError::operator= (this.ap=%p, rhs.ap=%p) => this (%s)",
70 m_opaque_ap.get(), (rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL), sstr.GetData());
71 }
Caroline Tice7826c882010-10-26 03:11:13 +000072
Chris Lattner24943d22010-06-08 16:52:24 +000073 return *this;
74}
75
76
77const char *
78SBError::GetCString () const
79{
Greg Clayton63094e02010-06-23 01:19:29 +000080 if (m_opaque_ap.get())
81 return m_opaque_ap->AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000082 return NULL;
83}
84
85void
86SBError::Clear ()
87{
Greg Clayton63094e02010-06-23 01:19:29 +000088 if (m_opaque_ap.get())
89 m_opaque_ap->Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000090}
91
92bool
93SBError::Fail () const
94{
Caroline Tice7826c882010-10-26 03:11:13 +000095 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
96
Caroline Tice61ba7ec2010-10-26 23:49:36 +000097 //if (log)
98 // log->Printf ("SBError::Fail ()");
Caroline Tice7826c882010-10-26 03:11:13 +000099
100 bool ret_value = false;
Greg Clayton63094e02010-06-23 01:19:29 +0000101 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +0000102 ret_value = m_opaque_ap->Fail();
103
104 if (log)
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000105 log->Printf ("SBError::Fail (this.ap=%p) => '%s'", m_opaque_ap.get(), (ret_value ? "true" : "false"));
Caroline Tice7826c882010-10-26 03:11:13 +0000106
107 return ret_value;
Chris Lattner24943d22010-06-08 16:52:24 +0000108}
109
110bool
111SBError::Success () const
112{
Greg Clayton63094e02010-06-23 01:19:29 +0000113 if (m_opaque_ap.get())
114 return m_opaque_ap->Success();
Chris Lattner24943d22010-06-08 16:52:24 +0000115 return false;
116}
117
118uint32_t
119SBError::GetError () const
120{
Greg Clayton63094e02010-06-23 01:19:29 +0000121 if (m_opaque_ap.get())
122 return m_opaque_ap->GetError();
Chris Lattner24943d22010-06-08 16:52:24 +0000123 return true;
124}
125
126ErrorType
127SBError::GetType () const
128{
Greg Clayton63094e02010-06-23 01:19:29 +0000129 if (m_opaque_ap.get())
130 return m_opaque_ap->GetType();
Chris Lattner24943d22010-06-08 16:52:24 +0000131 return eErrorTypeInvalid;
132}
133
134void
135SBError::SetError (uint32_t err, ErrorType type)
136{
137 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000138 m_opaque_ap->SetError (err, type);
Chris Lattner24943d22010-06-08 16:52:24 +0000139}
140
141void
142SBError::SetError (const Error &lldb_error)
143{
144 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000145 *m_opaque_ap = lldb_error;
Chris Lattner24943d22010-06-08 16:52:24 +0000146}
147
148
149void
150SBError::SetErrorToErrno ()
151{
152 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000153 m_opaque_ap->SetErrorToErrno ();
Chris Lattner24943d22010-06-08 16:52:24 +0000154}
155
156void
157SBError::SetErrorToGenericError ()
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::SetErrorString (const char *err_str)
165{
166 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000167 m_opaque_ap->SetErrorString (err_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000168}
169
170int
171SBError::SetErrorStringWithFormat (const char *format, ...)
172{
173 CreateIfNeeded ();
174 va_list args;
175 va_start (args, format);
Greg Clayton63094e02010-06-23 01:19:29 +0000176 int num_chars = m_opaque_ap->SetErrorStringWithVarArg (format, args);
Chris Lattner24943d22010-06-08 16:52:24 +0000177 va_end (args);
178 return num_chars;
179}
180
181bool
182SBError::IsValid () const
183{
Greg Clayton63094e02010-06-23 01:19:29 +0000184 return m_opaque_ap.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000185}
186
187void
188SBError::CreateIfNeeded ()
189{
Greg Clayton63094e02010-06-23 01:19:29 +0000190 if (m_opaque_ap.get() == NULL)
191 m_opaque_ap.reset(new Error ());
Chris Lattner24943d22010-06-08 16:52:24 +0000192}
193
194
195lldb_private::Error *
196SBError::operator->()
197{
Greg Clayton63094e02010-06-23 01:19:29 +0000198 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000199}
200
201lldb_private::Error *
202SBError::get()
203{
Greg Clayton63094e02010-06-23 01:19:29 +0000204 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000205}
206
207
208const lldb_private::Error &
209SBError::operator*() const
210{
211 // Be sure to call "IsValid()" before calling this function or it will crash
Greg Clayton63094e02010-06-23 01:19:29 +0000212 return *m_opaque_ap;
Chris Lattner24943d22010-06-08 16:52:24 +0000213}
214
Caroline Tice98f930f2010-09-20 05:20:02 +0000215bool
216SBError::GetDescription (SBStream &description)
217{
218 if (m_opaque_ap.get())
219 {
220 if (Success())
221 description.Printf ("Status: Success");
222 else
223 {
224 const char * err_string = GetCString();
225 description.Printf ("Status: Error: %s", (err_string != NULL ? err_string : ""));
226 }
227 }
228 else
229 description.Printf ("No value");
230
231 return true;
232}
Caroline Tice7826c882010-10-26 03:11:13 +0000233
234bool
235SBError::GetDescription (SBStream &description) const
236{
237 if (m_opaque_ap.get())
238 {
239 if (Success())
240 description.Printf ("Status: Success");
241 else
242 {
243 const char * err_string = GetCString();
244 description.Printf ("Status: Error: %s", (err_string != NULL ? err_string : ""));
245 }
246 }
247 else
248 description.Printf ("No value");
249
250 return true;
251}