1 //===-- 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
10
11 #include "lldb/API/SBValueList.h"
12 #include "lldb/API/SBValue.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/ValueObjectList.h"
16
17 #include <vector>
18
19 using namespace lldb;
20 using namespace lldb_private;
21
22 class ValueListImpl
23 {
24 public:
ValueListImpl()25 ValueListImpl () :
26 m_values()
27 {
28 }
29
ValueListImpl(const ValueListImpl & rhs)30 ValueListImpl (const ValueListImpl& rhs) :
31 m_values(rhs.m_values)
32 {
33 }
34
35 ValueListImpl&
operator =(const ValueListImpl & rhs)36 operator = (const ValueListImpl& rhs)
37 {
38 if (this == &rhs)
39 return *this;
40 m_values = rhs.m_values;
41 return *this;
42 };
43
44 uint32_t
GetSize()45 GetSize ()
46 {
47 return m_values.size();
48 }
49
50 void
Append(const lldb::SBValue & sb_value)51 Append (const lldb::SBValue& sb_value)
52 {
53 m_values.push_back(sb_value);
54 }
55
56 void
Append(const ValueListImpl & list)57 Append (const ValueListImpl& list)
58 {
59 for (auto val : list.m_values)
60 Append (val);
61 }
62
63 lldb::SBValue
GetValueAtIndex(uint32_t index)64 GetValueAtIndex (uint32_t index)
65 {
66 if (index >= GetSize())
67 return lldb::SBValue();
68 return m_values[index];
69 }
70
71 lldb::SBValue
FindValueByUID(lldb::user_id_t uid)72 FindValueByUID (lldb::user_id_t uid)
73 {
74 for (auto val : m_values)
75 {
76 if (val.IsValid() && val.GetID() == uid)
77 return val;
78 }
79 return lldb::SBValue();
80 }
81
82 private:
83 std::vector<lldb::SBValue> m_values;
84 };
85
SBValueList()86 SBValueList::SBValueList () :
87 m_opaque_ap ()
88 {
89 }
90
SBValueList(const SBValueList & rhs)91 SBValueList::SBValueList (const SBValueList &rhs) :
92 m_opaque_ap ()
93 {
94 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
95
96 if (rhs.IsValid())
97 m_opaque_ap.reset (new ValueListImpl (*rhs));
98
99 if (log)
100 {
101 log->Printf ("SBValueList::SBValueList (rhs.ap=%p) => this.ap = %p",
102 (rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL),
103 m_opaque_ap.get());
104 }
105 }
106
SBValueList(const ValueListImpl * lldb_object_ptr)107 SBValueList::SBValueList (const ValueListImpl *lldb_object_ptr) :
108 m_opaque_ap ()
109 {
110 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
111
112 if (lldb_object_ptr)
113 m_opaque_ap.reset (new ValueListImpl (*lldb_object_ptr));
114
115 if (log)
116 {
117 log->Printf ("SBValueList::SBValueList (lldb_object_ptr=%p) => this.ap = %p",
118 lldb_object_ptr,
119 m_opaque_ap.get());
120 }
121 }
122
~SBValueList()123 SBValueList::~SBValueList ()
124 {
125 }
126
127 bool
IsValid() const128 SBValueList::IsValid () const
129 {
130 return (m_opaque_ap.get() != NULL);
131 }
132
133 void
Clear()134 SBValueList::Clear()
135 {
136 m_opaque_ap.reset();
137 }
138
139 const SBValueList &
operator =(const SBValueList & rhs)140 SBValueList::operator = (const SBValueList &rhs)
141 {
142 if (this != &rhs)
143 {
144 if (rhs.IsValid())
145 m_opaque_ap.reset (new ValueListImpl (*rhs));
146 else
147 m_opaque_ap.reset ();
148 }
149 return *this;
150 }
151
152 ValueListImpl *
operator ->()153 SBValueList::operator->()
154 {
155 return m_opaque_ap.get();
156 }
157
158 ValueListImpl &
operator *()159 SBValueList::operator*()
160 {
161 return *m_opaque_ap;
162 }
163
164 const ValueListImpl *
operator ->() const165 SBValueList::operator->() const
166 {
167 return m_opaque_ap.get();
168 }
169
170 const ValueListImpl &
operator *() const171 SBValueList::operator*() const
172 {
173 return *m_opaque_ap;
174 }
175
176 void
Append(const SBValue & val_obj)177 SBValueList::Append (const SBValue &val_obj)
178 {
179 CreateIfNeeded ();
180 m_opaque_ap->Append (val_obj);
181 }
182
183 void
Append(lldb::ValueObjectSP & val_obj_sp)184 SBValueList::Append (lldb::ValueObjectSP& val_obj_sp)
185 {
186 if (val_obj_sp)
187 {
188 CreateIfNeeded ();
189 m_opaque_ap->Append (SBValue(val_obj_sp));
190 }
191 }
192
193 void
Append(const lldb::SBValueList & value_list)194 SBValueList::Append (const lldb::SBValueList& value_list)
195 {
196 if (value_list.IsValid())
197 {
198 CreateIfNeeded ();
199 m_opaque_ap->Append (*value_list);
200 }
201 }
202
203
204 SBValue
GetValueAtIndex(uint32_t idx) const205 SBValueList::GetValueAtIndex (uint32_t idx) const
206 {
207 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
208
209 //if (log)
210 // log->Printf ("SBValueList::GetValueAtIndex (uint32_t idx) idx = %d", idx);
211
212 SBValue sb_value;
213 if (m_opaque_ap.get())
214 sb_value = m_opaque_ap->GetValueAtIndex (idx);
215
216 if (log)
217 {
218 SBStream sstr;
219 sb_value.GetDescription (sstr);
220 log->Printf ("SBValueList::GetValueAtIndex (this.ap=%p, idx=%d) => SBValue (this.sp = %p, '%s')",
221 m_opaque_ap.get(), idx, sb_value.GetSP().get(), sstr.GetData());
222 }
223
224 return sb_value;
225 }
226
227 uint32_t
GetSize() const228 SBValueList::GetSize () const
229 {
230 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
231
232 //if (log)
233 // log->Printf ("SBValueList::GetSize ()");
234
235 uint32_t size = 0;
236 if (m_opaque_ap.get())
237 size = m_opaque_ap->GetSize();
238
239 if (log)
240 log->Printf ("SBValueList::GetSize (this.ap=%p) => %d", m_opaque_ap.get(), size);
241
242 return size;
243 }
244
245 void
CreateIfNeeded()246 SBValueList::CreateIfNeeded ()
247 {
248 if (m_opaque_ap.get() == NULL)
249 m_opaque_ap.reset (new ValueListImpl());
250 }
251
252
253 SBValue
FindValueObjectByUID(lldb::user_id_t uid)254 SBValueList::FindValueObjectByUID (lldb::user_id_t uid)
255 {
256 SBValue sb_value;
257 if (m_opaque_ap.get())
258 sb_value = m_opaque_ap->FindValueByUID(uid);
259 return sb_value;
260 }
261
262 void *
opaque_ptr()263 SBValueList::opaque_ptr ()
264 {
265 return m_opaque_ap.get();
266 }
267
268 ValueListImpl &
ref()269 SBValueList::ref ()
270 {
271 CreateIfNeeded();
272 return *m_opaque_ap.get();
273 }
274
275
276