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
17using namespace lldb;
18using namespace lldb_private;
19
20
21SBError::SBError () :
22    m_opaque_ap ()
23{
24}
25
26SBError::SBError (const SBError &rhs) :
27    m_opaque_ap ()
28{
29    if (rhs.IsValid())
30        m_opaque_ap.reset (new Error(*rhs));
31}
32
33
34SBError::~SBError()
35{
36}
37
38const SBError &
39SBError::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
55const char *
56SBError::GetCString () const
57{
58    if (m_opaque_ap.get())
59        return m_opaque_ap->AsCString();
60    return NULL;
61}
62
63void
64SBError::Clear ()
65{
66    if (m_opaque_ap.get())
67        m_opaque_ap->Clear();
68}
69
70bool
71SBError::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
85bool
86SBError::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
99uint32_t
100SBError::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
115ErrorType
116SBError::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
129void
130SBError::SetError (uint32_t err, ErrorType type)
131{
132    CreateIfNeeded ();
133    m_opaque_ap->SetError (err, type);
134}
135
136void
137SBError::SetError (const Error &lldb_error)
138{
139    CreateIfNeeded ();
140    *m_opaque_ap = lldb_error;
141}
142
143
144void
145SBError::SetErrorToErrno ()
146{
147    CreateIfNeeded ();
148    m_opaque_ap->SetErrorToErrno ();
149}
150
151void
152SBError::SetErrorToGenericError ()
153{
154    CreateIfNeeded ();
155    m_opaque_ap->SetErrorToErrno ();
156}
157
158void
159SBError::SetErrorString (const char *err_str)
160{
161    CreateIfNeeded ();
162    m_opaque_ap->SetErrorString (err_str);
163}
164
165int
166SBError::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
176bool
177SBError::IsValid () const
178{
179    return m_opaque_ap.get() != NULL;
180}
181
182void
183SBError::CreateIfNeeded ()
184{
185    if (m_opaque_ap.get() == NULL)
186        m_opaque_ap.reset(new Error ());
187}
188
189
190lldb_private::Error *
191SBError::operator->()
192{
193    return m_opaque_ap.get();
194}
195
196lldb_private::Error *
197SBError::get()
198{
199    return m_opaque_ap.get();
200}
201
202lldb_private::Error &
203SBError::ref()
204{
205    CreateIfNeeded();
206    return *m_opaque_ap;
207}
208
209const lldb_private::Error &
210SBError::operator*() const
211{
212    // Be sure to call "IsValid()" before calling this function or it will crash
213    return *m_opaque_ap;
214}
215
216bool
217SBError::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