9#ifndef LLDB_UTILITY_STRUCTUREDDATA_H
10#define LLDB_UTILITY_STRUCTUREDDATA_H
12#include "llvm/ADT/StringMap.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/Support/JSON.h"
53 template <
typename N>
class Integer;
76 typedef std::variant<UnsignedIntegerSP, SignedIntegerSP>
IntegerSP;
78 class Object :
public std::enable_shared_from_this<Object> {
85 virtual bool IsValid()
const {
return true; }
95 ?
static_cast<Array *
>(
this)
132 ?
static_cast<Float *
>(
this)
138 return ((f !=
nullptr) ? f->
GetValue() : fail_value);
149 return ((b !=
nullptr) ? b->
GetValue() : fail_value);
154 ?
static_cast<String *
>(
this)
176 virtual void Serialize(llvm::json::OStream &s)
const = 0;
179 llvm::json::OStream jso(s.
AsRawOstream(), pretty_print ? 2 : 0);
200 ForEach(std::function<
bool(
Object *
object)>
const &foreach_callback)
const {
201 for (
const auto &object_sp :
m_items) {
202 if (!foreach_callback(object_sp.get()))
223 template <
class IntType>
226 if constexpr (std::numeric_limits<IntType>::is_signed) {
227 if (
auto *signed_value = item_sp->GetAsSignedInteger())
228 return static_cast<IntType
>(signed_value->GetValue());
230 if (
auto *unsigned_value = item_sp->GetAsUnsignedInteger())
231 return static_cast<IntType
>(unsigned_value->GetValue());
239 if (
auto *string_value = item_sp->GetAsString())
240 return string_value->GetValue();
259 if (
auto *dict = item_sp->GetAsDictionary())
270 static_assert(std::is_integral<T>::value ||
271 std::is_floating_point<T>::value,
272 "value type should be integral");
273 if constexpr (std::numeric_limits<T>::is_signed)
274 AddItem(std::make_shared<SignedInteger>(value));
276 AddItem(std::make_shared<UnsignedInteger>(value));
282 AddItem(std::make_shared<String>(std::move(value)));
286 AddItem(std::make_shared<Boolean>(value));
289 void Serialize(llvm::json::OStream &s)
const override;
300 static_assert(std::is_integral<N>::value,
"N must be an integral type");
304 :
Object(std::numeric_limits<N>::is_signed
305 ?
lldb::eStructuredDataTypeSignedInteger
306 :
lldb::eStructuredDataTypeUnsignedInteger),
315 s.value(
static_cast<N
>(
m_value));
319 s.
Printf(std::numeric_limits<N>::is_signed ?
"%" PRId64 :
"%" PRIu64,
339 void Serialize(llvm::json::OStream &s)
const override;
358 void Serialize(llvm::json::OStream &s)
const override;
376 void Serialize(llvm::json::OStream &s)
const override;
404 for (
const auto &pair :
m_dict) {
405 if (!callback(pair.first(), pair.second.get()))
411 auto array_sp = std::make_shared<Array>();
412 for (
auto iter =
m_dict.begin(); iter !=
m_dict.end(); ++iter) {
413 auto key_object_sp = std::make_shared<String>(iter->first());
414 array_sp->Push(key_object_sp);
420 return m_dict.lookup(key);
424 bool success =
false;
426 if (value_sp.get()) {
436 template <
class IntType>
440 if constexpr (std::numeric_limits<IntType>::is_signed) {
441 if (
auto signed_value = value_sp->GetAsSignedInteger()) {
442 result =
static_cast<IntType
>(signed_value->GetValue());
446 if (
auto unsigned_value = value_sp->GetAsUnsignedInteger()) {
447 result =
static_cast<IntType
>(unsigned_value->GetValue());
455 template <
class IntType>
457 IntType default_val)
const {
458 bool success = GetValueForKeyAsInteger<IntType>(key, result);
460 result = default_val;
465 llvm::StringRef &result)
const {
467 if (value_sp.get()) {
468 if (
auto string_value = value_sp->GetAsString()) {
469 result = string_value->GetValue();
477 const char *default_val)
const {
481 result = default_val;
483 result = llvm::StringRef();
492 if (value_sp.get()) {
494 return (result !=
nullptr);
502 if (value_sp.get()) {
504 return (result !=
nullptr);
509 bool HasKey(llvm::StringRef key)
const {
return m_dict.contains(key); }
512 m_dict.insert_or_assign(key, std::move(value_sp));
516 static_assert(std::is_integral<T>::value ||
517 std::is_floating_point<T>::value,
518 "value type should be integral");
519 if constexpr (std::numeric_limits<T>::is_signed)
520 AddItem(key, std::make_shared<SignedInteger>(value));
522 AddItem(key, std::make_shared<UnsignedInteger>(value));
526 AddItem(key, std::make_shared<Float>(value));
530 AddItem(key, std::make_shared<String>(std::move(value)));
534 AddItem(key, std::make_shared<Boolean>(value));
537 void Serialize(llvm::json::OStream &s)
const override;
551 bool IsValid()
const override {
return false; }
553 void Serialize(llvm::json::OStream &s)
const override;
569 void Serialize(llvm::json::OStream &s)
const override;
static llvm::raw_ostream & error(Stream &strm)
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.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
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 AddIntegerItem(T value)
void Serialize(llvm::json::OStream &s) const override
void AddFloatItem(double value)
ObjectSP GetItemAtIndex(size_t idx) const
void AddStringItem(llvm::StringRef value)
void AddBooleanItem(bool value)
std::vector< ObjectSP > collection
void GetDescription(lldb_private::Stream &s) const override
std::optional< IntType > GetItemAtIndexAsInteger(size_t idx) const
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
ObjectSP operator[](size_t idx)
void Push(const ObjectSP &item)
std::optional< Dictionary * > GetItemAtIndexAsDictionary(size_t idx) const
Retrieves the element at index idx from a StructuredData::Array if it is a Dictionary.
std::optional< llvm::StringRef > GetItemAtIndexAsString(size_t idx) 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)
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
llvm::StringMap< ObjectSP > m_dict
void AddIntegerItem(llvm::StringRef key, T value)
ObjectSP GetValueForKey(llvm::StringRef key) const
void AddStringItem(llvm::StringRef key, llvm::StringRef value)
void GetDescription(lldb_private::Stream &s) const override
bool HasKey(llvm::StringRef key) const
void AddItem(llvm::StringRef key, ObjectSP value_sp)
bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
void ForEach(std::function< bool(llvm::StringRef key, Object *object)> const &callback) 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 GetDescription(lldb_private::Stream &s) const override
void Serialize(llvm::json::OStream &s) const override
~Integer() override=default
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)
UnsignedInteger * GetAsUnsignedInteger()
virtual bool IsValid() const
Object(lldb::StructuredDataType t=lldb::eStructuredDataTypeInvalid)
int64_t GetSignedIntegerValue(int64_t fail_value=0)
bool GetBooleanValue(bool fail_value=false)
void SetType(lldb::StructuredDataType t)
void Dump(lldb_private::Stream &s, bool pretty_print=true) const
lldb::StructuredDataType GetType() const
SignedInteger * GetAsSignedInteger()
uint64_t GetUnsignedIntegerValue(uint64_t fail_value=0)
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< Generic > GenericSP
std::shared_ptr< UnsignedInteger > UnsignedIntegerSP
std::shared_ptr< Float > FloatSP
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
static ObjectSP ParseJSON(llvm::StringRef json_text)
std::shared_ptr< String > StringSP
std::shared_ptr< Array > ArraySP
std::shared_ptr< Boolean > BooleanSP
static ObjectSP ParseJSONFromFile(const FileSpec &file, Status &error)
std::shared_ptr< SignedInteger > SignedIntegerSP
std::variant< UnsignedIntegerSP, SignedIntegerSP > IntegerSP
A class that represents a running process on the host machine.
@ eStructuredDataTypeFloat
@ eStructuredDataTypeDictionary
@ eStructuredDataTypeInvalid
@ eStructuredDataTypeInteger
@ eStructuredDataTypeGeneric
@ eStructuredDataTypeArray
@ eStructuredDataTypeSignedInteger
@ eStructuredDataTypeUnsignedInteger
@ eStructuredDataTypeBoolean
@ eStructuredDataTypeString