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