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:
25     ValueListImpl () :
26     m_values()
27     {
28     }
29 
30     ValueListImpl (const ValueListImpl& rhs) :
31     m_values(rhs.m_values)
32     {
33     }
34 
35     ValueListImpl&
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
45     GetSize ()
46     {
47         return m_values.size();
48     }
49 
50     void
51     Append (const lldb::SBValue& sb_value)
52     {
53         m_values.push_back(sb_value);
54     }
55 
56     void
57     Append (const ValueListImpl& list)
58     {
59         for (auto val : list.m_values)
60             Append (val);
61     }
62 
63     lldb::SBValue
64     GetValueAtIndex (uint32_t index)
65     {
66         if (index >= GetSize())
67             return lldb::SBValue();
68         return m_values[index];
69     }
70 
71     lldb::SBValue
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 
86 SBValueList::SBValueList () :
87     m_opaque_ap ()
88 {
89 }
90 
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                      static_cast<void*>(rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL),
103                      static_cast<void*>(m_opaque_ap.get()));
104     }
105 }
106 
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                      static_cast<const void*>(lldb_object_ptr),
119                      static_cast<void*>(m_opaque_ap.get()));
120     }
121 }
122 
123 SBValueList::~SBValueList ()
124 {
125 }
126 
127 bool
128 SBValueList::IsValid () const
129 {
130     return (m_opaque_ap.get() != NULL);
131 }
132 
133 void
134 SBValueList::Clear()
135 {
136     m_opaque_ap.reset();
137 }
138 
139 const SBValueList &
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 *
153 SBValueList::operator->()
154 {
155     return m_opaque_ap.get();
156 }
157 
158 ValueListImpl &
159 SBValueList::operator*()
160 {
161     return *m_opaque_ap;
162 }
163 
164 const ValueListImpl *
165 SBValueList::operator->() const
166 {
167     return m_opaque_ap.get();
168 }
169 
170 const ValueListImpl &
171 SBValueList::operator*() const
172 {
173     return *m_opaque_ap;
174 }
175 
176 void
177 SBValueList::Append (const SBValue &val_obj)
178 {
179     CreateIfNeeded ();
180     m_opaque_ap->Append (val_obj);
181 }
182 
183 void
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
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
205 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                      static_cast<void*>(m_opaque_ap.get()), idx,
222                      static_cast<void*>(sb_value.GetSP().get()), sstr.GetData());
223     }
224 
225     return sb_value;
226 }
227 
228 uint32_t
229 SBValueList::GetSize () const
230 {
231     Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
232 
233     //if (log)
234     //    log->Printf ("SBValueList::GetSize ()");
235 
236     uint32_t size = 0;
237     if (m_opaque_ap.get())
238         size = m_opaque_ap->GetSize();
239 
240     if (log)
241         log->Printf ("SBValueList::GetSize (this.ap=%p) => %d",
242                      static_cast<void*>(m_opaque_ap.get()), size);
243 
244     return size;
245 }
246 
247 void
248 SBValueList::CreateIfNeeded ()
249 {
250     if (m_opaque_ap.get() == NULL)
251         m_opaque_ap.reset (new ValueListImpl());
252 }
253 
254 
255 SBValue
256 SBValueList::FindValueObjectByUID (lldb::user_id_t uid)
257 {
258     SBValue sb_value;
259     if (m_opaque_ap.get())
260         sb_value = m_opaque_ap->FindValueByUID(uid);
261     return sb_value;
262 }
263 
264 void *
265 SBValueList::opaque_ptr ()
266 {
267     return m_opaque_ap.get();
268 }
269 
270 ValueListImpl &
271 SBValueList::ref ()
272 {
273     CreateIfNeeded();
274     return *m_opaque_ap.get();
275 }
276 
277 
278