1 //===-- SBValueList.cpp -----------------------------------------*- 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 #include "lldb/API/SBValueList.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/API/SBValue.h"
13 #include "lldb/Core/ValueObjectList.h"
14 
15 #include <vector>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 class ValueListImpl {
21 public:
22   ValueListImpl() : m_values() {}
23 
24   ValueListImpl(const ValueListImpl &rhs) : m_values(rhs.m_values) {}
25 
26   ValueListImpl &operator=(const ValueListImpl &rhs) {
27     if (this == &rhs)
28       return *this;
29     m_values = rhs.m_values;
30     return *this;
31   }
32 
33   uint32_t GetSize() { return m_values.size(); }
34 
35   void Append(const lldb::SBValue &sb_value) { m_values.push_back(sb_value); }
36 
37   void Append(const ValueListImpl &list) {
38     for (auto val : list.m_values)
39       Append(val);
40   }
41 
42   lldb::SBValue GetValueAtIndex(uint32_t index) {
43     if (index >= GetSize())
44       return lldb::SBValue();
45     return m_values[index];
46   }
47 
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 
56   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 
70 SBValueList::SBValueList() : m_opaque_up() {
71   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBValueList);
72 }
73 
74 SBValueList::SBValueList(const SBValueList &rhs) : m_opaque_up() {
75   LLDB_RECORD_CONSTRUCTOR(SBValueList, (const lldb::SBValueList &), rhs);
76 
77   if (rhs.IsValid())
78     m_opaque_up.reset(new ValueListImpl(*rhs));
79 }
80 
81 SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) : m_opaque_up() {
82   if (lldb_object_ptr)
83     m_opaque_up.reset(new ValueListImpl(*lldb_object_ptr));
84 }
85 
86 SBValueList::~SBValueList() {}
87 
88 bool SBValueList::IsValid() const {
89   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBValueList, IsValid);
90 
91   return (m_opaque_up != NULL);
92 }
93 
94 void SBValueList::Clear() {
95   LLDB_RECORD_METHOD_NO_ARGS(void, SBValueList, Clear);
96 
97   m_opaque_up.reset();
98 }
99 
100 const SBValueList &SBValueList::operator=(const SBValueList &rhs) {
101   LLDB_RECORD_METHOD(const lldb::SBValueList &,
102                      SBValueList, operator=,(const lldb::SBValueList &), rhs);
103 
104   if (this != &rhs) {
105     if (rhs.IsValid())
106       m_opaque_up.reset(new ValueListImpl(*rhs));
107     else
108       m_opaque_up.reset();
109   }
110   return *this;
111 }
112 
113 ValueListImpl *SBValueList::operator->() { return m_opaque_up.get(); }
114 
115 ValueListImpl &SBValueList::operator*() { return *m_opaque_up; }
116 
117 const ValueListImpl *SBValueList::operator->() const {
118   return m_opaque_up.get();
119 }
120 
121 const ValueListImpl &SBValueList::operator*() const { return *m_opaque_up; }
122 
123 void SBValueList::Append(const SBValue &val_obj) {
124   LLDB_RECORD_METHOD(void, SBValueList, Append, (const lldb::SBValue &),
125                      val_obj);
126 
127   CreateIfNeeded();
128   m_opaque_up->Append(val_obj);
129 }
130 
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 
138 void SBValueList::Append(const lldb::SBValueList &value_list) {
139   LLDB_RECORD_METHOD(void, SBValueList, Append, (const lldb::SBValueList &),
140                      value_list);
141 
142   if (value_list.IsValid()) {
143     CreateIfNeeded();
144     m_opaque_up->Append(*value_list);
145   }
146 }
147 
148 SBValue SBValueList::GetValueAtIndex(uint32_t idx) const {
149   LLDB_RECORD_METHOD_CONST(lldb::SBValue, SBValueList, GetValueAtIndex,
150                            (uint32_t), idx);
151 
152 
153   SBValue sb_value;
154   if (m_opaque_up)
155     sb_value = m_opaque_up->GetValueAtIndex(idx);
156 
157   return LLDB_RECORD_RESULT(sb_value);
158 }
159 
160 uint32_t SBValueList::GetSize() const {
161   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBValueList, GetSize);
162 
163   uint32_t size = 0;
164   if (m_opaque_up)
165     size = m_opaque_up->GetSize();
166 
167   return size;
168 }
169 
170 void SBValueList::CreateIfNeeded() {
171   if (m_opaque_up == NULL)
172     m_opaque_up.reset(new ValueListImpl());
173 }
174 
175 SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) {
176   LLDB_RECORD_METHOD(lldb::SBValue, SBValueList, FindValueObjectByUID,
177                      (lldb::user_id_t), uid);
178 
179   SBValue sb_value;
180   if (m_opaque_up)
181     sb_value = m_opaque_up->FindValueByUID(uid);
182   return LLDB_RECORD_RESULT(sb_value);
183 }
184 
185 SBValue SBValueList::GetFirstValueByName(const char *name) const {
186   LLDB_RECORD_METHOD_CONST(lldb::SBValue, SBValueList, GetFirstValueByName,
187                            (const char *), name);
188 
189   SBValue sb_value;
190   if (m_opaque_up)
191     sb_value = m_opaque_up->GetFirstValueByName(name);
192   return LLDB_RECORD_RESULT(sb_value);
193 }
194 
195 void *SBValueList::opaque_ptr() { return m_opaque_up.get(); }
196 
197 ValueListImpl &SBValueList::ref() {
198   CreateIfNeeded();
199   return *m_opaque_up;
200 }
201