LLDB mainline
SBStructuredData.cpp
Go to the documentation of this file.
1//===-- SBStructuredData.cpp ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11
12#include "lldb/API/SBStream.h"
16#include "lldb/Utility/Event.h"
17#include "lldb/Utility/Status.h"
18#include "lldb/Utility/Stream.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24#pragma mark--
25#pragma mark SBStructuredData
26
29}
30
32 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
33 LLDB_INSTRUMENT_VA(this, rhs);
34}
35
36SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
37 : m_impl_up(new StructuredDataImpl(event_sp)) {
38 LLDB_INSTRUMENT_VA(this, event_sp);
39}
40
42 : m_impl_up(new StructuredDataImpl(impl)) {
43 LLDB_INSTRUMENT_VA(this, impl);
44}
45
47
50 LLDB_INSTRUMENT_VA(this, rhs);
51
52 *m_impl_up = *rhs.m_impl_up;
53 return *this;
54}
55
57 LLDB_INSTRUMENT_VA(this, stream);
58
60 std::string json_str(stream.GetData());
61
63 m_impl_up->SetObjectSP(json_obj);
64
65 if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
66 error.SetErrorString("Invalid Syntax");
67 return error;
68}
69
71 LLDB_INSTRUMENT_VA(this, json);
73 s.Print(json);
74 return SetFromJSON(s);
75}
76
79 return this->operator bool();
80}
81
82SBStructuredData::operator bool() const {
84
85 return m_impl_up->IsValid();
86}
87
90
91 m_impl_up->Clear();
92}
93
95 LLDB_INSTRUMENT_VA(this, stream);
96
98 error.SetError(m_impl_up->GetAsJSON(stream.ref()));
99 return error;
100}
101
103 LLDB_INSTRUMENT_VA(this, stream);
104
105 Status error = m_impl_up->GetDescription(stream.ref());
106 SBError sb_error;
107 sb_error.SetError(error);
108 return sb_error;
109}
110
112 LLDB_INSTRUMENT_VA(this);
113
114 return m_impl_up->GetType();
115}
116
118 LLDB_INSTRUMENT_VA(this);
119
120 return m_impl_up->GetSize();
121}
122
124 LLDB_INSTRUMENT_VA(this, keys);
125
127 return false;
128
129 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
130 if (!obj_sp)
131 return false;
132
134 // We claimed we were a dictionary, so this can't be null.
135 assert(dict);
136 // The return kind of GetKeys is an Array:
137 StructuredData::ObjectSP array_sp = dict->GetKeys();
138 StructuredData::Array *key_arr = array_sp->GetAsArray();
139 assert(key_arr);
140
141 key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
142 llvm::StringRef key = object->GetStringValue("");
143 keys.AppendString(key.str().c_str());
144 return true;
145 });
146 return true;
147}
148
150 LLDB_INSTRUMENT_VA(this, key);
151
152 SBStructuredData result;
153 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
154 return result;
155}
156
158 LLDB_INSTRUMENT_VA(this, idx);
159
160 SBStructuredData result;
161 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
162 return result;
163}
164
165uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
166 LLDB_INSTRUMENT_VA(this, fail_value);
167
168 return m_impl_up->GetIntegerValue(fail_value);
169}
170
171double SBStructuredData::GetFloatValue(double fail_value) const {
172 LLDB_INSTRUMENT_VA(this, fail_value);
173
174 return m_impl_up->GetFloatValue(fail_value);
175}
176
177bool SBStructuredData::GetBooleanValue(bool fail_value) const {
178 LLDB_INSTRUMENT_VA(this, fail_value);
179
180 return m_impl_up->GetBooleanValue(fail_value);
181}
182
183size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
184 LLDB_INSTRUMENT_VA(this, dst, dst_len);
185
186 return m_impl_up->GetStringValue(dst, dst_len);
187}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:100
void Print(const char *str)
Definition: SBStream.cpp:64
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
const char * GetData()
Definition: SBStream.cpp:44
void AppendString(const char *str)
lldb::SBStructuredData GetItemAtIndex(size_t idx) const
Return the value corresponding to an index if this data structure is array.
lldb::SBStructuredData & operator=(const lldb::SBStructuredData &rhs)
lldb::SBError GetDescription(lldb::SBStream &stream) const
size_t GetStringValue(char *dst, size_t dst_len) const
Provides the string value if this data structure is a string type.
bool GetBooleanValue(bool fail_value=false) const
Return the boolean value if this data structure is a boolean type.
StructuredDataImplUP m_impl_up
lldb::StructuredDataType GetType() const
Return the type of data in this data structure.
uint64_t GetIntegerValue(uint64_t fail_value=0) const
Return the integer value if this data structure is an integer type.
size_t GetSize() const
Return the size (i.e.
bool GetKeys(lldb::SBStringList &keys) const
Fill keys with the keys in this object and return true if this data structure is a dictionary.
lldb::SBError GetAsJSON(lldb::SBStream &stream) const
lldb::SBStructuredData GetValueForKey(const char *key) const
Return the value corresponding to a key if this data structure is a dictionary type.
lldb::SBError SetFromJSON(lldb::SBStream &stream)
double GetFloatValue(double fail_value=0.0) const
Return the floating point value if this data structure is a floating type.
An error handling class.
Definition: Status.h:44
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
std::shared_ptr< Object > ObjectSP
static ObjectSP ParseJSON(const std::string &json_text)
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
@ eStructuredDataTypeDictionary