blob: 136561981b6e1e60ab8e783c74fd7e3cea25c7f8 [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 Tice98f930f2010-09-20 05:20:02 +000013
Eli Friedman1501a962010-06-09 08:46:23 +000014#include <stdarg.h>
Chris Lattner24943d22010-06-08 16:52:24 +000015
16using namespace lldb;
17using namespace lldb_private;
18
19
20SBError::SBError () :
Greg Clayton63094e02010-06-23 01:19:29 +000021 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000022{
23}
24
25SBError::SBError (const SBError &rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +000026 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000027{
28 if (rhs.IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000029 m_opaque_ap.reset (new Error(*rhs));
Chris Lattner24943d22010-06-08 16:52:24 +000030}
31
32
33SBError::~SBError()
34{
35}
36
37const SBError &
38SBError::operator = (const SBError &rhs)
39{
40 if (rhs.IsValid())
41 {
Greg Clayton63094e02010-06-23 01:19:29 +000042 if (m_opaque_ap.get())
43 *m_opaque_ap = *rhs;
Chris Lattner24943d22010-06-08 16:52:24 +000044 else
Greg Clayton63094e02010-06-23 01:19:29 +000045 m_opaque_ap.reset (new Error(*rhs));
Chris Lattner24943d22010-06-08 16:52:24 +000046 }
47 else
48 {
Greg Clayton63094e02010-06-23 01:19:29 +000049 m_opaque_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +000050 }
51 return *this;
52}
53
54
55const char *
56SBError::GetCString () const
57{
Greg Clayton63094e02010-06-23 01:19:29 +000058 if (m_opaque_ap.get())
59 return m_opaque_ap->AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000060 return NULL;
61}
62
63void
64SBError::Clear ()
65{
Greg Clayton63094e02010-06-23 01:19:29 +000066 if (m_opaque_ap.get())
67 m_opaque_ap->Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000068}
69
70bool
71SBError::Fail () const
72{
Greg Clayton63094e02010-06-23 01:19:29 +000073 if (m_opaque_ap.get())
74 return m_opaque_ap->Fail();
Chris Lattner24943d22010-06-08 16:52:24 +000075 return false;
76}
77
78bool
79SBError::Success () const
80{
Greg Clayton63094e02010-06-23 01:19:29 +000081 if (m_opaque_ap.get())
82 return m_opaque_ap->Success();
Chris Lattner24943d22010-06-08 16:52:24 +000083 return false;
84}
85
86uint32_t
87SBError::GetError () const
88{
Greg Clayton63094e02010-06-23 01:19:29 +000089 if (m_opaque_ap.get())
90 return m_opaque_ap->GetError();
Chris Lattner24943d22010-06-08 16:52:24 +000091 return true;
92}
93
94ErrorType
95SBError::GetType () const
96{
Greg Clayton63094e02010-06-23 01:19:29 +000097 if (m_opaque_ap.get())
98 return m_opaque_ap->GetType();
Chris Lattner24943d22010-06-08 16:52:24 +000099 return eErrorTypeInvalid;
100}
101
102void
103SBError::SetError (uint32_t err, ErrorType type)
104{
105 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000106 m_opaque_ap->SetError (err, type);
Chris Lattner24943d22010-06-08 16:52:24 +0000107}
108
109void
110SBError::SetError (const Error &lldb_error)
111{
112 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000113 *m_opaque_ap = lldb_error;
Chris Lattner24943d22010-06-08 16:52:24 +0000114}
115
116
117void
118SBError::SetErrorToErrno ()
119{
120 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000121 m_opaque_ap->SetErrorToErrno ();
Chris Lattner24943d22010-06-08 16:52:24 +0000122}
123
124void
125SBError::SetErrorToGenericError ()
126{
127 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000128 m_opaque_ap->SetErrorToErrno ();
Chris Lattner24943d22010-06-08 16:52:24 +0000129}
130
131void
132SBError::SetErrorString (const char *err_str)
133{
134 CreateIfNeeded ();
Greg Clayton63094e02010-06-23 01:19:29 +0000135 m_opaque_ap->SetErrorString (err_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000136}
137
138int
139SBError::SetErrorStringWithFormat (const char *format, ...)
140{
141 CreateIfNeeded ();
142 va_list args;
143 va_start (args, format);
Greg Clayton63094e02010-06-23 01:19:29 +0000144 int num_chars = m_opaque_ap->SetErrorStringWithVarArg (format, args);
Chris Lattner24943d22010-06-08 16:52:24 +0000145 va_end (args);
146 return num_chars;
147}
148
149bool
150SBError::IsValid () const
151{
Greg Clayton63094e02010-06-23 01:19:29 +0000152 return m_opaque_ap.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000153}
154
155void
156SBError::CreateIfNeeded ()
157{
Greg Clayton63094e02010-06-23 01:19:29 +0000158 if (m_opaque_ap.get() == NULL)
159 m_opaque_ap.reset(new Error ());
Chris Lattner24943d22010-06-08 16:52:24 +0000160}
161
162
163lldb_private::Error *
164SBError::operator->()
165{
Greg Clayton63094e02010-06-23 01:19:29 +0000166 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000167}
168
169lldb_private::Error *
170SBError::get()
171{
Greg Clayton63094e02010-06-23 01:19:29 +0000172 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000173}
174
175
176const lldb_private::Error &
177SBError::operator*() const
178{
179 // Be sure to call "IsValid()" before calling this function or it will crash
Greg Clayton63094e02010-06-23 01:19:29 +0000180 return *m_opaque_ap;
Chris Lattner24943d22010-06-08 16:52:24 +0000181}
182
Caroline Tice98f930f2010-09-20 05:20:02 +0000183bool
184SBError::GetDescription (SBStream &description)
185{
186 if (m_opaque_ap.get())
187 {
188 if (Success())
189 description.Printf ("Status: Success");
190 else
191 {
192 const char * err_string = GetCString();
193 description.Printf ("Status: Error: %s", (err_string != NULL ? err_string : ""));
194 }
195 }
196 else
197 description.Printf ("No value");
198
199 return true;
200}
201
202PyObject *
203SBError::__repr__ ()
204{
205 SBStream description;
206 GetDescription (description);
207 return PyString_FromString (description.GetData());
208}