LLDB mainline
StructuredDataImpl.h
Go to the documentation of this file.
1//===-- StructuredDataImpl.h ------------------------------------*- 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
9#ifndef LLDB_CORE_STRUCTUREDDATAIMPL_H
10#define LLDB_CORE_STRUCTUREDDATAIMPL_H
11
13#include "lldb/Utility/Event.h"
14#include "lldb/Utility/Status.h"
15#include "lldb/Utility/Stream.h"
18#include "lldb/lldb-forward.h"
19#include "llvm/ADT/StringRef.h"
20
21#pragma mark--
22#pragma mark StructuredDataImpl
23
24namespace lldb_private {
25
27public:
28 StructuredDataImpl() = default;
29
31
34
37 EventDataStructuredData::GetPluginFromEvent(event_sp.get())),
38 m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) {
39 }
40
42
44
45 bool IsValid() const { return m_data_sp.get() != nullptr; }
46
47 void Clear() {
48 m_plugin_wp.reset();
49 m_data_sp.reset();
50 }
51
52 Status GetAsJSON(Stream &stream) const {
53 if (!m_data_sp)
54 return Status::FromErrorString("No structured data.");
55
56 llvm::json::OStream s(stream.AsRawOstream());
57 m_data_sp->Serialize(s);
58 return Status();
59 }
60
61 Status GetDescription(Stream &stream) const {
62 if (!m_data_sp)
63 return Status::FromErrorString("Cannot pretty print structured data: "
64 "no data to print.");
65
66 // Grab the plugin
68
69 // If there's no plugin, call underlying data's dump method:
70 if (!plugin_sp) {
71 if (!m_data_sp)
72 return Status::FromErrorString("No data to describe.");
73 m_data_sp->GetDescription(stream);
74 return Status();
75 }
76 // Get the data's description.
77 return plugin_sp->GetDescription(m_data_sp, stream);
78 }
79
81
83
84 void SetValueForKey(llvm::StringRef key,
85 const StructuredData::ObjectSP &value) {
86 if (!m_data_sp ||
89 } else if (StructuredData::Dictionary *dict =
90 m_data_sp->GetAsDictionary()) {
91 dict->AddItem(key, value);
92 }
93 }
94
95 void SetUnsignedIntegerValue(uint64_t value) {
97 }
98
99 void SetSignedIntegerValue(int64_t value) {
101 }
102
103 void SetFloatValue(double value) {
105 }
106
107 void SetBooleanValue(bool value) {
109 }
110
111 void SetStringValue(std::string value) {
112 m_data_sp = StructuredData::FromString(std::move(value));
113 }
114
115 void SetGenericValue(void *value) {
117 }
118
123
124 size_t GetSize() const {
125 if (!m_data_sp)
126 return 0;
127
129 auto dict = m_data_sp->GetAsDictionary();
130 return (dict->GetSize());
131 } else if (m_data_sp->GetType() == lldb::eStructuredDataTypeArray) {
132 auto array = m_data_sp->GetAsArray();
133 return (array->GetSize());
134 } else
135 return 0;
136 }
137
139 if (m_data_sp) {
140 auto dict = m_data_sp->GetAsDictionary();
141 if (dict)
142 return dict->GetValueForKey(llvm::StringRef(key));
143 }
145 }
146
148 if (m_data_sp) {
149 auto array = m_data_sp->GetAsArray();
150 if (array)
151 return array->GetItemAtIndex(idx);
152 }
154 }
155
156 uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
157 return (m_data_sp ? m_data_sp->GetUnsignedIntegerValue(fail_value)
158 : fail_value);
159 }
160
161 int64_t GetIntegerValue(int64_t fail_value = 0) const {
162 return (m_data_sp ? m_data_sp->GetSignedIntegerValue(fail_value)
163 : fail_value);
164 }
165
166 double GetFloatValue(double fail_value = 0.0) const {
167 return (m_data_sp ? m_data_sp->GetFloatValue(fail_value) : fail_value);
168 }
169
170 bool GetBooleanValue(bool fail_value = false) const {
171 return (m_data_sp ? m_data_sp->GetBooleanValue(fail_value) : fail_value);
172 }
173
174 size_t GetStringValue(char *dst, size_t dst_len) const {
175 if (!m_data_sp)
176 return 0;
177
178 llvm::StringRef result = m_data_sp->GetStringValue();
179 if (result.empty())
180 return 0;
181
182 if (!dst || !dst_len) {
183 char s[1];
184 return (::snprintf(s, 1, "%s", result.data()));
185 }
186 return (::snprintf(dst, dst_len, "%s", result.data()));
187 }
188
189 void *GetGenericValue() const {
190 if (!m_data_sp)
191 return nullptr;
192
193 StructuredData::Generic *generic_data = m_data_sp->GetAsGeneric();
194 if (!generic_data)
195 return nullptr;
196
197 return generic_data->GetValue();
198 }
199
201
202private:
205};
206} // namespace lldb_private
207#endif
This class handles one or more StructuredData::Dictionary entries that are raised for structured data...
Definition Event.h:127
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
A stream class that can stream formatted output to a file.
Definition Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:400
StructuredDataImpl(StructuredData::ObjectSP obj)
double GetFloatValue(double fail_value=0.0) const
Status GetAsJSON(Stream &stream) const
void SetSignedIntegerValue(int64_t value)
StructuredData::ObjectSP GetItemAtIndex(size_t idx) const
int64_t GetIntegerValue(int64_t fail_value=0) const
uint64_t GetIntegerValue(uint64_t fail_value=0) const
lldb::StructuredDataType GetType() const
StructuredDataImpl(const lldb::EventSP &event_sp)
void SetValueForKey(llvm::StringRef key, const StructuredData::ObjectSP &value)
StructuredData::ObjectSP m_data_sp
StructuredDataImpl & operator=(const StructuredDataImpl &rhs)=default
StructuredData::ObjectSP GetValueForKey(const char *key) const
size_t GetStringValue(char *dst, size_t dst_len) const
StructuredData::ObjectSP GetObjectSP() const
void SetObjectSP(const StructuredData::ObjectSP &obj)
void SetUnsignedIntegerValue(uint64_t value)
void SetStringValue(std::string value)
StructuredData::ObjectSP GetObjectSP()
bool GetBooleanValue(bool fail_value=false) const
lldb::StructuredDataPluginWP m_plugin_wp
Status GetDescription(Stream &stream) const
StructuredDataImpl(const StructuredDataImpl &rhs)=default
static StructuredData::ObjectSP FromString(std::string value)
static StructuredData::ObjectSP FromFloat(double value)
static ObjectSP FromInteger(T value)
std::shared_ptr< Object > ObjectSP
static StructuredData::ObjectSP FromKeyValue(llvm::StringRef key, const StructuredData::ObjectSP &value_sp)
static StructuredData::ObjectSP FromBoolean(bool value)
static StructuredData::ObjectSP FromGeneric(void *value)
A class that represents a running process on the host machine.
std::weak_ptr< lldb_private::StructuredDataPlugin > StructuredDataPluginWP
std::shared_ptr< lldb_private::StructuredDataPlugin > StructuredDataPluginSP
std::shared_ptr< lldb_private::Event > EventSP
@ eStructuredDataTypeDictionary
@ eStructuredDataTypeInvalid
@ eStructuredDataTypeArray