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