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 
14 #include <stdarg.h>
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 
20 SBError::SBError () :
21     m_opaque_ap ()
22 {
23 }
24 
25 SBError::SBError (const SBError &rhs) :
26     m_opaque_ap ()
27 {
28     if (rhs.IsValid())
29         m_opaque_ap.reset (new Error(*rhs));
30 }
31 
32 
33 SBError::~SBError()
34 {
35 }
36 
37 const SBError &
38 SBError::operator = (const SBError &rhs)
39 {
40     if (rhs.IsValid())
41     {
42         if (m_opaque_ap.get())
43             *m_opaque_ap = *rhs;
44         else
45             m_opaque_ap.reset (new Error(*rhs));
46     }
47     else
48     {
49         m_opaque_ap.reset();
50     }
51     return *this;
52 }
53 
54 
55 const char *
56 SBError::GetCString () const
57 {
58     if (m_opaque_ap.get())
59         return m_opaque_ap->AsCString();
60     return NULL;
61 }
62 
63 void
64 SBError::Clear ()
65 {
66     if (m_opaque_ap.get())
67         m_opaque_ap->Clear();
68 }
69 
70 bool
71 SBError::Fail () const
72 {
73     if (m_opaque_ap.get())
74         return m_opaque_ap->Fail();
75     return false;
76 }
77 
78 bool
79 SBError::Success () const
80 {
81     if (m_opaque_ap.get())
82         return m_opaque_ap->Success();
83     return false;
84 }
85 
86 uint32_t
87 SBError::GetError () const
88 {
89     if (m_opaque_ap.get())
90         return m_opaque_ap->GetError();
91     return true;
92 }
93 
94 ErrorType
95 SBError::GetType () const
96 {
97     if (m_opaque_ap.get())
98         return m_opaque_ap->GetType();
99     return eErrorTypeInvalid;
100 }
101 
102 void
103 SBError::SetError (uint32_t err, ErrorType type)
104 {
105     CreateIfNeeded ();
106     m_opaque_ap->SetError (err, type);
107 }
108 
109 void
110 SBError::SetError (const Error &lldb_error)
111 {
112     CreateIfNeeded ();
113     *m_opaque_ap = lldb_error;
114 }
115 
116 
117 void
118 SBError::SetErrorToErrno ()
119 {
120     CreateIfNeeded ();
121     m_opaque_ap->SetErrorToErrno ();
122 }
123 
124 void
125 SBError::SetErrorToGenericError ()
126 {
127     CreateIfNeeded ();
128     m_opaque_ap->SetErrorToErrno ();
129 }
130 
131 void
132 SBError::SetErrorString (const char *err_str)
133 {
134     CreateIfNeeded ();
135     m_opaque_ap->SetErrorString (err_str);
136 }
137 
138 int
139 SBError::SetErrorStringWithFormat (const char *format, ...)
140 {
141     CreateIfNeeded ();
142     va_list args;
143     va_start (args, format);
144     int num_chars = m_opaque_ap->SetErrorStringWithVarArg (format, args);
145     va_end (args);
146     return num_chars;
147 }
148 
149 bool
150 SBError::IsValid () const
151 {
152     return m_opaque_ap.get() != NULL;
153 }
154 
155 void
156 SBError::CreateIfNeeded ()
157 {
158     if (m_opaque_ap.get() == NULL)
159         m_opaque_ap.reset(new Error ());
160 }
161 
162 
163 lldb_private::Error *
164 SBError::operator->()
165 {
166     return m_opaque_ap.get();
167 }
168 
169 lldb_private::Error *
170 SBError::get()
171 {
172     return m_opaque_ap.get();
173 }
174 
175 
176 const lldb_private::Error &
177 SBError::operator*() const
178 {
179     // Be sure to call "IsValid()" before calling this function or it will crash
180     return *m_opaque_ap;
181 }
182 
183 bool
184 SBError::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