blob: b03b86e415be6c5e91059f9dac51fa0eb8c1e452 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBValueList.cpp -----------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Chris Lattner30fdc8d2010-06-08 16:52:24 +00009#include "lldb/API/SBValueList.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000010#include "lldb/API/SBStream.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000011#include "lldb/API/SBValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/ValueObjectList.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000013#include "lldb/Utility/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
Enrico Granata85425d72013-02-07 18:23:56 +000015#include <vector>
16
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017using namespace lldb;
18using namespace lldb_private;
19
Kate Stoneb9c1b512016-09-06 20:57:50 +000020class ValueListImpl {
Enrico Granata19f0e8c2013-04-22 22:57:56 +000021public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000022 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;
Enrico Granata19f0e8c2013-04-22 22:57:56 +000052 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 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 }
Enrico Granata19f0e8c2013-04-22 22:57:56 +000062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 return lldb::SBValue();
64 }
Enrico Granata85425d72013-02-07 18:23:56 +000065
Enrico Granata19f0e8c2013-04-22 22:57:56 +000066private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 std::vector<lldb::SBValue> m_values;
Enrico Granata19f0e8c2013-04-22 22:57:56 +000068};
Enrico Granata85425d72013-02-07 18:23:56 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070SBValueList::SBValueList() : m_opaque_ap() {}
71
72SBValueList::SBValueList(const SBValueList &rhs) : m_opaque_ap() {
73 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
74
75 if (rhs.IsValid())
76 m_opaque_ap.reset(new ValueListImpl(*rhs));
77
78 if (log) {
79 log->Printf(
80 "SBValueList::SBValueList (rhs.ap=%p) => this.ap = %p",
81 static_cast<void *>(rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL),
82 static_cast<void *>(m_opaque_ap.get()));
83 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) : m_opaque_ap() {
87 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 if (lldb_object_ptr)
90 m_opaque_ap.reset(new ValueListImpl(*lldb_object_ptr));
91
92 if (log) {
93 log->Printf("SBValueList::SBValueList (lldb_object_ptr=%p) => this.ap = %p",
94 static_cast<const void *>(lldb_object_ptr),
95 static_cast<void *>(m_opaque_ap.get()));
96 }
97}
98
99SBValueList::~SBValueList() {}
100
Jonas Devlieghere34470772018-12-20 21:02:55 +0000101bool SBValueList::IsValid() const { return (m_opaque_ap != NULL); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102
103void SBValueList::Clear() { m_opaque_ap.reset(); }
104
105const SBValueList &SBValueList::operator=(const SBValueList &rhs) {
106 if (this != &rhs) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 if (rhs.IsValid())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 m_opaque_ap.reset(new ValueListImpl(*rhs));
109 else
110 m_opaque_ap.reset();
111 }
112 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113}
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115ValueListImpl *SBValueList::operator->() { return m_opaque_ap.get(); }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117ValueListImpl &SBValueList::operator*() { return *m_opaque_ap; }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119const ValueListImpl *SBValueList::operator->() const {
120 return m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121}
122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123const ValueListImpl &SBValueList::operator*() const { return *m_opaque_ap; }
124
125void SBValueList::Append(const SBValue &val_obj) {
126 CreateIfNeeded();
127 m_opaque_ap->Append(val_obj);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128}
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) {
131 if (val_obj_sp) {
Greg Claytondea8cb42011-06-29 22:09:02 +0000132 CreateIfNeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 m_opaque_ap->Append(SBValue(val_obj_sp));
134 }
135}
136
137void SBValueList::Append(const lldb::SBValueList &value_list) {
138 if (value_list.IsValid()) {
139 CreateIfNeeded();
140 m_opaque_ap->Append(*value_list);
141 }
142}
143
144SBValue SBValueList::GetValueAtIndex(uint32_t idx) const {
145 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
146
147 // if (log)
148 // log->Printf ("SBValueList::GetValueAtIndex (uint32_t idx) idx = %d",
149 // idx);
150
151 SBValue sb_value;
Jonas Devlieghere34470772018-12-20 21:02:55 +0000152 if (m_opaque_ap)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 sb_value = m_opaque_ap->GetValueAtIndex(idx);
154
155 if (log) {
156 SBStream sstr;
157 sb_value.GetDescription(sstr);
158 log->Printf("SBValueList::GetValueAtIndex (this.ap=%p, idx=%d) => SBValue "
159 "(this.sp = %p, '%s')",
160 static_cast<void *>(m_opaque_ap.get()), idx,
161 static_cast<void *>(sb_value.GetSP().get()), sstr.GetData());
162 }
163
164 return sb_value;
165}
166
167uint32_t SBValueList::GetSize() const {
168 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
169
170 // if (log)
171 // log->Printf ("SBValueList::GetSize ()");
172
173 uint32_t size = 0;
Jonas Devlieghere34470772018-12-20 21:02:55 +0000174 if (m_opaque_ap)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 size = m_opaque_ap->GetSize();
176
177 if (log)
178 log->Printf("SBValueList::GetSize (this.ap=%p) => %d",
179 static_cast<void *>(m_opaque_ap.get()), size);
180
181 return size;
182}
183
184void SBValueList::CreateIfNeeded() {
Jonas Devlieghere34470772018-12-20 21:02:55 +0000185 if (m_opaque_ap == NULL)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 m_opaque_ap.reset(new ValueListImpl());
187}
188
189SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) {
190 SBValue sb_value;
Jonas Devlieghere34470772018-12-20 21:02:55 +0000191 if (m_opaque_ap)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 sb_value = m_opaque_ap->FindValueByUID(uid);
193 return sb_value;
194}
195
196SBValue SBValueList::GetFirstValueByName(const char *name) const {
197 SBValue sb_value;
Jonas Devlieghere34470772018-12-20 21:02:55 +0000198 if (m_opaque_ap)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 sb_value = m_opaque_ap->GetFirstValueByName(name);
200 return sb_value;
201}
202
203void *SBValueList::opaque_ptr() { return m_opaque_ap.get(); }
204
205ValueListImpl &SBValueList::ref() {
206 CreateIfNeeded();
Jonas Devlieghere34470772018-12-20 21:02:55 +0000207 return *m_opaque_ap;
Greg Claytondea8cb42011-06-29 22:09:02 +0000208}