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
19using namespace lldb;
20using namespace lldb_private;
21
22class ValueListImpl
23{
24public:
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    lldb::SBValue
83    GetFirstValueByName (const char* name) const
84    {
85        if (name)
86        {
87            for (auto val : m_values)
88            {
89                if (val.IsValid() && val.GetName() &&
90                    strcmp(name,val.GetName()) == 0)
91                    return val;
92            }
93        }
94        return lldb::SBValue();
95    }
96
97private:
98    std::vector<lldb::SBValue> m_values;
99};
100
101SBValueList::SBValueList () :
102    m_opaque_ap ()
103{
104}
105
106SBValueList::SBValueList (const SBValueList &rhs) :
107    m_opaque_ap ()
108{
109    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
110
111    if (rhs.IsValid())
112        m_opaque_ap.reset (new ValueListImpl (*rhs));
113
114    if (log)
115    {
116        log->Printf ("SBValueList::SBValueList (rhs.ap=%p) => this.ap = %p",
117                     static_cast<void*>(rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL),
118                     static_cast<void*>(m_opaque_ap.get()));
119    }
120}
121
122SBValueList::SBValueList (const ValueListImpl *lldb_object_ptr) :
123    m_opaque_ap ()
124{
125    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
126
127    if (lldb_object_ptr)
128        m_opaque_ap.reset (new ValueListImpl (*lldb_object_ptr));
129
130    if (log)
131    {
132        log->Printf ("SBValueList::SBValueList (lldb_object_ptr=%p) => this.ap = %p",
133                     static_cast<const void*>(lldb_object_ptr),
134                     static_cast<void*>(m_opaque_ap.get()));
135    }
136}
137
138SBValueList::~SBValueList ()
139{
140}
141
142bool
143SBValueList::IsValid () const
144{
145    return (m_opaque_ap.get() != NULL);
146}
147
148void
149SBValueList::Clear()
150{
151    m_opaque_ap.reset();
152}
153
154const SBValueList &
155SBValueList::operator = (const SBValueList &rhs)
156{
157    if (this != &rhs)
158    {
159        if (rhs.IsValid())
160            m_opaque_ap.reset (new ValueListImpl (*rhs));
161        else
162            m_opaque_ap.reset ();
163    }
164    return *this;
165}
166
167ValueListImpl *
168SBValueList::operator->()
169{
170    return m_opaque_ap.get();
171}
172
173ValueListImpl &
174SBValueList::operator*()
175{
176    return *m_opaque_ap;
177}
178
179const ValueListImpl *
180SBValueList::operator->() const
181{
182    return m_opaque_ap.get();
183}
184
185const ValueListImpl &
186SBValueList::operator*() const
187{
188    return *m_opaque_ap;
189}
190
191void
192SBValueList::Append (const SBValue &val_obj)
193{
194    CreateIfNeeded ();
195    m_opaque_ap->Append (val_obj);
196}
197
198void
199SBValueList::Append (lldb::ValueObjectSP& val_obj_sp)
200{
201    if (val_obj_sp)
202    {
203        CreateIfNeeded ();
204        m_opaque_ap->Append (SBValue(val_obj_sp));
205    }
206}
207
208void
209SBValueList::Append (const lldb::SBValueList& value_list)
210{
211    if (value_list.IsValid())
212    {
213        CreateIfNeeded ();
214        m_opaque_ap->Append (*value_list);
215    }
216}
217
218
219SBValue
220SBValueList::GetValueAtIndex (uint32_t idx) const
221{
222    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
223
224    //if (log)
225    //    log->Printf ("SBValueList::GetValueAtIndex (uint32_t idx) idx = %d", idx);
226
227    SBValue sb_value;
228    if (m_opaque_ap.get())
229        sb_value = m_opaque_ap->GetValueAtIndex (idx);
230
231    if (log)
232    {
233        SBStream sstr;
234        sb_value.GetDescription (sstr);
235        log->Printf ("SBValueList::GetValueAtIndex (this.ap=%p, idx=%d) => SBValue (this.sp = %p, '%s')",
236                     static_cast<void*>(m_opaque_ap.get()), idx,
237                     static_cast<void*>(sb_value.GetSP().get()), sstr.GetData());
238    }
239
240    return sb_value;
241}
242
243uint32_t
244SBValueList::GetSize () const
245{
246    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
247
248    //if (log)
249    //    log->Printf ("SBValueList::GetSize ()");
250
251    uint32_t size = 0;
252    if (m_opaque_ap.get())
253        size = m_opaque_ap->GetSize();
254
255    if (log)
256        log->Printf ("SBValueList::GetSize (this.ap=%p) => %d",
257                     static_cast<void*>(m_opaque_ap.get()), size);
258
259    return size;
260}
261
262void
263SBValueList::CreateIfNeeded ()
264{
265    if (m_opaque_ap.get() == NULL)
266        m_opaque_ap.reset (new ValueListImpl());
267}
268
269
270SBValue
271SBValueList::FindValueObjectByUID (lldb::user_id_t uid)
272{
273    SBValue sb_value;
274    if (m_opaque_ap.get())
275        sb_value = m_opaque_ap->FindValueByUID(uid);
276    return sb_value;
277}
278
279SBValue
280SBValueList::GetFirstValueByName (const char* name) const
281{
282    SBValue sb_value;
283    if (m_opaque_ap.get())
284        sb_value = m_opaque_ap->GetFirstValueByName(name);
285    return sb_value;
286}
287
288void *
289SBValueList::opaque_ptr ()
290{
291    return m_opaque_ap.get();
292}
293
294ValueListImpl &
295SBValueList::ref ()
296{
297    CreateIfNeeded();
298    return *m_opaque_ap.get();
299}
300