1 //===-- SBCommandReturnObject.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBCommandReturnObject.h"
11 #include "lldb/API/SBError.h"
12 #include "lldb/API/SBStream.h"
14 #include "lldb/Core/Error.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Interpreter/CommandReturnObject.h"
19 using namespace lldb_private;
21 SBCommandReturnObject::SBCommandReturnObject () :
22 m_opaque_ap (new CommandReturnObject ())
26 SBCommandReturnObject::SBCommandReturnObject (const SBCommandReturnObject &rhs):
29 if (rhs.m_opaque_ap.get())
30 m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
33 SBCommandReturnObject::SBCommandReturnObject (CommandReturnObject *ptr) :
39 SBCommandReturnObject::Release ()
41 return m_opaque_ap.release();
44 const SBCommandReturnObject &
45 SBCommandReturnObject::operator = (const SBCommandReturnObject &rhs)
49 if (rhs.m_opaque_ap.get())
50 m_opaque_ap.reset (new CommandReturnObject (*rhs.m_opaque_ap));
58 SBCommandReturnObject::~SBCommandReturnObject ()
60 // m_opaque_ap will automatically delete any pointer it owns
64 SBCommandReturnObject::IsValid() const
66 return m_opaque_ap.get() != NULL;
71 SBCommandReturnObject::GetOutput ()
73 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
75 if (m_opaque_ap.get())
78 log->Printf ("SBCommandReturnObject(%p)::GetOutput () => \"%s\"",
79 static_cast<void*>(m_opaque_ap.get()),
80 m_opaque_ap->GetOutputData());
82 return m_opaque_ap->GetOutputData();
86 log->Printf ("SBCommandReturnObject(%p)::GetOutput () => NULL",
87 static_cast<void*>(m_opaque_ap.get()));
93 SBCommandReturnObject::GetError ()
95 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
97 if (m_opaque_ap.get())
100 log->Printf ("SBCommandReturnObject(%p)::GetError () => \"%s\"",
101 static_cast<void*>(m_opaque_ap.get()),
102 m_opaque_ap->GetErrorData());
104 return m_opaque_ap->GetErrorData();
108 log->Printf ("SBCommandReturnObject(%p)::GetError () => NULL",
109 static_cast<void*>(m_opaque_ap.get()));
115 SBCommandReturnObject::GetOutputSize ()
117 if (m_opaque_ap.get())
118 return strlen (m_opaque_ap->GetOutputData());
123 SBCommandReturnObject::GetErrorSize ()
125 if (m_opaque_ap.get())
126 return strlen(m_opaque_ap->GetErrorData());
131 SBCommandReturnObject::PutOutput (FILE *fh)
135 size_t num_bytes = GetOutputSize ();
137 return ::fprintf (fh, "%s", GetOutput());
143 SBCommandReturnObject::PutError (FILE *fh)
147 size_t num_bytes = GetErrorSize ();
149 return ::fprintf (fh, "%s", GetError());
155 SBCommandReturnObject::Clear()
157 if (m_opaque_ap.get())
158 m_opaque_ap->Clear();
162 SBCommandReturnObject::GetStatus()
164 if (m_opaque_ap.get())
165 return m_opaque_ap->GetStatus();
166 return lldb::eReturnStatusInvalid;
170 SBCommandReturnObject::SetStatus(lldb::ReturnStatus status)
172 if (m_opaque_ap.get())
173 m_opaque_ap->SetStatus(status);
177 SBCommandReturnObject::Succeeded ()
179 if (m_opaque_ap.get())
180 return m_opaque_ap->Succeeded();
185 SBCommandReturnObject::HasResult ()
187 if (m_opaque_ap.get())
188 return m_opaque_ap->HasResult();
193 SBCommandReturnObject::AppendMessage (const char *message)
195 if (m_opaque_ap.get())
196 m_opaque_ap->AppendMessage (message);
200 SBCommandReturnObject::AppendWarning (const char *message)
202 if (m_opaque_ap.get())
203 m_opaque_ap->AppendWarning (message);
206 CommandReturnObject *
207 SBCommandReturnObject::operator ->() const
209 return m_opaque_ap.get();
212 CommandReturnObject *
213 SBCommandReturnObject::get() const
215 return m_opaque_ap.get();
218 CommandReturnObject &
219 SBCommandReturnObject::operator *() const
221 assert(m_opaque_ap.get());
222 return *(m_opaque_ap.get());
226 CommandReturnObject &
227 SBCommandReturnObject::ref() const
229 assert(m_opaque_ap.get());
230 return *(m_opaque_ap.get());
235 SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
237 if (m_opaque_ap.get())
238 m_opaque_ap.reset (ptr);
242 SBCommandReturnObject::GetDescription (SBStream &description)
244 Stream &strm = description.ref();
246 if (m_opaque_ap.get())
248 description.Printf ("Status: ");
249 lldb::ReturnStatus status = m_opaque_ap->GetStatus();
250 if (status == lldb::eReturnStatusStarted)
251 strm.PutCString ("Started");
252 else if (status == lldb::eReturnStatusInvalid)
253 strm.PutCString ("Invalid");
254 else if (m_opaque_ap->Succeeded())
255 strm.PutCString ("Success");
257 strm.PutCString ("Fail");
259 if (GetOutputSize() > 0)
260 strm.Printf ("\nOutput Message:\n%s", GetOutput());
262 if (GetErrorSize() > 0)
263 strm.Printf ("\nError Message:\n%s", GetError());
266 strm.PutCString ("No value");
272 SBCommandReturnObject::SetImmediateOutputFile (FILE *fh)
274 if (m_opaque_ap.get())
275 m_opaque_ap->SetImmediateOutputFile (fh);
279 SBCommandReturnObject::SetImmediateErrorFile (FILE *fh)
281 if (m_opaque_ap.get())
282 m_opaque_ap->SetImmediateErrorFile (fh);
286 SBCommandReturnObject::PutCString(const char* string, int len)
288 if (m_opaque_ap.get())
290 if (len == 0 || string == NULL || *string == 0)
296 std::string buffer(string, len);
297 m_opaque_ap->AppendMessage(buffer.c_str());
300 m_opaque_ap->AppendMessage(string);
305 SBCommandReturnObject::GetOutput (bool only_if_no_immediate)
307 if (!m_opaque_ap.get())
309 if (only_if_no_immediate == false || m_opaque_ap->GetImmediateOutputStream().get() == NULL)
315 SBCommandReturnObject::GetError (bool only_if_no_immediate)
317 if (!m_opaque_ap.get())
319 if (only_if_no_immediate == false || m_opaque_ap->GetImmediateErrorStream().get() == NULL)
325 SBCommandReturnObject::Printf(const char* format, ...)
327 if (m_opaque_ap.get())
330 va_start (args, format);
331 size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
339 SBCommandReturnObject::SetError (lldb::SBError &error, const char *fallback_error_cstr)
341 if (m_opaque_ap.get())
344 m_opaque_ap->SetError(error.ref(), fallback_error_cstr);
345 else if (fallback_error_cstr)
346 m_opaque_ap->SetError(Error(), fallback_error_cstr);
351 SBCommandReturnObject::SetError (const char *error_cstr)
353 if (m_opaque_ap.get() && error_cstr)
354 m_opaque_ap->SetError(error_cstr);