]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/StructuredDataImpl.h
Merge lldb trunk r366426, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / StructuredDataImpl.h
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 liblldb_StructuredDataImpl_h_
10 #define liblldb_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() : m_plugin_wp(), m_data_sp() {}
29
30   StructuredDataImpl(const StructuredDataImpl &rhs) = default;
31
32   StructuredDataImpl(const lldb::EventSP &event_sp)
33       : m_plugin_wp(
34             EventDataStructuredData::GetPluginFromEvent(event_sp.get())),
35         m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) {
36   }
37
38   ~StructuredDataImpl() = default;
39
40   StructuredDataImpl &operator=(const StructuredDataImpl &rhs) = default;
41
42   bool IsValid() const { return m_data_sp.get() != nullptr; }
43
44   void Clear() {
45     m_plugin_wp.reset();
46     m_data_sp.reset();
47   }
48
49   Status GetAsJSON(Stream &stream) const {
50     Status error;
51
52     if (!m_data_sp) {
53       error.SetErrorString("No structured data.");
54       return error;
55     }
56
57     m_data_sp->Dump(stream);
58     return error;
59   }
60
61   Status GetDescription(Stream &stream) const {
62     Status error;
63
64     if (!m_data_sp) {
65       error.SetErrorString("Cannot pretty print structured data: "
66                            "no data to print.");
67       return error;
68     }
69
70     // Grab the plugin.
71     auto plugin_sp = lldb::StructuredDataPluginSP(m_plugin_wp);
72     if (!plugin_sp) {
73       error.SetErrorString("Cannot pretty print structured data: "
74                            "plugin doesn't exist.");
75       return error;
76     }
77
78     // Get the data's description.
79     return plugin_sp->GetDescription(m_data_sp, stream);
80   }
81
82   StructuredData::ObjectSP GetObjectSP() { return m_data_sp; }
83
84   void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
85
86   lldb::StructuredDataType GetType() const {
87     return (m_data_sp ? m_data_sp->GetType() :
88         lldb::eStructuredDataTypeInvalid);
89   }
90
91   size_t GetSize() const {
92     if (!m_data_sp)
93       return 0;
94
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());
101     } else
102       return 0;
103   }
104
105   StructuredData::ObjectSP GetValueForKey(const char *key) const {
106     if (m_data_sp) {
107       auto dict = m_data_sp->GetAsDictionary();
108       if (dict)
109         return dict->GetValueForKey(llvm::StringRef(key));
110     }
111     return StructuredData::ObjectSP();
112   }
113
114   StructuredData::ObjectSP GetItemAtIndex(size_t idx) const {
115     if (m_data_sp) {
116       auto array = m_data_sp->GetAsArray();
117       if (array)
118         return array->GetItemAtIndex(idx);
119     }
120     return StructuredData::ObjectSP();
121   }
122
123   uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
124     return (m_data_sp ? m_data_sp->GetIntegerValue(fail_value) : fail_value);
125   }
126
127   double GetFloatValue(double fail_value = 0.0) const {
128     return (m_data_sp ? m_data_sp->GetFloatValue(fail_value) : fail_value);
129   }
130
131   bool GetBooleanValue(bool fail_value = false) const {
132     return (m_data_sp ? m_data_sp->GetBooleanValue(fail_value) : fail_value);
133   }
134
135   size_t GetStringValue(char *dst, size_t dst_len) const {
136     if (!m_data_sp)
137       return 0;
138
139     llvm::StringRef result = m_data_sp->GetStringValue();
140     if (result.empty())
141       return 0;
142
143     if (!dst || !dst_len) {
144       char s[1];
145       return (::snprintf(s, 1, "%s", result.data()));
146     }
147     return (::snprintf(dst, dst_len, "%s", result.data()));
148   }
149
150 private:
151   lldb::StructuredDataPluginWP m_plugin_wp;
152   StructuredData::ObjectSP m_data_sp;
153 };
154 } // namespace lldb_private
155 #endif