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
82private:
83    std::vector<lldb::SBValue> m_values;
84};
85
86SBValueList::SBValueList () :
87    m_opaque_ap ()
88{
89}
90
91SBValueList::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
107SBValueList::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
123SBValueList::~SBValueList ()
124{
125}
126
127bool
128SBValueList::IsValid () const
129{
130    return (m_opaque_ap.get() != NULL);
131}
132
133void
134SBValueList::Clear()
135{
136    m_opaque_ap.reset();
137}
138
139const SBValueList &
140SBValueList::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
152ValueListImpl *
153SBValueList::operator->()
154{
155    return m_opaque_ap.get();
156}
157
158ValueListImpl &
159SBValueList::operator*()
160{
161    return *m_opaque_ap;
162}
163
164const ValueListImpl *
165SBValueList::operator->() const
166{
167    return m_opaque_ap.get();
168}
169
170const ValueListImpl &
171SBValueList::operator*() const
172{
173    return *m_opaque_ap;
174}
175
176void
177SBValueList::Append (const SBValue &val_obj)
178{
179    CreateIfNeeded ();
180    m_opaque_ap->Append (val_obj);
181}
182
183void
184SBValueList::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
193void
194SBValueList::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
204SBValue
205SBValueList::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
227uint32_t
228SBValueList::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
245void
246SBValueList::CreateIfNeeded ()
247{
248    if (m_opaque_ap.get() == NULL)
249        m_opaque_ap.reset (new ValueListImpl());
250}
251
252
253SBValue
254SBValueList::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
262void *
263SBValueList::opaque_ptr ()
264{
265    return m_opaque_ap.get();
266}
267
268ValueListImpl &
269SBValueList::ref ()
270{
271    CreateIfNeeded();
272    return *m_opaque_ap.get();
273}
274
275
276