1 //===-- StructuredDataImpl.h ------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_StructuredDataImpl_h_
10 #define liblldb_StructuredDataImpl_h_
12 #include "lldb/Target/StructuredDataPlugin.h"
13 #include "lldb/Utility/Event.h"
14 #include "lldb/Utility/Status.h"
15 #include "lldb/Utility/Stream.h"
16 #include "lldb/Utility/StructuredData.h"
17 #include "lldb/lldb-enumerations.h"
18 #include "lldb/lldb-forward.h"
19 #include "llvm/ADT/StringRef.h"
22 #pragma mark StructuredDataImpl
24 namespace lldb_private {
26 class StructuredDataImpl {
28 StructuredDataImpl() : m_plugin_wp(), m_data_sp() {}
30 StructuredDataImpl(const StructuredDataImpl &rhs) = default;
32 StructuredDataImpl(const lldb::EventSP &event_sp)
34 EventDataStructuredData::GetPluginFromEvent(event_sp.get())),
35 m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) {
38 ~StructuredDataImpl() = default;
40 StructuredDataImpl &operator=(const StructuredDataImpl &rhs) = default;
42 bool IsValid() const { return m_data_sp.get() != nullptr; }
49 Status GetAsJSON(Stream &stream) const {
53 error.SetErrorString("No structured data.");
57 m_data_sp->Dump(stream);
61 Status GetDescription(Stream &stream) const {
65 error.SetErrorString("Cannot pretty print structured data: "
71 auto plugin_sp = lldb::StructuredDataPluginSP(m_plugin_wp);
73 error.SetErrorString("Cannot pretty print structured data: "
74 "plugin doesn't exist.");
78 // Get the data's description.
79 return plugin_sp->GetDescription(m_data_sp, stream);
82 StructuredData::ObjectSP GetObjectSP() { return m_data_sp; }
84 void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
86 lldb::StructuredDataType GetType() const {
87 return (m_data_sp ? m_data_sp->GetType() :
88 lldb::eStructuredDataTypeInvalid);
91 size_t GetSize() const {
95 if (m_data_sp->GetType() == lldb::eStructuredDataTypeDictionary) {
96 auto dict = m_data_sp->GetAsDictionary();
97 return (dict->GetSize());
98 } else if (m_data_sp->GetType() == lldb::eStructuredDataTypeArray) {
99 auto array = m_data_sp->GetAsArray();
100 return (array->GetSize());
105 StructuredData::ObjectSP GetValueForKey(const char *key) const {
107 auto dict = m_data_sp->GetAsDictionary();
109 return dict->GetValueForKey(llvm::StringRef(key));
111 return StructuredData::ObjectSP();
114 StructuredData::ObjectSP GetItemAtIndex(size_t idx) const {
116 auto array = m_data_sp->GetAsArray();
118 return array->GetItemAtIndex(idx);
120 return StructuredData::ObjectSP();
123 uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
124 return (m_data_sp ? m_data_sp->GetIntegerValue(fail_value) : fail_value);
127 double GetFloatValue(double fail_value = 0.0) const {
128 return (m_data_sp ? m_data_sp->GetFloatValue(fail_value) : fail_value);
131 bool GetBooleanValue(bool fail_value = false) const {
132 return (m_data_sp ? m_data_sp->GetBooleanValue(fail_value) : fail_value);
135 size_t GetStringValue(char *dst, size_t dst_len) const {
139 llvm::StringRef result = m_data_sp->GetStringValue();
143 if (!dst || !dst_len) {
145 return (::snprintf(s, 1, "%s", result.data()));
147 return (::snprintf(dst, dst_len, "%s", result.data()));
151 lldb::StructuredDataPluginWP m_plugin_wp;
152 StructuredData::ObjectSP m_data_sp;
154 } // namespace lldb_private