]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/API/SBStructuredData.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / API / SBStructuredData.cpp
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
27 SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {}
28
29 SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
30     : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {}
31
32 SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
33     : m_impl_up(new StructuredDataImpl(event_sp)) {}
34
35 SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl)
36     : m_impl_up(impl) {}
37
38 SBStructuredData::~SBStructuredData() {}
39
40 SBStructuredData &SBStructuredData::
41 operator=(const lldb::SBStructuredData &rhs) {
42   *m_impl_up = *rhs.m_impl_up;
43   return *this;
44 }
45
46 lldb::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
58 bool SBStructuredData::IsValid() const { return m_impl_up->IsValid(); }
59
60 void SBStructuredData::Clear() { m_impl_up->Clear(); }
61
62 SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
63   SBError error;
64   error.SetError(m_impl_up->GetAsJSON(stream.ref()));
65   return error;
66 }
67
68 lldb::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
75 StructuredDataType SBStructuredData::GetType() const {
76   return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid);
77 }
78
79 size_t SBStructuredData::GetSize() const {
80   return (m_impl_up ? m_impl_up->GetSize() : 0);
81 }
82
83 bool 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
110 lldb::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
119 lldb::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
128 uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
129   return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value);
130 }
131
132 double SBStructuredData::GetFloatValue(double fail_value) const {
133   return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value);
134 }
135
136 bool SBStructuredData::GetBooleanValue(bool fail_value) const {
137   return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value);
138 }
139
140 size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
141   return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0);
142 }