blob: 16289d9a9a964f0d12534661d89b9e770fe3aef8 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBValueList.cpp -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010#include "lldb/API/SBValueList.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000011#include "lldb/API/SBStream.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000012#include "lldb/API/SBValue.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000013#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/ValueObjectList.h"
15
Enrico Granata85425d72013-02-07 18:23:56 +000016#include <vector>
17
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018using namespace lldb;
19using namespace lldb_private;
20
Kate Stoneb9c1b512016-09-06 20:57:50 +000021class ValueListImpl {
Enrico Granata19f0e8c2013-04-22 22:57:56 +000022public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000023 ValueListImpl() : m_values() {}
24
25 ValueListImpl(const ValueListImpl &rhs) : m_values(rhs.m_values) {}
26
27 ValueListImpl &operator=(const ValueListImpl &rhs) {
28 if (this == &rhs)
29 return *this;
30 m_values = rhs.m_values;
31 return *this;
32 }
33
34 uint32_t GetSize() { return m_values.size(); }
35
36 void Append(const lldb::SBValue &sb_value) { m_values.push_back(sb_value); }
37
38 void Append(const ValueListImpl &list) {
39 for (auto val : list.m_values)
40 Append(val);
41 }
42
43 lldb::SBValue GetValueAtIndex(uint32_t index) {
44 if (index >= GetSize())
45 return lldb::SBValue();
46 return m_values[index];
47 }
48
49 lldb::SBValue FindValueByUID(lldb::user_id_t uid) {
50 for (auto val : m_values) {
51 if (val.IsValid() && val.GetID() == uid)
52 return val;
Enrico Granata19f0e8c2013-04-22 22:57:56 +000053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 return lldb::SBValue();
55 }
56
57 lldb::SBValue GetFirstValueByName(const char *name) const {
58 if (name) {
59 for (auto val : m_values) {
60 if (val.IsValid() && val.GetName() && strcmp(name, val.GetName()) == 0)
61 return val;
62 }
Enrico Granata19f0e8c2013-04-22 22:57:56 +000063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 return lldb::SBValue();
65 }
Enrico Granata85425d72013-02-07 18:23:56 +000066
Enrico Granata19f0e8c2013-04-22 22:57:56 +000067private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 std::vector<lldb::SBValue> m_values;
Enrico Granata19f0e8c2013-04-22 22:57:56 +000069};
Enrico Granata85425d72013-02-07 18:23:56 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071SBValueList::SBValueList() : m_opaque_ap() {}
72
73SBValueList::SBValueList(const SBValueList &rhs) : m_opaque_ap() {
74 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
75
76 if (rhs.IsValid())
77 m_opaque_ap.reset(new ValueListImpl(*rhs));
78
79 if (log) {
80 log->Printf(
81 "SBValueList::SBValueList (rhs.ap=%p) => this.ap = %p",
82 static_cast<void *>(rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL),
83 static_cast<void *>(m_opaque_ap.get()));
84 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) : m_opaque_ap() {
88 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 if (lldb_object_ptr)
91 m_opaque_ap.reset(new ValueListImpl(*lldb_object_ptr));
92
93 if (log) {
94 log->Printf("SBValueList::SBValueList (lldb_object_ptr=%p) => this.ap = %p",
95 static_cast<const void *>(lldb_object_ptr),
96 static_cast<void *>(m_opaque_ap.get()));
97 }
98}
99
100SBValueList::~SBValueList() {}
101
102bool SBValueList::IsValid() const { return (m_opaque_ap.get() != NULL); }
103
104void SBValueList::Clear() { m_opaque_ap.reset(); }
105
106const SBValueList &SBValueList::operator=(const SBValueList &rhs) {
107 if (this != &rhs) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 if (rhs.IsValid())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 m_opaque_ap.reset(new ValueListImpl(*rhs));
110 else
111 m_opaque_ap.reset();
112 }
113 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114}
115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116ValueListImpl *SBValueList::operator->() { return m_opaque_ap.get(); }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118ValueListImpl &SBValueList::operator*() { return *m_opaque_ap; }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120const ValueListImpl *SBValueList::operator->() const {
121 return m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124const ValueListImpl &SBValueList::operator*() const { return *m_opaque_ap; }
125
126void SBValueList::Append(const SBValue &val_obj) {
127 CreateIfNeeded();
128 m_opaque_ap->Append(val_obj);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129}
130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) {
132 if (val_obj_sp) {
Greg Claytondea8cb42011-06-29 22:09:02 +0000133 CreateIfNeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 m_opaque_ap->Append(SBValue(val_obj_sp));
135 }
136}
137
138void SBValueList::Append(const lldb::SBValueList &value_list) {
139 if (value_list.IsValid()) {
140 CreateIfNeeded();
141 m_opaque_ap->Append(*value_list);
142 }
143}
144
145SBValue SBValueList::GetValueAtIndex(uint32_t idx) const {
146 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
147
148 // if (log)
149 // log->Printf ("SBValueList::GetValueAtIndex (uint32_t idx) idx = %d",
150 // idx);
151
152 SBValue sb_value;
153 if (m_opaque_ap.get())
154 sb_value = m_opaque_ap->GetValueAtIndex(idx);
155
156 if (log) {
157 SBStream sstr;
158 sb_value.GetDescription(sstr);
159 log->Printf("SBValueList::GetValueAtIndex (this.ap=%p, idx=%d) => SBValue "
160 "(this.sp = %p, '%s')",
161 static_cast<void *>(m_opaque_ap.get()), idx,
162 static_cast<void *>(sb_value.GetSP().get()), sstr.GetData());
163 }
164
165 return sb_value;
166}
167
168uint32_t SBValueList::GetSize() const {
169 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
170
171 // if (log)
172 // log->Printf ("SBValueList::GetSize ()");
173
174 uint32_t size = 0;
175 if (m_opaque_ap.get())
176 size = m_opaque_ap->GetSize();
177
178 if (log)
179 log->Printf("SBValueList::GetSize (this.ap=%p) => %d",
180 static_cast<void *>(m_opaque_ap.get()), size);
181
182 return size;
183}
184
185void SBValueList::CreateIfNeeded() {
186 if (m_opaque_ap.get() == NULL)
187 m_opaque_ap.reset(new ValueListImpl());
188}
189
190SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) {
191 SBValue sb_value;
192 if (m_opaque_ap.get())
193 sb_value = m_opaque_ap->FindValueByUID(uid);
194 return sb_value;
195}
196
197SBValue SBValueList::GetFirstValueByName(const char *name) const {
198 SBValue sb_value;
199 if (m_opaque_ap.get())
200 sb_value = m_opaque_ap->GetFirstValueByName(name);
201 return sb_value;
202}
203
204void *SBValueList::opaque_ptr() { return m_opaque_ap.get(); }
205
206ValueListImpl &SBValueList::ref() {
207 CreateIfNeeded();
208 return *m_opaque_ap.get();
Greg Claytondea8cb42011-06-29 22:09:02 +0000209}