Go to the documentation of this file.
9 #ifndef LLDB_UTILITY_STRUCTUREDDATA_H
10 #define LLDB_UTILITY_STRUCTUREDDATA_H
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/JSON.h"
27 #include <type_traits>
61 typedef std::shared_ptr<Object>
ObjectSP;
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);
169 ~Array()
override =
default;
172 ForEach(std::function<
bool(
Object *
object)>
const &foreach_callback)
const {
173 for (
const auto &object_sp :
m_items) {
174 if (!foreach_callback(object_sp.get()))
195 template <
class IntType>
198 if (value_sp.get()) {
199 if (
auto int_value = value_sp->GetAsInteger()) {
200 result =
static_cast<IntType
>(int_value->GetValue());
207 template <
class IntType>
209 IntType default_val)
const {
212 result = default_val;
218 if (value_sp.get()) {
219 if (
auto string_value = value_sp->GetAsString()) {
220 result = string_value->GetValue();
228 llvm::StringRef default_val)
const {
231 result = default_val;
237 if (value_sp.get()) {
238 if (
auto string_value = value_sp->GetAsString()) {
247 const char *default_val)
const {
257 if (value_sp.get()) {
259 return (result !=
nullptr);
267 if (value_sp.get()) {
269 return (result !=
nullptr);
278 void Serialize(llvm::json::OStream &s)
const override;
296 void Serialize(llvm::json::OStream &s)
const override;
307 ~Float()
override =
default;
313 void Serialize(llvm::json::OStream &s)
const override;
330 void Serialize(llvm::json::OStream &s)
const override;
346 void Serialize(llvm::json::OStream &s)
const override;
372 for (
const auto &pair :
m_dict) {
373 if (!callback(pair.first, pair.second.get()))
379 auto array_sp = std::make_shared<Array>();
380 collection::const_iterator iter;
381 for (iter =
m_dict.begin(); iter !=
m_dict.end(); ++iter) {
382 auto key_object_sp = std::make_shared<String>();
383 key_object_sp->SetValue(iter->first.AsCString());
384 array_sp->Push(key_object_sp);
393 collection::const_iterator iter =
m_dict.find(key_cs);
395 value_sp = iter->second;
401 bool success =
false;
403 if (value_sp.get()) {
412 template <
class IntType>
416 if (
auto int_value = value_sp->GetAsInteger()) {
417 result =
static_cast<IntType
>(int_value->GetValue());
424 template <
class IntType>
426 IntType default_val)
const {
427 bool success = GetValueForKeyAsInteger<IntType>(key, result);
429 result = default_val;
434 llvm::StringRef &result)
const {
436 if (value_sp.get()) {
437 if (
auto string_value = value_sp->GetAsString()) {
438 result = string_value->GetValue();
446 const char *default_val)
const {
450 result = default_val;
452 result = llvm::StringRef();
460 if (value_sp.get()) {
461 if (
auto string_value = value_sp->GetAsString()) {
470 const char *default_val)
const {
481 if (value_sp.get()) {
483 return (result !=
nullptr);
491 if (value_sp.get()) {
493 return (result !=
nullptr);
500 collection::const_iterator search =
m_dict.find(key_cs);
501 return search !=
m_dict.end();
506 m_dict[key_cs] = std::move(value_sp);
510 AddItem(key, std::make_shared<Integer>(value));
514 AddItem(key, std::make_shared<Float>(value));
518 AddItem(key, std::make_shared<String>(std::move(value)));
522 AddItem(key, std::make_shared<Boolean>(value));
525 void Serialize(llvm::json::OStream &s)
const override;
536 ~Null()
override =
default;
538 bool IsValid()
const override {
return false; }
540 void Serialize(llvm::json::OStream &s)
const override;
554 void Serialize(llvm::json::OStream &s)
const override;
566 #endif // LLDB_UTILITY_STRUCTUREDDATA_H
~Float() override=default
@ eStructuredDataTypeBoolean
virtual bool IsValid() const
ObjectSP GetValueForKey(llvm::StringRef key) const
std::shared_ptr< Generic > GenericSP
@ eStructuredDataTypeString
void Serialize(llvm::json::OStream &s) const override
@ eStructuredDataTypeDictionary
bool GetItemAtIndexAsDictionary(size_t idx, Dictionary *&result) const
~Boolean() override=default
lldb::StructuredDataType GetType() const
String(llvm::StringRef S)
void DumpToStdout(bool pretty_print=true) const
double GetFloatValue(double fail_value=0.0)
bool GetItemAtIndexAsInteger(size_t idx, IntType &result, IntType default_val) const
void SetCString(const char *cstr)
Set the C string value.
void Serialize(llvm::json::OStream &s) const override
void Serialize(llvm::json::OStream &s) const override
std::vector< ObjectSP > collection
Dictionary(ObjectSP obj_sp)
bool GetItemAtIndexAsString(size_t idx, ConstString &result) const
void Serialize(llvm::json::OStream &s) const override
@ eStructuredDataTypeInvalid
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
void AddBooleanItem(llvm::StringRef key, bool value)
~Dictionary() override=default
@ eStructuredDataTypeGeneric
static llvm::raw_ostream & error(Stream &strm)
std::shared_ptr< String > StringSP
static ObjectSP ParseJSONFromFile(const FileSpec &file, Status &error)
void Dump(lldb_private::Stream &s, bool pretty_print=true) const
llvm::StringRef GetStringValue(const char *fail_value=nullptr)
bool GetValueForKeyAsString(llvm::StringRef key, ConstString &result, const char *default_val) const
void SetValue(double value)
std::shared_ptr< Object > ObjectSP
bool GetItemAtIndexAsString(size_t idx, ConstString &result, const char *default_val) const
void SetValue(bool value)
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result, IntType default_val) const
bool GetItemAtIndexAsArray(size_t idx, Array *&result) const
void Serialize(llvm::json::OStream &s) const override
void AddItem(const ObjectSP &item)
uint64_t GetIntegerValue(uint64_t fail_value=0)
std::shared_ptr< Dictionary > DictionarySP
void Push(const ObjectSP &item)
Generic(void *object=nullptr)
string(SUBSTRING ${p} 10 -1 pStripped) if($
llvm::StringRef GetValue()
bool IsValid() const override
void AddItem(llvm::StringRef key, ObjectSP value_sp)
Object(lldb::StructuredDataType t=lldb::eStructuredDataTypeInvalid)
ObjectSP operator[](size_t idx)
virtual ~Object()=default
bool GetBooleanValue(bool fail_value=false)
std::shared_ptr< Float > FloatSP
void AddFloatItem(llvm::StringRef key, double value)
void ForEach(std::function< bool(ConstString key, Object *object)> const &callback) const
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result, const char *default_val) const
void SetType(lldb::StructuredDataType t)
Dictionary * GetAsDictionary()
void Serialize(llvm::json::OStream &s) const override
bool GetItemAtIndexAsInteger(size_t idx, IntType &result) const
virtual void Serialize(llvm::json::OStream &s) const =0
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
bool HasKey(llvm::StringRef key) const
std::shared_ptr< Boolean > BooleanSP
@ eStructuredDataTypeInteger
@ eStructuredDataTypeFloat
void AddStringItem(llvm::StringRef key, llvm::StringRef value)
void SetValue(llvm::StringRef S)
bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result, llvm::StringRef default_val) const
A class that represents a running process on the host machine.
ObjectSP GetObjectForDotSeparatedPath(llvm::StringRef path)
bool GetValueForKeyAsDictionary(llvm::StringRef key, Dictionary *&result) const
void AddIntegerItem(llvm::StringRef key, uint64_t value)
std::map< ConstString, ObjectSP > collection
@ eStructuredDataTypeArray
bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result) const
static ObjectSP ParseJSON(const std::string &json_text)
std::shared_ptr< Array > ArraySP
~Integer() override=default
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
ObjectSP GetItemAtIndex(size_t idx) const
void SetValue(void *value)
void Serialize(llvm::json::OStream &s) const override
bool GetValueForKeyAsString(llvm::StringRef key, ConstString &result) const
bool IsValid() const override
@ eStructuredDataTypeNull
bool GetValueForKeyAsBoolean(llvm::StringRef key, bool &result) const
lldb::StructuredDataType m_type
std::shared_ptr< Integer > IntegerSP
~Array() override=default
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
void Serialize(llvm::json::OStream &s) const override
void SetValue(uint64_t value)