1 //===-- StructuredData.h ----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_StructuredData_h_
10 #define liblldb_StructuredData_h_
12 #include "llvm/ADT/StringRef.h"
14 #include "lldb/Utility/ConstString.h"
15 #include "lldb/Utility/FileSpec.h"
16 #include "lldb/lldb-enumerations.h"
25 #include <type_traits>
29 namespace lldb_private {
34 namespace lldb_private {
36 /// \class StructuredData StructuredData.h "lldb/Utility/StructuredData.h"
37 /// A class which can hold structured data
39 /// The StructuredData class is designed to hold the data from a JSON or plist
40 /// style file -- a serialized data structure with dictionaries (maps,
41 /// hashes), arrays, and concrete values like integers, floating point
42 /// numbers, strings, booleans.
44 /// StructuredData does not presuppose any knowledge of the schema for the
45 /// data it is holding; it can parse JSON data, for instance, and other parts
46 /// of lldb can iterate through the parsed data set to find keys and values
47 /// that may be present.
49 class StructuredData {
60 typedef std::shared_ptr<Object> ObjectSP;
61 typedef std::shared_ptr<Array> ArraySP;
62 typedef std::shared_ptr<Integer> IntegerSP;
63 typedef std::shared_ptr<Float> FloatSP;
64 typedef std::shared_ptr<Boolean> BooleanSP;
65 typedef std::shared_ptr<String> StringSP;
66 typedef std::shared_ptr<Dictionary> DictionarySP;
67 typedef std::shared_ptr<Generic> GenericSP;
69 class Object : public std::enable_shared_from_this<Object> {
71 Object(lldb::StructuredDataType t = lldb::eStructuredDataTypeInvalid)
74 virtual ~Object() = default;
76 virtual bool IsValid() const { return true; }
78 virtual void Clear() { m_type = lldb::eStructuredDataTypeInvalid; }
80 lldb::StructuredDataType GetType() const { return m_type; }
82 void SetType(lldb::StructuredDataType t) { m_type = t; }
85 return ((m_type == lldb::eStructuredDataTypeArray)
86 ? static_cast<Array *>(this)
90 Dictionary *GetAsDictionary() {
91 return ((m_type == lldb::eStructuredDataTypeDictionary)
92 ? static_cast<Dictionary *>(this)
96 Integer *GetAsInteger() {
97 return ((m_type == lldb::eStructuredDataTypeInteger)
98 ? static_cast<Integer *>(this)
102 uint64_t GetIntegerValue(uint64_t fail_value = 0) {
103 Integer *integer = GetAsInteger();
104 return ((integer != nullptr) ? integer->GetValue() : fail_value);
107 Float *GetAsFloat() {
108 return ((m_type == lldb::eStructuredDataTypeFloat)
109 ? static_cast<Float *>(this)
113 double GetFloatValue(double fail_value = 0.0) {
114 Float *f = GetAsFloat();
115 return ((f != nullptr) ? f->GetValue() : fail_value);
118 Boolean *GetAsBoolean() {
119 return ((m_type == lldb::eStructuredDataTypeBoolean)
120 ? static_cast<Boolean *>(this)
124 bool GetBooleanValue(bool fail_value = false) {
125 Boolean *b = GetAsBoolean();
126 return ((b != nullptr) ? b->GetValue() : fail_value);
129 String *GetAsString() {
130 return ((m_type == lldb::eStructuredDataTypeString)
131 ? static_cast<String *>(this)
135 llvm::StringRef GetStringValue(const char *fail_value = nullptr) {
136 String *s = GetAsString();
138 return s->GetValue();
143 Generic *GetAsGeneric() {
144 return ((m_type == lldb::eStructuredDataTypeGeneric)
145 ? static_cast<Generic *>(this)
149 ObjectSP GetObjectForDotSeparatedPath(llvm::StringRef path);
151 void DumpToStdout(bool pretty_print = true) const;
153 virtual void Dump(Stream &s, bool pretty_print = true) const = 0;
156 lldb::StructuredDataType m_type;
159 class Array : public Object {
161 Array() : Object(lldb::eStructuredDataTypeArray) {}
163 ~Array() override = default;
166 ForEach(std::function<bool(Object *object)> const &foreach_callback) const {
167 for (const auto &object_sp : m_items) {
168 if (!foreach_callback(object_sp.get()))
174 size_t GetSize() const { return m_items.size(); }
176 ObjectSP operator[](size_t idx) {
177 if (idx < m_items.size())
182 ObjectSP GetItemAtIndex(size_t idx) const {
183 assert(idx < GetSize());
184 if (idx < m_items.size())
189 template <class IntType>
190 bool GetItemAtIndexAsInteger(size_t idx, IntType &result) const {
191 ObjectSP value_sp = GetItemAtIndex(idx);
192 if (value_sp.get()) {
193 if (auto int_value = value_sp->GetAsInteger()) {
194 result = static_cast<IntType>(int_value->GetValue());
201 template <class IntType>
202 bool GetItemAtIndexAsInteger(size_t idx, IntType &result,
203 IntType default_val) const {
204 bool success = GetItemAtIndexAsInteger(idx, result);
206 result = default_val;
210 bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result) const {
211 ObjectSP value_sp = GetItemAtIndex(idx);
212 if (value_sp.get()) {
213 if (auto string_value = value_sp->GetAsString()) {
214 result = string_value->GetValue();
221 bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result,
222 llvm::StringRef default_val) const {
223 bool success = GetItemAtIndexAsString(idx, result);
225 result = default_val;
229 bool GetItemAtIndexAsString(size_t idx, ConstString &result) const {
230 ObjectSP value_sp = GetItemAtIndex(idx);
231 if (value_sp.get()) {
232 if (auto string_value = value_sp->GetAsString()) {
233 result = ConstString(string_value->GetValue());
240 bool GetItemAtIndexAsString(size_t idx, ConstString &result,
241 const char *default_val) const {
242 bool success = GetItemAtIndexAsString(idx, result);
244 result.SetCString(default_val);
248 bool GetItemAtIndexAsDictionary(size_t idx, Dictionary *&result) const {
250 ObjectSP value_sp = GetItemAtIndex(idx);
251 if (value_sp.get()) {
252 result = value_sp->GetAsDictionary();
253 return (result != nullptr);
258 bool GetItemAtIndexAsArray(size_t idx, Array *&result) const {
260 ObjectSP value_sp = GetItemAtIndex(idx);
261 if (value_sp.get()) {
262 result = value_sp->GetAsArray();
263 return (result != nullptr);
268 void Push(ObjectSP item) { m_items.push_back(item); }
270 void AddItem(ObjectSP item) { m_items.push_back(item); }
272 void Dump(Stream &s, bool pretty_print = true) const override;
275 typedef std::vector<ObjectSP> collection;
279 class Integer : public Object {
281 Integer(uint64_t i = 0)
282 : Object(lldb::eStructuredDataTypeInteger), m_value(i) {}
284 ~Integer() override = default;
286 void SetValue(uint64_t value) { m_value = value; }
288 uint64_t GetValue() { return m_value; }
290 void Dump(Stream &s, bool pretty_print = true) const override;
296 class Float : public Object {
298 Float(double d = 0.0)
299 : Object(lldb::eStructuredDataTypeFloat), m_value(d) {}
301 ~Float() override = default;
303 void SetValue(double value) { m_value = value; }
305 double GetValue() { return m_value; }
307 void Dump(Stream &s, bool pretty_print = true) const override;
313 class Boolean : public Object {
315 Boolean(bool b = false)
316 : Object(lldb::eStructuredDataTypeBoolean), m_value(b) {}
318 ~Boolean() override = default;
320 void SetValue(bool value) { m_value = value; }
322 bool GetValue() { return m_value; }
324 void Dump(Stream &s, bool pretty_print = true) const override;
330 class String : public Object {
332 String() : Object(lldb::eStructuredDataTypeString) {}
333 explicit String(llvm::StringRef S)
334 : Object(lldb::eStructuredDataTypeString), m_value(S) {}
336 void SetValue(llvm::StringRef S) { m_value = S; }
338 llvm::StringRef GetValue() { return m_value; }
340 void Dump(Stream &s, bool pretty_print = true) const override;
346 class Dictionary : public Object {
348 Dictionary() : Object(lldb::eStructuredDataTypeDictionary), m_dict() {}
350 ~Dictionary() override = default;
352 size_t GetSize() const { return m_dict.size(); }
354 void ForEach(std::function<bool(ConstString key, Object *object)> const
356 for (const auto &pair : m_dict) {
357 if (!callback(pair.first, pair.second.get()))
362 ObjectSP GetKeys() const {
363 auto object_sp = std::make_shared<Array>();
364 collection::const_iterator iter;
365 for (iter = m_dict.begin(); iter != m_dict.end(); ++iter) {
366 auto key_object_sp = std::make_shared<String>();
367 key_object_sp->SetValue(iter->first.AsCString());
368 object_sp->Push(key_object_sp);
373 ObjectSP GetValueForKey(llvm::StringRef key) const {
376 ConstString key_cs(key);
377 collection::const_iterator iter = m_dict.find(key_cs);
378 if (iter != m_dict.end())
379 value_sp = iter->second;
384 bool GetValueForKeyAsBoolean(llvm::StringRef key, bool &result) const {
385 bool success = false;
386 ObjectSP value_sp = GetValueForKey(key);
387 if (value_sp.get()) {
388 Boolean *result_ptr = value_sp->GetAsBoolean();
390 result = result_ptr->GetValue();
396 template <class IntType>
397 bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const {
398 ObjectSP value_sp = GetValueForKey(key);
400 if (auto int_value = value_sp->GetAsInteger()) {
401 result = static_cast<IntType>(int_value->GetValue());
408 template <class IntType>
409 bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result,
410 IntType default_val) const {
411 bool success = GetValueForKeyAsInteger<IntType>(key, result);
413 result = default_val;
417 bool GetValueForKeyAsString(llvm::StringRef key,
418 llvm::StringRef &result) const {
419 ObjectSP value_sp = GetValueForKey(key);
420 if (value_sp.get()) {
421 if (auto string_value = value_sp->GetAsString()) {
422 result = string_value->GetValue();
429 bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result,
430 const char *default_val) const {
431 bool success = GetValueForKeyAsString(key, result);
434 result = default_val;
436 result = llvm::StringRef();
441 bool GetValueForKeyAsString(llvm::StringRef key,
442 ConstString &result) const {
443 ObjectSP value_sp = GetValueForKey(key);
444 if (value_sp.get()) {
445 if (auto string_value = value_sp->GetAsString()) {
446 result = ConstString(string_value->GetValue());
453 bool GetValueForKeyAsString(llvm::StringRef key, ConstString &result,
454 const char *default_val) const {
455 bool success = GetValueForKeyAsString(key, result);
457 result.SetCString(default_val);
461 bool GetValueForKeyAsDictionary(llvm::StringRef key,
462 Dictionary *&result) const {
464 ObjectSP value_sp = GetValueForKey(key);
465 if (value_sp.get()) {
466 result = value_sp->GetAsDictionary();
467 return (result != nullptr);
472 bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const {
474 ObjectSP value_sp = GetValueForKey(key);
475 if (value_sp.get()) {
476 result = value_sp->GetAsArray();
477 return (result != nullptr);
482 bool HasKey(llvm::StringRef key) const {
483 ConstString key_cs(key);
484 collection::const_iterator search = m_dict.find(key_cs);
485 return search != m_dict.end();
488 void AddItem(llvm::StringRef key, ObjectSP value_sp) {
489 ConstString key_cs(key);
490 m_dict[key_cs] = value_sp;
493 void AddIntegerItem(llvm::StringRef key, uint64_t value) {
494 AddItem(key, std::make_shared<Integer>(value));
497 void AddFloatItem(llvm::StringRef key, double value) {
498 AddItem(key, std::make_shared<Float>(value));
501 void AddStringItem(llvm::StringRef key, llvm::StringRef value) {
502 AddItem(key, std::make_shared<String>(std::move(value)));
505 void AddBooleanItem(llvm::StringRef key, bool value) {
506 AddItem(key, std::make_shared<Boolean>(value));
509 void Dump(Stream &s, bool pretty_print = true) const override;
512 typedef std::map<ConstString, ObjectSP> collection;
516 class Null : public Object {
518 Null() : Object(lldb::eStructuredDataTypeNull) {}
520 ~Null() override = default;
522 bool IsValid() const override { return false; }
524 void Dump(Stream &s, bool pretty_print = true) const override;
527 class Generic : public Object {
529 explicit Generic(void *object = nullptr)
530 : Object(lldb::eStructuredDataTypeGeneric), m_object(object) {}
532 void SetValue(void *value) { m_object = value; }
534 void *GetValue() const { return m_object; }
536 bool IsValid() const override { return m_object != nullptr; }
538 void Dump(Stream &s, bool pretty_print = true) const override;
544 static ObjectSP ParseJSON(std::string json_text);
546 static ObjectSP ParseJSONFromFile(const FileSpec &file, Status &error);
549 } // namespace lldb_private
551 #endif // liblldb_StructuredData_h_