9#ifndef LLDB_UTILITY_STRUCTUREDDATA_H
10#define LLDB_UTILITY_STRUCTUREDDATA_H
12#include "llvm/ADT/StringRef.h"
13#include "llvm/Support/JSON.h"
70 class Object :
public std::enable_shared_from_this<Object> {
77 virtual bool IsValid()
const {
return true; }
87 ?
static_cast<Array *
>(
this)
110 ?
static_cast<Float *
>(
this)
116 return ((f !=
nullptr) ? f->
GetValue() : fail_value);
127 return ((b !=
nullptr) ? b->
GetValue() : fail_value);
132 ?
static_cast<String *
>(
this)
154 virtual void Serialize(llvm::json::OStream &s)
const = 0;
157 llvm::json::OStream jso(s.
AsRawOstream(), pretty_print ? 2 : 0);
178 ForEach(std::function<
bool(
Object *
object)>
const &foreach_callback)
const {
179 for (
const auto &object_sp :
m_items) {
180 if (!foreach_callback(object_sp.get()))
201 template <
class IntType>
204 if (value_sp.get()) {
205 if (
auto int_value = value_sp->GetAsInteger()) {
206 result =
static_cast<IntType
>(int_value->GetValue());
213 template <
class IntType>
215 IntType default_val)
const {
218 result = default_val;
224 if (value_sp.get()) {
225 if (
auto string_value = value_sp->GetAsString()) {
226 result = string_value->GetValue();
234 llvm::StringRef default_val)
const {
237 result = default_val;
243 if (value_sp.get()) {
244 if (
auto string_value = value_sp->GetAsString()) {
253 const char *default_val)
const {
263 if (value_sp.get()) {
265 return (result !=
nullptr);
273 if (value_sp.get()) {
275 return (result !=
nullptr);
284 void Serialize(llvm::json::OStream &s)
const override;
304 void Serialize(llvm::json::OStream &s)
const override;
323 void Serialize(llvm::json::OStream &s)
const override;
342 void Serialize(llvm::json::OStream &s)
const override;
360 void Serialize(llvm::json::OStream &s)
const override;
388 for (
const auto &pair :
m_dict) {
389 if (!callback(pair.first, pair.second.get()))
395 auto array_sp = std::make_shared<Array>();
396 collection::const_iterator iter;
397 for (iter =
m_dict.begin(); iter !=
m_dict.end(); ++iter) {
398 auto key_object_sp = std::make_shared<String>();
399 key_object_sp->SetValue(iter->first.AsCString());
400 array_sp->Push(key_object_sp);
409 collection::const_iterator iter =
m_dict.find(key_cs);
411 value_sp = iter->second;
417 bool success =
false;
419 if (value_sp.get()) {
428 template <
class IntType>
432 if (
auto int_value = value_sp->GetAsInteger()) {
433 result =
static_cast<IntType
>(int_value->GetValue());
440 template <
class IntType>
442 IntType default_val)
const {
443 bool success = GetValueForKeyAsInteger<IntType>(key, result);
445 result = default_val;
450 llvm::StringRef &result)
const {
452 if (value_sp.get()) {
453 if (
auto string_value = value_sp->GetAsString()) {
454 result = string_value->GetValue();
462 const char *default_val)
const {
466 result = default_val;
468 result = llvm::StringRef();
476 if (value_sp.get()) {
477 if (
auto string_value = value_sp->GetAsString()) {
486 const char *default_val)
const {
497 if (value_sp.get()) {
499 return (result !=
nullptr);
507 if (value_sp.get()) {
509 return (result !=
nullptr);
516 collection::const_iterator search =
m_dict.find(key_cs);
517 return search !=
m_dict.end();
522 m_dict[key_cs] = std::move(value_sp);
526 AddItem(key, std::make_shared<Integer>(value));
530 AddItem(key, std::make_shared<Float>(value));
534 AddItem(key, std::make_shared<String>(std::move(value)));
538 AddItem(key, std::make_shared<Boolean>(value));
541 void Serialize(llvm::json::OStream &s)
const override;
556 bool IsValid()
const override {
return false; }
558 void Serialize(llvm::json::OStream &s)
const override;
574 void Serialize(llvm::json::OStream &s)
const override;
static llvm::raw_ostream & error(Stream &strm)
A uniqued constant string class.
void SetCString(const char *cstr)
Set the C string value.
A stream class that can stream formatted output to a file.
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
void IndentMore(unsigned amount=2)
Increment the current indentation level.
~Array() override=default
void AddItem(const ObjectSP &item)
void Serialize(llvm::json::OStream &s) const override
ObjectSP GetItemAtIndex(size_t idx) const
bool GetItemAtIndexAsString(size_t idx, ConstString &result) const
bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result) const
std::vector< ObjectSP > collection
void GetDescription(lldb_private::Stream &s) const override
bool GetItemAtIndexAsString(size_t idx, ConstString &result, const char *default_val) const
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
ObjectSP operator[](size_t idx)
void Push(const ObjectSP &item)
bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result, llvm::StringRef default_val) const
bool GetItemAtIndexAsArray(size_t idx, Array *&result) const
bool GetItemAtIndexAsDictionary(size_t idx, Dictionary *&result) const
bool GetItemAtIndexAsInteger(size_t idx, IntType &result, IntType default_val) const
bool GetItemAtIndexAsInteger(size_t idx, IntType &result) const
void Serialize(llvm::json::OStream &s) const override
~Boolean() override=default
void SetValue(bool value)
void GetDescription(lldb_private::Stream &s) const override
void AddBooleanItem(llvm::StringRef key, bool value)
void Serialize(llvm::json::OStream &s) const override
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
~Dictionary() override=default
Dictionary(ObjectSP obj_sp)
std::map< ConstString, ObjectSP > collection
bool GetValueForKeyAsString(llvm::StringRef key, ConstString &result) const
bool GetValueForKeyAsBoolean(llvm::StringRef key, bool &result) const
void AddFloatItem(llvm::StringRef key, double value)
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result, IntType default_val) const
ObjectSP GetValueForKey(llvm::StringRef key) const
void AddIntegerItem(llvm::StringRef key, uint64_t value)
void AddStringItem(llvm::StringRef key, llvm::StringRef value)
void GetDescription(lldb_private::Stream &s) const override
void ForEach(std::function< bool(ConstString key, Object *object)> const &callback) const
bool HasKey(llvm::StringRef key) const
bool GetValueForKeyAsString(llvm::StringRef key, ConstString &result, const char *default_val) const
void AddItem(llvm::StringRef key, ObjectSP value_sp)
bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result, const char *default_val) const
bool GetValueForKeyAsDictionary(llvm::StringRef key, Dictionary *&result) const
void SetValue(double value)
~Float() override=default
void Serialize(llvm::json::OStream &s) const override
void GetDescription(lldb_private::Stream &s) const override
void SetValue(void *value)
void GetDescription(lldb_private::Stream &s) const override
void Serialize(llvm::json::OStream &s) const override
Generic(void *object=nullptr)
bool IsValid() const override
void Serialize(llvm::json::OStream &s) const override
~Integer() override=default
void GetDescription(lldb_private::Stream &s) const override
void SetValue(uint64_t value)
void GetDescription(lldb_private::Stream &s) const override
bool IsValid() const override
void Serialize(llvm::json::OStream &s) const override
Dictionary * GetAsDictionary()
lldb::StructuredDataType m_type
virtual void Serialize(llvm::json::OStream &s) const =0
llvm::StringRef GetStringValue(const char *fail_value=nullptr)
void DumpToStdout(bool pretty_print=true) const
virtual void GetDescription(lldb_private::Stream &s) const
double GetFloatValue(double fail_value=0.0)
virtual bool IsValid() const
Object(lldb::StructuredDataType t=lldb::eStructuredDataTypeInvalid)
bool GetBooleanValue(bool fail_value=false)
void SetType(lldb::StructuredDataType t)
uint64_t GetIntegerValue(uint64_t fail_value=0)
void Dump(lldb_private::Stream &s, bool pretty_print=true) const
lldb::StructuredDataType GetType() const
ObjectSP GetObjectForDotSeparatedPath(llvm::StringRef path)
virtual ~Object()=default
void GetDescription(lldb_private::Stream &s) const override
void SetValue(llvm::StringRef S)
llvm::StringRef GetValue()
void Serialize(llvm::json::OStream &s) const override
String(llvm::StringRef S)
A class which can hold structured data.
static bool IsRecordType(const ObjectSP object_sp)
std::shared_ptr< Integer > IntegerSP
std::shared_ptr< Generic > GenericSP
std::shared_ptr< Float > FloatSP
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< String > StringSP
static ObjectSP ParseJSON(const std::string &json_text)
std::shared_ptr< Array > ArraySP
std::shared_ptr< Boolean > BooleanSP
static ObjectSP ParseJSONFromFile(const FileSpec &file, Status &error)
A class that represents a running process on the host machine.
@ eStructuredDataTypeFloat
@ eStructuredDataTypeDictionary
@ eStructuredDataTypeInvalid
@ eStructuredDataTypeInteger
@ eStructuredDataTypeGeneric
@ eStructuredDataTypeArray
@ eStructuredDataTypeBoolean
@ eStructuredDataTypeString