xref: /llvm-project/lldb/include/lldb/Core/StructuredDataImpl.h (revision 0642cd768b80665585c8500bed2933a3b99123dc)
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 
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"
20 
21 #pragma mark--
22 #pragma mark StructuredDataImpl
23 
24 namespace lldb_private {
25 
26 class StructuredDataImpl {
27 public:
28   StructuredDataImpl() = default;
29 
30   StructuredDataImpl(const StructuredDataImpl &rhs) = default;
31 
32   StructuredDataImpl(StructuredData::ObjectSP obj)
33       : m_data_sp(std::move(obj)) {}
34 
35   StructuredDataImpl(const lldb::EventSP &event_sp)
36       : m_plugin_wp(
37             EventDataStructuredData::GetPluginFromEvent(event_sp.get())),
38         m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) {
39   }
40 
41   ~StructuredDataImpl() = default;
42 
43   StructuredDataImpl &operator=(const StructuredDataImpl &rhs) = default;
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
67     lldb::StructuredDataPluginSP plugin_sp = m_plugin_wp.lock();
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 
80   StructuredData::ObjectSP GetObjectSP() { return m_data_sp; }
81 
82   void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
83 
84   lldb::StructuredDataType GetType() const {
85     return (m_data_sp ? m_data_sp->GetType() :
86         lldb::eStructuredDataTypeInvalid);
87   }
88 
89   size_t GetSize() const {
90     if (!m_data_sp)
91       return 0;
92 
93     if (m_data_sp->GetType() == lldb::eStructuredDataTypeDictionary) {
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 
103   StructuredData::ObjectSP GetValueForKey(const char *key) const {
104     if (m_data_sp) {
105       auto dict = m_data_sp->GetAsDictionary();
106       if (dict)
107         return dict->GetValueForKey(llvm::StringRef(key));
108     }
109     return StructuredData::ObjectSP();
110   }
111 
112   StructuredData::ObjectSP GetItemAtIndex(size_t idx) const {
113     if (m_data_sp) {
114       auto array = m_data_sp->GetAsArray();
115       if (array)
116         return array->GetItemAtIndex(idx);
117     }
118     return StructuredData::ObjectSP();
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 
165   StructuredData::ObjectSP GetObjectSP() const { return m_data_sp; }
166 
167 private:
168   lldb::StructuredDataPluginWP m_plugin_wp;
169   StructuredData::ObjectSP m_data_sp;
170 };
171 } // namespace lldb_private
172 #endif
173