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"
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 
27 public:
28  StructuredDataImpl() = default;
29 
30  StructuredDataImpl(const StructuredDataImpl &rhs) = default;
31 
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  Status error;
54 
55  if (!m_data_sp) {
56  error.SetErrorString("No structured data.");
57  return error;
58  }
59 
60  llvm::json::OStream s(stream.AsRawOstream());
61  m_data_sp->Serialize(s);
62  return error;
63  }
64 
65  Status GetDescription(Stream &stream) const {
66  Status error;
67 
68  if (!m_data_sp) {
69  error.SetErrorString("Cannot pretty print structured data: "
70  "no data to print.");
71  return error;
72  }
73 
74  // Grab the plugin
75  lldb::StructuredDataPluginSP plugin_sp = m_plugin_wp.lock();
76 
77  // If there's no plugin, call underlying data's dump method:
78  if (!plugin_sp) {
79  if (!m_data_sp) {
80  error.SetErrorString("No data to describe.");
81  return error;
82  }
83  m_data_sp->Dump(stream, true);
84  return error;
85  }
86  // Get the data's description.
87  return plugin_sp->GetDescription(m_data_sp, stream);
88  }
89 
91 
92  void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
93 
95  return (m_data_sp ? m_data_sp->GetType() :
97  }
98 
99  size_t GetSize() const {
100  if (!m_data_sp)
101  return 0;
102 
104  auto dict = m_data_sp->GetAsDictionary();
105  return (dict->GetSize());
106  } else if (m_data_sp->GetType() == lldb::eStructuredDataTypeArray) {
107  auto array = m_data_sp->GetAsArray();
108  return (array->GetSize());
109  } else
110  return 0;
111  }
112 
113  StructuredData::ObjectSP GetValueForKey(const char *key) const {
114  if (m_data_sp) {
115  auto dict = m_data_sp->GetAsDictionary();
116  if (dict)
117  return dict->GetValueForKey(llvm::StringRef(key));
118  }
119  return StructuredData::ObjectSP();
120  }
121 
123  if (m_data_sp) {
124  auto array = m_data_sp->GetAsArray();
125  if (array)
126  return array->GetItemAtIndex(idx);
127  }
128  return StructuredData::ObjectSP();
129  }
130 
131  uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
132  return (m_data_sp ? m_data_sp->GetIntegerValue(fail_value) : fail_value);
133  }
134 
135  double GetFloatValue(double fail_value = 0.0) const {
136  return (m_data_sp ? m_data_sp->GetFloatValue(fail_value) : fail_value);
137  }
138 
139  bool GetBooleanValue(bool fail_value = false) const {
140  return (m_data_sp ? m_data_sp->GetBooleanValue(fail_value) : fail_value);
141  }
142 
143  size_t GetStringValue(char *dst, size_t dst_len) const {
144  if (!m_data_sp)
145  return 0;
146 
147  llvm::StringRef result = m_data_sp->GetStringValue();
148  if (result.empty())
149  return 0;
150 
151  if (!dst || !dst_len) {
152  char s[1];
153  return (::snprintf(s, 1, "%s", result.data()));
154  }
155  return (::snprintf(dst, dst_len, "%s", result.data()));
156  }
157 
159 
160 private:
161  lldb::StructuredDataPluginWP m_plugin_wp;
163 };
164 } // namespace lldb_private
165 #endif
lldb::StructuredDataType
StructuredDataType
Definition: lldb-enumerations.h:792
lldb_private::StructuredDataImpl::m_data_sp
StructuredData::ObjectSP m_data_sp
Definition: StructuredDataImpl.h:162
lldb_private::StructuredDataImpl::GetValueForKey
StructuredData::ObjectSP GetValueForKey(const char *key) const
Definition: StructuredDataImpl.h:113
StructuredData.h
lldb_private::StructuredDataImpl::GetIntegerValue
uint64_t GetIntegerValue(uint64_t fail_value=0) const
Definition: StructuredDataImpl.h:131
lldb::eStructuredDataTypeDictionary
@ eStructuredDataTypeDictionary
Definition: lldb-enumerations.h:801
lldb_private::StructuredDataImpl::GetDescription
Status GetDescription(Stream &stream) const
Definition: StructuredDataImpl.h:65
lldb_private::StructuredDataImpl::GetObjectSP
StructuredData::ObjectSP GetObjectSP()
Definition: StructuredDataImpl.h:90
lldb_private::StructuredDataImpl::GetObjectSP
StructuredData::ObjectSP GetObjectSP() const
Definition: StructuredDataImpl.h:158
lldb_private::StructuredDataImpl::GetAsJSON
Status GetAsJSON(Stream &stream) const
Definition: StructuredDataImpl.h:52
lldb_private::StructuredDataImpl::operator=
StructuredDataImpl & operator=(const StructuredDataImpl &rhs)=default
lldb_private::StructuredDataImpl::GetSize
size_t GetSize() const
Definition: StructuredDataImpl.h:99
lldb_private::Stream
Definition: Stream.h:28
lldb::eStructuredDataTypeInvalid
@ eStructuredDataTypeInvalid
Definition: lldb-enumerations.h:793
lldb_private::StructuredDataImpl::~StructuredDataImpl
~StructuredDataImpl()=default
lldb_private::StructuredDataImpl::GetType
lldb::StructuredDataType GetType() const
Definition: StructuredDataImpl.h:94
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::StructuredDataImpl::GetItemAtIndex
StructuredData::ObjectSP GetItemAtIndex(size_t idx) const
Definition: StructuredDataImpl.h:122
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
StructuredDataPlugin.h
lldb-enumerations.h
lldb_private::StructuredDataImpl
Definition: StructuredDataImpl.h:26
lldb_private::StructuredDataImpl::GetStringValue
size_t GetStringValue(char *dst, size_t dst_len) const
Definition: StructuredDataImpl.h:143
lldb_private::StructuredDataImpl::SetObjectSP
void SetObjectSP(const StructuredData::ObjectSP &obj)
Definition: StructuredDataImpl.h:92
Event.h
lldb_private::Status
Definition: Status.h:44
lldb_private::StructuredDataImpl::StructuredDataImpl
StructuredDataImpl(const lldb::EventSP &event_sp)
Definition: StructuredDataImpl.h:35
lldb_private::Stream::AsRawOstream
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
lldb-forward.h
Status.h
lldb_private::StructuredDataImpl::IsValid
bool IsValid() const
Definition: StructuredDataImpl.h:45
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::StructuredDataImpl::StructuredDataImpl
StructuredDataImpl(StructuredData::ObjectSP obj)
Definition: StructuredDataImpl.h:32
lldb::eStructuredDataTypeArray
@ eStructuredDataTypeArray
Definition: lldb-enumerations.h:796
Stream.h
lldb_private::StructuredDataImpl::Clear
void Clear()
Definition: StructuredDataImpl.h:47
lldb_private::StructuredDataImpl::StructuredDataImpl
StructuredDataImpl()=default
lldb_private::StructuredDataImpl::GetBooleanValue
bool GetBooleanValue(bool fail_value=false) const
Definition: StructuredDataImpl.h:139
lldb_private::StructuredDataImpl::GetFloatValue
double GetFloatValue(double fail_value=0.0) const
Definition: StructuredDataImpl.h:135
lldb_private::EventDataStructuredData
This class handles one or more StructuredData::Dictionary entries that are raised for structured data...
Definition: Event.h:130
lldb_private::StructuredDataImpl::m_plugin_wp
lldb::StructuredDataPluginWP m_plugin_wp
Definition: StructuredDataImpl.h:161