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