1 //===-- SBStructuredData.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/SBStructuredData.h" 11 12 #include "lldb/API/SBStream.h" 13 #include "lldb/API/SBStringList.h" 14 #include "lldb/Core/StructuredDataImpl.h" 15 #include "lldb/Target/StructuredDataPlugin.h" 16 #include "lldb/Utility/Event.h" 17 #include "lldb/Utility/Status.h" 18 #include "lldb/Utility/Stream.h" 19 #include "lldb/Utility/StructuredData.h" 20 21 using namespace lldb; 22 using namespace lldb_private; 23 24 #pragma mark-- 25 #pragma mark SBStructuredData 26 SBStructuredData()27SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {} 28 SBStructuredData(const lldb::SBStructuredData & rhs)29SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs) 30 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {} 31 SBStructuredData(const lldb::EventSP & event_sp)32SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp) 33 : m_impl_up(new StructuredDataImpl(event_sp)) {} 34 SBStructuredData(lldb_private::StructuredDataImpl * impl)35SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl) 36 : m_impl_up(impl) {} 37 ~SBStructuredData()38SBStructuredData::~SBStructuredData() {} 39 40 SBStructuredData &SBStructuredData:: operator =(const lldb::SBStructuredData & rhs)41operator=(const lldb::SBStructuredData &rhs) { 42 *m_impl_up = *rhs.m_impl_up; 43 return *this; 44 } 45 SetFromJSON(lldb::SBStream & stream)46lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) { 47 lldb::SBError error; 48 std::string json_str(stream.GetData()); 49 50 StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str); 51 m_impl_up->SetObjectSP(json_obj); 52 53 if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary) 54 error.SetErrorString("Invalid Syntax"); 55 return error; 56 } 57 IsValid() const58bool SBStructuredData::IsValid() const { return m_impl_up->IsValid(); } 59 Clear()60void SBStructuredData::Clear() { m_impl_up->Clear(); } 61 GetAsJSON(lldb::SBStream & stream) const62SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const { 63 SBError error; 64 error.SetError(m_impl_up->GetAsJSON(stream.ref())); 65 return error; 66 } 67 GetDescription(lldb::SBStream & stream) const68lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const { 69 Status error = m_impl_up->GetDescription(stream.ref()); 70 SBError sb_error; 71 sb_error.SetError(error); 72 return sb_error; 73 } 74 GetType() const75StructuredDataType SBStructuredData::GetType() const { 76 return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid); 77 } 78 GetSize() const79size_t SBStructuredData::GetSize() const { 80 return (m_impl_up ? m_impl_up->GetSize() : 0); 81 } 82 GetKeys(lldb::SBStringList & keys) const83bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const { 84 if (!m_impl_up) 85 return false; 86 87 if (GetType() != eStructuredDataTypeDictionary) 88 return false; 89 90 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP(); 91 if (!obj_sp) 92 return false; 93 94 StructuredData::Dictionary *dict = obj_sp->GetAsDictionary(); 95 // We claimed we were a dictionary, so this can't be null. 96 assert(dict); 97 // The return kind of GetKeys is an Array: 98 StructuredData::ObjectSP array_sp = dict->GetKeys(); 99 StructuredData::Array *key_arr = array_sp->GetAsArray(); 100 assert(key_arr); 101 102 key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool { 103 llvm::StringRef key = object->GetStringValue(""); 104 keys.AppendString(key.str().c_str()); 105 return true; 106 }); 107 return true; 108 } 109 GetValueForKey(const char * key) const110lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const { 111 if (!m_impl_up) 112 return SBStructuredData(); 113 114 SBStructuredData result; 115 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key)); 116 return result; 117 } 118 GetItemAtIndex(size_t idx) const119lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const { 120 if (!m_impl_up) 121 return SBStructuredData(); 122 123 SBStructuredData result; 124 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx)); 125 return result; 126 } 127 GetIntegerValue(uint64_t fail_value) const128uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const { 129 return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value); 130 } 131 GetFloatValue(double fail_value) const132double SBStructuredData::GetFloatValue(double fail_value) const { 133 return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value); 134 } 135 GetBooleanValue(bool fail_value) const136bool SBStructuredData::GetBooleanValue(bool fail_value) const { 137 return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value); 138 } 139 GetStringValue(char * dst,size_t dst_len) const140size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const { 141 return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0); 142 } 143