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
33 : m_data_sp(std::move(obj)) {}
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
85 return (m_data_sp ? m_data_sp->GetType() :
87 }
88
89 size_t GetSize() const {
90 if (!m_data_sp)
91 return 0;
92
94 auto dict = m_data_sp->GetAsDictionary();
95 return (dict->GetSize());
96 } else if (m_data_sp->GetType() == lldb::eStructuredDataTypeArray) {
97 auto array = m_data_sp->GetAsArray();
98 return (array->GetSize());
99 } else
100 return 0;
101 }
102
104 if (m_data_sp) {
105 auto dict = m_data_sp->GetAsDictionary();
106 if (dict)
107 return dict->GetValueForKey(llvm::StringRef(key));
108 }
110 }
111
113 if (m_data_sp) {
114 auto array = m_data_sp->GetAsArray();
115 if (array)
116 return array->GetItemAtIndex(idx);
117 }
119 }
120
121 uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
122 return (m_data_sp ? m_data_sp->GetUnsignedIntegerValue(fail_value)
123 : fail_value);
124 }
125
126 int64_t GetIntegerValue(int64_t fail_value = 0) const {
127 return (m_data_sp ? m_data_sp->GetSignedIntegerValue(fail_value)
128 : fail_value);
129 }
130
131 double GetFloatValue(double fail_value = 0.0) const {
132 return (m_data_sp ? m_data_sp->GetFloatValue(fail_value) : fail_value);
133 }
134
135 bool GetBooleanValue(bool fail_value = false) const {
136 return (m_data_sp ? m_data_sp->GetBooleanValue(fail_value) : fail_value);
137 }
138
139 size_t GetStringValue(char *dst, size_t dst_len) const {
140 if (!m_data_sp)
141 return 0;
142
143 llvm::StringRef result = m_data_sp->GetStringValue();
144 if (result.empty())
145 return 0;
146
147 if (!dst || !dst_len) {
148 char s[1];
149 return (::snprintf(s, 1, "%s", result.data()));
150 }
151 return (::snprintf(dst, dst_len, "%s", result.data()));
152 }
153
154 void *GetGenericValue() const {
155 if (!m_data_sp)
156 return nullptr;
157
158 StructuredData::Generic *generic_data = m_data_sp->GetAsGeneric();
159 if (!generic_data)
160 return nullptr;
161
162 return generic_data->GetValue();
163 }
164
166
167private:
170};
171} // namespace lldb_private
172#endif
This class handles one or more StructuredData::Dictionary entries that are raised for structured data...
Definition: Event.h:128
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:401
StructuredDataImpl(StructuredData::ObjectSP obj)
double GetFloatValue(double fail_value=0.0) const
Status GetAsJSON(Stream &stream) const
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)
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)
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
std::shared_ptr< Object > ObjectSP
A class that represents a running process on the host machine.
std::weak_ptr< lldb_private::StructuredDataPlugin > StructuredDataPluginWP
Definition: lldb-forward.h:440
std::shared_ptr< lldb_private::StructuredDataPlugin > StructuredDataPluginSP
Definition: lldb-forward.h:438
std::shared_ptr< lldb_private::Event > EventSP
Definition: lldb-forward.h:345
@ eStructuredDataTypeDictionary
@ eStructuredDataTypeInvalid
@ eStructuredDataTypeArray