1435933ddSDimitry Andric //===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===// 2435933ddSDimitry Andric // 3435933ddSDimitry Andric // The LLVM Compiler Infrastructure 4435933ddSDimitry Andric // 5435933ddSDimitry Andric // This file is distributed under the University of Illinois Open Source 6435933ddSDimitry Andric // License. See LICENSE.TXT for details. 7435933ddSDimitry Andric // 8435933ddSDimitry Andric //===----------------------------------------------------------------------===// 9435933ddSDimitry Andric 10435933ddSDimitry Andric #include "lldb/API/SBStructuredData.h" 11435933ddSDimitry Andric 12435933ddSDimitry Andric #include "lldb/API/SBStream.h" 13*b5893f02SDimitry Andric #include "lldb/API/SBStringList.h" 1451690af2SDimitry Andric #include "lldb/Core/StructuredDataImpl.h" 15435933ddSDimitry Andric #include "lldb/Target/StructuredDataPlugin.h" 16*b5893f02SDimitry Andric #include "lldb/Utility/Event.h" 175517e702SDimitry Andric #include "lldb/Utility/Status.h" 18f678e45dSDimitry Andric #include "lldb/Utility/Stream.h" 19a580b014SDimitry Andric #include "lldb/Utility/StructuredData.h" 20435933ddSDimitry Andric 21435933ddSDimitry Andric using namespace lldb; 22435933ddSDimitry Andric using namespace lldb_private; 23435933ddSDimitry Andric 24435933ddSDimitry Andric #pragma mark-- 25435933ddSDimitry Andric #pragma mark SBStructuredData 26435933ddSDimitry Andric SBStructuredData()27435933ddSDimitry AndricSBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {} 28435933ddSDimitry Andric SBStructuredData(const lldb::SBStructuredData & rhs)29435933ddSDimitry AndricSBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs) 30435933ddSDimitry Andric : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {} 31435933ddSDimitry Andric SBStructuredData(const lldb::EventSP & event_sp)32435933ddSDimitry AndricSBStructuredData::SBStructuredData(const lldb::EventSP &event_sp) 33435933ddSDimitry Andric : m_impl_up(new StructuredDataImpl(event_sp)) {} 34435933ddSDimitry Andric SBStructuredData(lldb_private::StructuredDataImpl * impl)35*b5893f02SDimitry AndricSBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl) 36*b5893f02SDimitry Andric : m_impl_up(impl) {} 37*b5893f02SDimitry Andric ~SBStructuredData()38435933ddSDimitry AndricSBStructuredData::~SBStructuredData() {} 39435933ddSDimitry Andric 40435933ddSDimitry Andric SBStructuredData &SBStructuredData:: operator =(const lldb::SBStructuredData & rhs)41435933ddSDimitry Andricoperator=(const lldb::SBStructuredData &rhs) { 42435933ddSDimitry Andric *m_impl_up = *rhs.m_impl_up; 43435933ddSDimitry Andric return *this; 44435933ddSDimitry Andric } 45435933ddSDimitry Andric SetFromJSON(lldb::SBStream & stream)4651690af2SDimitry Andriclldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) { 4751690af2SDimitry Andric lldb::SBError error; 4851690af2SDimitry Andric std::string json_str(stream.GetData()); 4951690af2SDimitry Andric 5051690af2SDimitry Andric StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str); 5151690af2SDimitry Andric m_impl_up->SetObjectSP(json_obj); 5251690af2SDimitry Andric 53302affcbSDimitry Andric if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary) 5451690af2SDimitry Andric error.SetErrorString("Invalid Syntax"); 5551690af2SDimitry Andric return error; 5651690af2SDimitry Andric } 5751690af2SDimitry Andric IsValid() const58435933ddSDimitry Andricbool SBStructuredData::IsValid() const { return m_impl_up->IsValid(); } 59435933ddSDimitry Andric Clear()60435933ddSDimitry Andricvoid SBStructuredData::Clear() { m_impl_up->Clear(); } 61435933ddSDimitry Andric GetAsJSON(lldb::SBStream & stream) const62435933ddSDimitry AndricSBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const { 6351690af2SDimitry Andric SBError error; 6451690af2SDimitry Andric error.SetError(m_impl_up->GetAsJSON(stream.ref())); 6551690af2SDimitry Andric return error; 66435933ddSDimitry Andric } 67435933ddSDimitry Andric GetDescription(lldb::SBStream & stream) const68435933ddSDimitry Andriclldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const { 695517e702SDimitry Andric Status error = m_impl_up->GetDescription(stream.ref()); 70435933ddSDimitry Andric SBError sb_error; 71435933ddSDimitry Andric sb_error.SetError(error); 72435933ddSDimitry Andric return sb_error; 73435933ddSDimitry Andric } 74302affcbSDimitry Andric GetType() const75302affcbSDimitry AndricStructuredDataType SBStructuredData::GetType() const { 76302affcbSDimitry Andric return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid); 77302affcbSDimitry Andric } 78302affcbSDimitry Andric GetSize() const79302affcbSDimitry Andricsize_t SBStructuredData::GetSize() const { 80302affcbSDimitry Andric return (m_impl_up ? m_impl_up->GetSize() : 0); 81302affcbSDimitry Andric } 82302affcbSDimitry Andric GetKeys(lldb::SBStringList & keys) const83*b5893f02SDimitry Andricbool SBStructuredData::GetKeys(lldb::SBStringList &keys) const { 84*b5893f02SDimitry Andric if (!m_impl_up) 85*b5893f02SDimitry Andric return false; 86*b5893f02SDimitry Andric 87*b5893f02SDimitry Andric if (GetType() != eStructuredDataTypeDictionary) 88*b5893f02SDimitry Andric return false; 89*b5893f02SDimitry Andric 90*b5893f02SDimitry Andric StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP(); 91*b5893f02SDimitry Andric if (!obj_sp) 92*b5893f02SDimitry Andric return false; 93*b5893f02SDimitry Andric 94*b5893f02SDimitry Andric StructuredData::Dictionary *dict = obj_sp->GetAsDictionary(); 95*b5893f02SDimitry Andric // We claimed we were a dictionary, so this can't be null. 96*b5893f02SDimitry Andric assert(dict); 97*b5893f02SDimitry Andric // The return kind of GetKeys is an Array: 98*b5893f02SDimitry Andric StructuredData::ObjectSP array_sp = dict->GetKeys(); 99*b5893f02SDimitry Andric StructuredData::Array *key_arr = array_sp->GetAsArray(); 100*b5893f02SDimitry Andric assert(key_arr); 101*b5893f02SDimitry Andric 102*b5893f02SDimitry Andric key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool { 103*b5893f02SDimitry Andric llvm::StringRef key = object->GetStringValue(""); 104*b5893f02SDimitry Andric keys.AppendString(key.str().c_str()); 105*b5893f02SDimitry Andric return true; 106*b5893f02SDimitry Andric }); 107*b5893f02SDimitry Andric return true; 108*b5893f02SDimitry Andric } 109*b5893f02SDimitry Andric GetValueForKey(const char * key) const110302affcbSDimitry Andriclldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const { 111302affcbSDimitry Andric if (!m_impl_up) 112302affcbSDimitry Andric return SBStructuredData(); 113302affcbSDimitry Andric 114302affcbSDimitry Andric SBStructuredData result; 115302affcbSDimitry Andric result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key)); 116302affcbSDimitry Andric return result; 117302affcbSDimitry Andric } 118302affcbSDimitry Andric GetItemAtIndex(size_t idx) const119302affcbSDimitry Andriclldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const { 120302affcbSDimitry Andric if (!m_impl_up) 121302affcbSDimitry Andric return SBStructuredData(); 122302affcbSDimitry Andric 123302affcbSDimitry Andric SBStructuredData result; 124302affcbSDimitry Andric result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx)); 125302affcbSDimitry Andric return result; 126302affcbSDimitry Andric } 127302affcbSDimitry Andric GetIntegerValue(uint64_t fail_value) const128302affcbSDimitry Andricuint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const { 129302affcbSDimitry Andric return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value); 130302affcbSDimitry Andric } 131302affcbSDimitry Andric GetFloatValue(double fail_value) const132302affcbSDimitry Andricdouble SBStructuredData::GetFloatValue(double fail_value) const { 133302affcbSDimitry Andric return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value); 134302affcbSDimitry Andric } 135302affcbSDimitry Andric GetBooleanValue(bool fail_value) const136302affcbSDimitry Andricbool SBStructuredData::GetBooleanValue(bool fail_value) const { 137302affcbSDimitry Andric return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value); 138302affcbSDimitry Andric } 139302affcbSDimitry Andric GetStringValue(char * dst,size_t dst_len) const140302affcbSDimitry Andricsize_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const { 141302affcbSDimitry Andric return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0); 142302affcbSDimitry Andric } 143