1 //===-- SBValueList.cpp ---------------------------------------------------===//
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 #include "lldb/API/SBValueList.h"
10 #include "lldb/API/SBStream.h"
11 #include "lldb/API/SBValue.h"
12 #include "lldb/Core/ValueObjectList.h"
13 #include "lldb/Utility/Instrumentation.h"
14 
15 #include <vector>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 class ValueListImpl {
21 public:
22   ValueListImpl() = default;
23 
24   ValueListImpl(const ValueListImpl &rhs) = default;
25 
operator =(const ValueListImpl & rhs)26   ValueListImpl &operator=(const ValueListImpl &rhs) {
27     if (this == &rhs)
28       return *this;
29     m_values = rhs.m_values;
30     return *this;
31   }
32 
GetSize()33   uint32_t GetSize() { return m_values.size(); }
34 
Append(const lldb::SBValue & sb_value)35   void Append(const lldb::SBValue &sb_value) { m_values.push_back(sb_value); }
36 
Append(const ValueListImpl & list)37   void Append(const ValueListImpl &list) {
38     for (auto val : list.m_values)
39       Append(val);
40   }
41 
GetValueAtIndex(uint32_t index)42   lldb::SBValue GetValueAtIndex(uint32_t index) {
43     if (index >= GetSize())
44       return lldb::SBValue();
45     return m_values[index];
46   }
47 
FindValueByUID(lldb::user_id_t uid)48   lldb::SBValue FindValueByUID(lldb::user_id_t uid) {
49     for (auto val : m_values) {
50       if (val.IsValid() && val.GetID() == uid)
51         return val;
52     }
53     return lldb::SBValue();
54   }
55 
GetFirstValueByName(const char * name) const56   lldb::SBValue GetFirstValueByName(const char *name) const {
57     if (name) {
58       for (auto val : m_values) {
59         if (val.IsValid() && val.GetName() && strcmp(name, val.GetName()) == 0)
60           return val;
61       }
62     }
63     return lldb::SBValue();
64   }
65 
66 private:
67   std::vector<lldb::SBValue> m_values;
68 };
69 
SBValueList()70 SBValueList::SBValueList() { LLDB_INSTRUMENT_VA(this); }
71 
SBValueList(const SBValueList & rhs)72 SBValueList::SBValueList(const SBValueList &rhs) {
73   LLDB_INSTRUMENT_VA(this, rhs);
74 
75   if (rhs.IsValid())
76     m_opaque_up = std::make_unique<ValueListImpl>(*rhs);
77 }
78 
SBValueList(const ValueListImpl * lldb_object_ptr)79 SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) {
80   if (lldb_object_ptr)
81     m_opaque_up = std::make_unique<ValueListImpl>(*lldb_object_ptr);
82 }
83 
84 SBValueList::~SBValueList() = default;
85 
IsValid() const86 bool SBValueList::IsValid() const {
87   LLDB_INSTRUMENT_VA(this);
88   return this->operator bool();
89 }
operator bool() const90 SBValueList::operator bool() const {
91   LLDB_INSTRUMENT_VA(this);
92 
93   return (m_opaque_up != nullptr);
94 }
95 
Clear()96 void SBValueList::Clear() {
97   LLDB_INSTRUMENT_VA(this);
98 
99   m_opaque_up.reset();
100 }
101 
operator =(const SBValueList & rhs)102 const SBValueList &SBValueList::operator=(const SBValueList &rhs) {
103   LLDB_INSTRUMENT_VA(this, rhs);
104 
105   if (this != &rhs) {
106     if (rhs.IsValid())
107       m_opaque_up = std::make_unique<ValueListImpl>(*rhs);
108     else
109       m_opaque_up.reset();
110   }
111   return *this;
112 }
113 
operator ->()114 ValueListImpl *SBValueList::operator->() { return m_opaque_up.get(); }
115 
operator *()116 ValueListImpl &SBValueList::operator*() { return *m_opaque_up; }
117 
operator ->() const118 const ValueListImpl *SBValueList::operator->() const {
119   return m_opaque_up.get();
120 }
121 
operator *() const122 const ValueListImpl &SBValueList::operator*() const { return *m_opaque_up; }
123 
Append(const SBValue & val_obj)124 void SBValueList::Append(const SBValue &val_obj) {
125   LLDB_INSTRUMENT_VA(this, val_obj);
126 
127   CreateIfNeeded();
128   m_opaque_up->Append(val_obj);
129 }
130 
Append(lldb::ValueObjectSP & val_obj_sp)131 void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) {
132   if (val_obj_sp) {
133     CreateIfNeeded();
134     m_opaque_up->Append(SBValue(val_obj_sp));
135   }
136 }
137 
Append(const lldb::SBValueList & value_list)138 void SBValueList::Append(const lldb::SBValueList &value_list) {
139   LLDB_INSTRUMENT_VA(this, value_list);
140 
141   if (value_list.IsValid()) {
142     CreateIfNeeded();
143     m_opaque_up->Append(*value_list);
144   }
145 }
146 
GetValueAtIndex(uint32_t idx) const147 SBValue SBValueList::GetValueAtIndex(uint32_t idx) const {
148   LLDB_INSTRUMENT_VA(this, idx);
149 
150   SBValue sb_value;
151   if (m_opaque_up)
152     sb_value = m_opaque_up->GetValueAtIndex(idx);
153 
154   return sb_value;
155 }
156 
GetSize() const157 uint32_t SBValueList::GetSize() const {
158   LLDB_INSTRUMENT_VA(this);
159 
160   uint32_t size = 0;
161   if (m_opaque_up)
162     size = m_opaque_up->GetSize();
163 
164   return size;
165 }
166 
CreateIfNeeded()167 void SBValueList::CreateIfNeeded() {
168   if (m_opaque_up == nullptr)
169     m_opaque_up = std::make_unique<ValueListImpl>();
170 }
171 
FindValueObjectByUID(lldb::user_id_t uid)172 SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) {
173   LLDB_INSTRUMENT_VA(this, uid);
174 
175   SBValue sb_value;
176   if (m_opaque_up)
177     sb_value = m_opaque_up->FindValueByUID(uid);
178   return sb_value;
179 }
180 
GetFirstValueByName(const char * name) const181 SBValue SBValueList::GetFirstValueByName(const char *name) const {
182   LLDB_INSTRUMENT_VA(this, name);
183 
184   SBValue sb_value;
185   if (m_opaque_up)
186     sb_value = m_opaque_up->GetFirstValueByName(name);
187   return sb_value;
188 }
189 
opaque_ptr()190 void *SBValueList::opaque_ptr() { return m_opaque_up.get(); }
191 
ref()192 ValueListImpl &SBValueList::ref() {
193   CreateIfNeeded();
194   return *m_opaque_up;
195 }
196