1ac7ddfbfSEd Maste //===-- SBError.cpp ---------------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/API/SBError.h"
11ac7ddfbfSEd Maste #include "lldb/API/SBStream.h"
12f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
135517e702SDimitry Andric #include "lldb/Utility/Status.h"
14ac7ddfbfSEd Maste 
15ac7ddfbfSEd Maste #include <stdarg.h>
16ac7ddfbfSEd Maste 
17ac7ddfbfSEd Maste using namespace lldb;
18ac7ddfbfSEd Maste using namespace lldb_private;
19ac7ddfbfSEd Maste 
SBError()20435933ddSDimitry Andric SBError::SBError() : m_opaque_ap() {}
21ac7ddfbfSEd Maste 
SBError(const SBError & rhs)22435933ddSDimitry Andric SBError::SBError(const SBError &rhs) : m_opaque_ap() {
23ac7ddfbfSEd Maste   if (rhs.IsValid())
245517e702SDimitry Andric     m_opaque_ap.reset(new Status(*rhs));
25ac7ddfbfSEd Maste }
26ac7ddfbfSEd Maste 
~SBError()27435933ddSDimitry Andric SBError::~SBError() {}
28ac7ddfbfSEd Maste 
operator =(const SBError & rhs)29435933ddSDimitry Andric const SBError &SBError::operator=(const SBError &rhs) {
30435933ddSDimitry Andric   if (rhs.IsValid()) {
31*b5893f02SDimitry Andric     if (m_opaque_ap)
32ac7ddfbfSEd Maste       *m_opaque_ap = *rhs;
33ac7ddfbfSEd Maste     else
345517e702SDimitry Andric       m_opaque_ap.reset(new Status(*rhs));
35435933ddSDimitry Andric   } else
36ac7ddfbfSEd Maste     m_opaque_ap.reset();
37ac7ddfbfSEd Maste 
38ac7ddfbfSEd Maste   return *this;
39ac7ddfbfSEd Maste }
40ac7ddfbfSEd Maste 
GetCString() const41435933ddSDimitry Andric const char *SBError::GetCString() const {
42*b5893f02SDimitry Andric   if (m_opaque_ap)
43ac7ddfbfSEd Maste     return m_opaque_ap->AsCString();
44ac7ddfbfSEd Maste   return NULL;
45ac7ddfbfSEd Maste }
46ac7ddfbfSEd Maste 
Clear()47435933ddSDimitry Andric void SBError::Clear() {
48*b5893f02SDimitry Andric   if (m_opaque_ap)
49ac7ddfbfSEd Maste     m_opaque_ap->Clear();
50ac7ddfbfSEd Maste }
51ac7ddfbfSEd Maste 
Fail() const52435933ddSDimitry Andric bool SBError::Fail() const {
53ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
54ac7ddfbfSEd Maste 
55ac7ddfbfSEd Maste   bool ret_value = false;
56*b5893f02SDimitry Andric   if (m_opaque_ap)
57ac7ddfbfSEd Maste     ret_value = m_opaque_ap->Fail();
58ac7ddfbfSEd Maste 
59ac7ddfbfSEd Maste   if (log)
600127ef0fSEd Maste     log->Printf("SBError(%p)::Fail () => %i",
610127ef0fSEd Maste                 static_cast<void *>(m_opaque_ap.get()), ret_value);
62ac7ddfbfSEd Maste 
63ac7ddfbfSEd Maste   return ret_value;
64ac7ddfbfSEd Maste }
65ac7ddfbfSEd Maste 
Success() const66435933ddSDimitry Andric bool SBError::Success() const {
67ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
68ac7ddfbfSEd Maste   bool ret_value = true;
69*b5893f02SDimitry Andric   if (m_opaque_ap)
70ac7ddfbfSEd Maste     ret_value = m_opaque_ap->Success();
71ac7ddfbfSEd Maste 
72ac7ddfbfSEd Maste   if (log)
730127ef0fSEd Maste     log->Printf("SBError(%p)::Success () => %i",
740127ef0fSEd Maste                 static_cast<void *>(m_opaque_ap.get()), ret_value);
75ac7ddfbfSEd Maste 
76ac7ddfbfSEd Maste   return ret_value;
77ac7ddfbfSEd Maste }
78ac7ddfbfSEd Maste 
GetError() const79435933ddSDimitry Andric uint32_t SBError::GetError() const {
80ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
81ac7ddfbfSEd Maste 
82ac7ddfbfSEd Maste   uint32_t err = 0;
83*b5893f02SDimitry Andric   if (m_opaque_ap)
84ac7ddfbfSEd Maste     err = m_opaque_ap->GetError();
85ac7ddfbfSEd Maste 
86ac7ddfbfSEd Maste   if (log)
870127ef0fSEd Maste     log->Printf("SBError(%p)::GetError () => 0x%8.8x",
880127ef0fSEd Maste                 static_cast<void *>(m_opaque_ap.get()), err);
89ac7ddfbfSEd Maste 
90ac7ddfbfSEd Maste   return err;
91ac7ddfbfSEd Maste }
92ac7ddfbfSEd Maste 
GetType() const93435933ddSDimitry Andric ErrorType SBError::GetType() const {
94ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
95ac7ddfbfSEd Maste   ErrorType err_type = eErrorTypeInvalid;
96*b5893f02SDimitry Andric   if (m_opaque_ap)
97ac7ddfbfSEd Maste     err_type = m_opaque_ap->GetType();
98ac7ddfbfSEd Maste 
99ac7ddfbfSEd Maste   if (log)
1000127ef0fSEd Maste     log->Printf("SBError(%p)::GetType () => %i",
1010127ef0fSEd Maste                 static_cast<void *>(m_opaque_ap.get()), err_type);
102ac7ddfbfSEd Maste 
103ac7ddfbfSEd Maste   return err_type;
104ac7ddfbfSEd Maste }
105ac7ddfbfSEd Maste 
SetError(uint32_t err,ErrorType type)106435933ddSDimitry Andric void SBError::SetError(uint32_t err, ErrorType type) {
107ac7ddfbfSEd Maste   CreateIfNeeded();
108ac7ddfbfSEd Maste   m_opaque_ap->SetError(err, type);
109ac7ddfbfSEd Maste }
110ac7ddfbfSEd Maste 
SetError(const Status & lldb_error)1115517e702SDimitry Andric void SBError::SetError(const Status &lldb_error) {
112ac7ddfbfSEd Maste   CreateIfNeeded();
113ac7ddfbfSEd Maste   *m_opaque_ap = lldb_error;
114ac7ddfbfSEd Maste }
115ac7ddfbfSEd Maste 
SetErrorToErrno()116435933ddSDimitry Andric void SBError::SetErrorToErrno() {
117ac7ddfbfSEd Maste   CreateIfNeeded();
118ac7ddfbfSEd Maste   m_opaque_ap->SetErrorToErrno();
119ac7ddfbfSEd Maste }
120ac7ddfbfSEd Maste 
SetErrorToGenericError()121435933ddSDimitry Andric void SBError::SetErrorToGenericError() {
122ac7ddfbfSEd Maste   CreateIfNeeded();
123ac7ddfbfSEd Maste   m_opaque_ap->SetErrorToErrno();
124ac7ddfbfSEd Maste }
125ac7ddfbfSEd Maste 
SetErrorString(const char * err_str)126435933ddSDimitry Andric void SBError::SetErrorString(const char *err_str) {
127ac7ddfbfSEd Maste   CreateIfNeeded();
128ac7ddfbfSEd Maste   m_opaque_ap->SetErrorString(err_str);
129ac7ddfbfSEd Maste }
130ac7ddfbfSEd Maste 
SetErrorStringWithFormat(const char * format,...)131435933ddSDimitry Andric int SBError::SetErrorStringWithFormat(const char *format, ...) {
132ac7ddfbfSEd Maste   CreateIfNeeded();
133ac7ddfbfSEd Maste   va_list args;
134ac7ddfbfSEd Maste   va_start(args, format);
135ac7ddfbfSEd Maste   int num_chars = m_opaque_ap->SetErrorStringWithVarArg(format, args);
136ac7ddfbfSEd Maste   va_end(args);
137ac7ddfbfSEd Maste   return num_chars;
138ac7ddfbfSEd Maste }
139ac7ddfbfSEd Maste 
IsValid() const140*b5893f02SDimitry Andric bool SBError::IsValid() const { return m_opaque_ap != NULL; }
141ac7ddfbfSEd Maste 
CreateIfNeeded()142435933ddSDimitry Andric void SBError::CreateIfNeeded() {
143*b5893f02SDimitry Andric   if (m_opaque_ap == NULL)
1445517e702SDimitry Andric     m_opaque_ap.reset(new Status());
145ac7ddfbfSEd Maste }
146ac7ddfbfSEd Maste 
operator ->()1475517e702SDimitry Andric lldb_private::Status *SBError::operator->() { return m_opaque_ap.get(); }
148ac7ddfbfSEd Maste 
get()1495517e702SDimitry Andric lldb_private::Status *SBError::get() { return m_opaque_ap.get(); }
150ac7ddfbfSEd Maste 
ref()1515517e702SDimitry Andric lldb_private::Status &SBError::ref() {
152ac7ddfbfSEd Maste   CreateIfNeeded();
153ac7ddfbfSEd Maste   return *m_opaque_ap;
154ac7ddfbfSEd Maste }
155ac7ddfbfSEd Maste 
operator *() const1565517e702SDimitry Andric const lldb_private::Status &SBError::operator*() const {
157ac7ddfbfSEd Maste   // Be sure to call "IsValid()" before calling this function or it will crash
158ac7ddfbfSEd Maste   return *m_opaque_ap;
159ac7ddfbfSEd Maste }
160ac7ddfbfSEd Maste 
GetDescription(SBStream & description)161435933ddSDimitry Andric bool SBError::GetDescription(SBStream &description) {
162*b5893f02SDimitry Andric   if (m_opaque_ap) {
163ac7ddfbfSEd Maste     if (m_opaque_ap->Success())
164ac7ddfbfSEd Maste       description.Printf("success");
165435933ddSDimitry Andric     else {
166ac7ddfbfSEd Maste       const char *err_string = GetCString();
167ac7ddfbfSEd Maste       description.Printf("error: %s", (err_string != NULL ? err_string : ""));
168ac7ddfbfSEd Maste     }
169435933ddSDimitry Andric   } else
170ac7ddfbfSEd Maste     description.Printf("error: <NULL>");
171ac7ddfbfSEd Maste 
172ac7ddfbfSEd Maste   return true;
173ac7ddfbfSEd Maste }
174