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\"", m_opaque_ap.get(),
79 m_opaque_ap->GetOutputData());
81 return m_opaque_ap->GetOutputData();
85 log->Printf ("SBCommandReturnObject(%p)::GetOutput () => NULL", m_opaque_ap.get());
91 SBCommandReturnObject::GetError ()
93 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
95 if (m_opaque_ap.get())
98 log->Printf ("SBCommandReturnObject(%p)::GetError () => \"%s\"", m_opaque_ap.get(),
99 m_opaque_ap->GetErrorData());
101 return m_opaque_ap->GetErrorData();
105 log->Printf ("SBCommandReturnObject(%p)::GetError () => NULL", m_opaque_ap.get());
111 SBCommandReturnObject::GetOutputSize ()
113 if (m_opaque_ap.get())
114 return strlen (m_opaque_ap->GetOutputData());
119 SBCommandReturnObject::GetErrorSize ()
121 if (m_opaque_ap.get())
122 return strlen(m_opaque_ap->GetErrorData());
127 SBCommandReturnObject::PutOutput (FILE *fh)
131 size_t num_bytes = GetOutputSize ();
133 return ::fprintf (fh, "%s", GetOutput());
139 SBCommandReturnObject::PutError (FILE *fh)
143 size_t num_bytes = GetErrorSize ();
145 return ::fprintf (fh, "%s", GetError());
151 SBCommandReturnObject::Clear()
153 if (m_opaque_ap.get())
154 m_opaque_ap->Clear();
158 SBCommandReturnObject::GetStatus()
160 if (m_opaque_ap.get())
161 return m_opaque_ap->GetStatus();
162 return lldb::eReturnStatusInvalid;
166 SBCommandReturnObject::SetStatus(lldb::ReturnStatus status)
168 if (m_opaque_ap.get())
169 m_opaque_ap->SetStatus(status);
173 SBCommandReturnObject::Succeeded ()
175 if (m_opaque_ap.get())
176 return m_opaque_ap->Succeeded();
181 SBCommandReturnObject::HasResult ()
183 if (m_opaque_ap.get())
184 return m_opaque_ap->HasResult();
189 SBCommandReturnObject::AppendMessage (const char *message)
191 if (m_opaque_ap.get())
192 m_opaque_ap->AppendMessage (message);
196 SBCommandReturnObject::AppendWarning (const char *message)
198 if (m_opaque_ap.get())
199 m_opaque_ap->AppendWarning (message);
202 CommandReturnObject *
203 SBCommandReturnObject::operator ->() const
205 return m_opaque_ap.get();
208 CommandReturnObject *
209 SBCommandReturnObject::get() const
211 return m_opaque_ap.get();
214 CommandReturnObject &
215 SBCommandReturnObject::operator *() const
217 assert(m_opaque_ap.get());
218 return *(m_opaque_ap.get());
222 CommandReturnObject &
223 SBCommandReturnObject::ref() const
225 assert(m_opaque_ap.get());
226 return *(m_opaque_ap.get());
231 SBCommandReturnObject::SetLLDBObjectPtr (CommandReturnObject *ptr)
233 if (m_opaque_ap.get())
234 m_opaque_ap.reset (ptr);
238 SBCommandReturnObject::GetDescription (SBStream &description)
240 Stream &strm = description.ref();
242 if (m_opaque_ap.get())
244 description.Printf ("Status: ");
245 lldb::ReturnStatus status = m_opaque_ap->GetStatus();
246 if (status == lldb::eReturnStatusStarted)
247 strm.PutCString ("Started");
248 else if (status == lldb::eReturnStatusInvalid)
249 strm.PutCString ("Invalid");
250 else if (m_opaque_ap->Succeeded())
251 strm.PutCString ("Success");
253 strm.PutCString ("Fail");
255 if (GetOutputSize() > 0)
256 strm.Printf ("\nOutput Message:\n%s", GetOutput());
258 if (GetErrorSize() > 0)
259 strm.Printf ("\nError Message:\n%s", GetError());
262 strm.PutCString ("No value");
268 SBCommandReturnObject::SetImmediateOutputFile (FILE *fh)
270 if (m_opaque_ap.get())
271 m_opaque_ap->SetImmediateOutputFile (fh);
275 SBCommandReturnObject::SetImmediateErrorFile (FILE *fh)
277 if (m_opaque_ap.get())
278 m_opaque_ap->SetImmediateErrorFile (fh);
282 SBCommandReturnObject::PutCString(const char* string, int len)
284 if (m_opaque_ap.get())
286 if (len == 0 || string == NULL || *string == 0)
292 std::string buffer(string, len);
293 m_opaque_ap->AppendMessage(buffer.c_str());
296 m_opaque_ap->AppendMessage(string);
301 SBCommandReturnObject::GetOutput (bool only_if_no_immediate)
303 if (!m_opaque_ap.get())
305 if (only_if_no_immediate == false || m_opaque_ap->GetImmediateOutputStream().get() == NULL)
311 SBCommandReturnObject::GetError (bool only_if_no_immediate)
313 if (!m_opaque_ap.get())
315 if (only_if_no_immediate == false || m_opaque_ap->GetImmediateErrorStream().get() == NULL)
321 SBCommandReturnObject::Printf(const char* format, ...)
323 if (m_opaque_ap.get())
326 va_start (args, format);
327 size_t result = m_opaque_ap->GetOutputStream().PrintfVarArg(format, args);
335 SBCommandReturnObject::SetError (lldb::SBError &error, const char *fallback_error_cstr)
337 if (m_opaque_ap.get())
340 m_opaque_ap->SetError(error.ref(), fallback_error_cstr);
341 else if (fallback_error_cstr)
342 m_opaque_ap->SetError(Error(), fallback_error_cstr);
347 SBCommandReturnObject::SetError (const char *error_cstr)
349 if (m_opaque_ap.get() && error_cstr)
350 m_opaque_ap->SetError(error_cstr);