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