LLDB  mainline
SBStructuredData.cpp
Go to the documentation of this file.
1 //===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===//
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 
10 #include "SBReproducerPrivate.h"
11 
12 #include "lldb/API/SBStream.h"
13 #include "lldb/API/SBStringList.h"
16 #include "lldb/Utility/Event.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/Utility/Stream.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 #pragma mark--
25 #pragma mark SBStructuredData
26 
29 }
30 
32  : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {
34  rhs);
35 }
36 
37 SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
38  : m_impl_up(new StructuredDataImpl(event_sp)) {
39  LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &), event_sp);
40 }
41 
43  : m_impl_up(impl) {
46 }
47 
49 
54  SBStructuredData, operator=,(const lldb::SBStructuredData &), rhs);
55 
56  *m_impl_up = *rhs.m_impl_up;
57  return LLDB_RECORD_RESULT(*this);
58 }
59 
62  (lldb::SBStream &), stream);
63 
64  lldb::SBError error;
65  std::string json_str(stream.GetData());
66 
67  StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
68  m_impl_up->SetObjectSP(json_obj);
69 
70  if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
71  error.SetErrorString("Invalid Syntax");
72  return LLDB_RECORD_RESULT(error);
73 }
74 
77  return this->operator bool();
78 }
79 SBStructuredData::operator bool() const {
81 
82  return m_impl_up->IsValid();
83 }
84 
87 
88  m_impl_up->Clear();
89 }
90 
93  (lldb::SBStream &), stream);
94 
95  SBError error;
96  error.SetError(m_impl_up->GetAsJSON(stream.ref()));
97  return LLDB_RECORD_RESULT(error);
98 }
99 
102  (lldb::SBStream &), stream);
103 
104  Status error = m_impl_up->GetDescription(stream.ref());
105  SBError sb_error;
106  sb_error.SetError(error);
107  return LLDB_RECORD_RESULT(sb_error);
108 }
109 
112  GetType);
113 
114  return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid);
115 }
116 
119 
120  return (m_impl_up ? m_impl_up->GetSize() : 0);
121 }
122 
125  (lldb::SBStringList &), keys);
126 
127  if (!m_impl_up)
128  return false;
129 
131  return false;
132 
133  StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
134  if (!obj_sp)
135  return false;
136 
138  // We claimed we were a dictionary, so this can't be null.
139  assert(dict);
140  // The return kind of GetKeys is an Array:
141  StructuredData::ObjectSP array_sp = dict->GetKeys();
142  StructuredData::Array *key_arr = array_sp->GetAsArray();
143  assert(key_arr);
144 
145  key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
146  llvm::StringRef key = object->GetStringValue("");
147  keys.AppendString(key.str().c_str());
148  return true;
149  });
150  return true;
151 }
152 
155  GetValueForKey, (const char *), key);
156 
157  if (!m_impl_up)
159 
160  SBStructuredData result;
161  result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
162  return LLDB_RECORD_RESULT(result);
163 }
164 
167  GetItemAtIndex, (size_t), idx);
168 
169  if (!m_impl_up)
171 
172  SBStructuredData result;
173  result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
174  return LLDB_RECORD_RESULT(result);
175 }
176 
177 uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
179  (uint64_t), fail_value);
180 
181  return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value);
182 }
183 
184 double SBStructuredData::GetFloatValue(double fail_value) const {
186  fail_value);
187 
188  return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value);
189 }
190 
191 bool SBStructuredData::GetBooleanValue(bool fail_value) const {
193  fail_value);
194 
195  return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value);
196 }
197 
198 size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
200  (char *, size_t), dst, dst_len);
201 
202  return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0);
203 }
204 
205 namespace lldb_private {
206 namespace repro {
207 
208 template <>
212  (const lldb::SBStructuredData &));
213  LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &));
218  SBStructuredData, operator=,(const lldb::SBStructuredData &));
220  (lldb::SBStream &));
222  LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, operator bool, ());
225  (lldb::SBStream &));
227  (lldb::SBStream &));
229  GetType, ());
232  (lldb::SBStringList &));
234  GetValueForKey, (const char *));
236  GetItemAtIndex, (size_t));
238  (uint64_t));
240  (double));
243  (char *, size_t));
244 }
245 
246 }
247 }
The registry contains a unique mapping between functions and their ID.
lldb::SBError GetAsJSON(lldb::SBStream &stream) const
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
size_t GetSize() const
Return the size (i.e.
lldb::SBError GetDescription(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.
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:97
void AppendString(const char *str)
void SetErrorString(const char *err_str)
Definition: SBError.cpp:124
const char * GetData()
Definition: SBStream.cpp:42
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
bool GetBooleanValue(bool fail_value=false) const
Return the boolean value if this data structure is a boolean type.
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
uint64_t GetIntegerValue(uint64_t fail_value=0) const
Return the integer value if this data structure is an integer type.
size_t GetStringValue(char *dst, size_t dst_len) const
Provides the string value if this data structure is a string type.
double GetFloatValue(double fail_value=0.0) const
Return the floating point value if this data structure is a floating type.
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
StructuredDataImplUP m_impl_up
lldb_private::Stream & ref()
Definition: SBStream.cpp:162
lldb::StructuredDataType GetType() const
Return the type of data in this data structure.
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
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::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)
Definition: SBAddress.h:15
std::shared_ptr< Object > ObjectSP
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
void RegisterMethods< SBStructuredData >(Registry &R)
#define LLDB_RECORD_RESULT(Result)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
An error handling class.
Definition: Status.h:44
lldb::SBError SetFromJSON(lldb::SBStream &stream)