1ac7ddfbfSEd Maste //===-- SBValueList.cpp -----------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
10ac7ddfbfSEd Maste #include "lldb/API/SBValueList.h"
11ac7ddfbfSEd Maste #include "lldb/API/SBStream.h"
12435933ddSDimitry Andric #include "lldb/API/SBValue.h"
13ac7ddfbfSEd Maste #include "lldb/Core/ValueObjectList.h"
14f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
15ac7ddfbfSEd Maste
16ac7ddfbfSEd Maste #include <vector>
17ac7ddfbfSEd Maste
18ac7ddfbfSEd Maste using namespace lldb;
19ac7ddfbfSEd Maste using namespace lldb_private;
20ac7ddfbfSEd Maste
21435933ddSDimitry Andric class ValueListImpl {
22ac7ddfbfSEd Maste public:
ValueListImpl()23435933ddSDimitry Andric ValueListImpl() : m_values() {}
24ac7ddfbfSEd Maste
ValueListImpl(const ValueListImpl & rhs)25435933ddSDimitry Andric ValueListImpl(const ValueListImpl &rhs) : m_values(rhs.m_values) {}
26ac7ddfbfSEd Maste
operator =(const ValueListImpl & rhs)27435933ddSDimitry Andric ValueListImpl &operator=(const ValueListImpl &rhs) {
28ac7ddfbfSEd Maste if (this == &rhs)
29ac7ddfbfSEd Maste return *this;
30ac7ddfbfSEd Maste m_values = rhs.m_values;
31ac7ddfbfSEd Maste return *this;
329f2f44ceSEd Maste }
33ac7ddfbfSEd Maste
GetSize()34435933ddSDimitry Andric uint32_t GetSize() { return m_values.size(); }
35ac7ddfbfSEd Maste
Append(const lldb::SBValue & sb_value)36435933ddSDimitry Andric void Append(const lldb::SBValue &sb_value) { m_values.push_back(sb_value); }
37ac7ddfbfSEd Maste
Append(const ValueListImpl & list)38435933ddSDimitry Andric void Append(const ValueListImpl &list) {
39ac7ddfbfSEd Maste for (auto val : list.m_values)
40ac7ddfbfSEd Maste Append(val);
41ac7ddfbfSEd Maste }
42ac7ddfbfSEd Maste
GetValueAtIndex(uint32_t index)43435933ddSDimitry Andric lldb::SBValue GetValueAtIndex(uint32_t index) {
44ac7ddfbfSEd Maste if (index >= GetSize())
45ac7ddfbfSEd Maste return lldb::SBValue();
46ac7ddfbfSEd Maste return m_values[index];
47ac7ddfbfSEd Maste }
48ac7ddfbfSEd Maste
FindValueByUID(lldb::user_id_t uid)49435933ddSDimitry Andric lldb::SBValue FindValueByUID(lldb::user_id_t uid) {
50435933ddSDimitry Andric for (auto val : m_values) {
51ac7ddfbfSEd Maste if (val.IsValid() && val.GetID() == uid)
52ac7ddfbfSEd Maste return val;
53ac7ddfbfSEd Maste }
54ac7ddfbfSEd Maste return lldb::SBValue();
55ac7ddfbfSEd Maste }
56ac7ddfbfSEd Maste
GetFirstValueByName(const char * name) const57435933ddSDimitry Andric lldb::SBValue GetFirstValueByName(const char *name) const {
58435933ddSDimitry Andric if (name) {
59435933ddSDimitry Andric for (auto val : m_values) {
60435933ddSDimitry Andric if (val.IsValid() && val.GetName() && strcmp(name, val.GetName()) == 0)
617aa51b79SEd Maste return val;
627aa51b79SEd Maste }
637aa51b79SEd Maste }
647aa51b79SEd Maste return lldb::SBValue();
657aa51b79SEd Maste }
667aa51b79SEd Maste
67ac7ddfbfSEd Maste private:
68ac7ddfbfSEd Maste std::vector<lldb::SBValue> m_values;
69ac7ddfbfSEd Maste };
70ac7ddfbfSEd Maste
SBValueList()71435933ddSDimitry Andric SBValueList::SBValueList() : m_opaque_ap() {}
72ac7ddfbfSEd Maste
SBValueList(const SBValueList & rhs)73435933ddSDimitry Andric SBValueList::SBValueList(const SBValueList &rhs) : m_opaque_ap() {
74ac7ddfbfSEd Maste Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
75ac7ddfbfSEd Maste
76ac7ddfbfSEd Maste if (rhs.IsValid())
77ac7ddfbfSEd Maste m_opaque_ap.reset(new ValueListImpl(*rhs));
78ac7ddfbfSEd Maste
79435933ddSDimitry Andric if (log) {
80435933ddSDimitry Andric log->Printf(
81435933ddSDimitry Andric "SBValueList::SBValueList (rhs.ap=%p) => this.ap = %p",
820127ef0fSEd Maste static_cast<void *>(rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL),
830127ef0fSEd Maste static_cast<void *>(m_opaque_ap.get()));
84ac7ddfbfSEd Maste }
85ac7ddfbfSEd Maste }
86ac7ddfbfSEd Maste
SBValueList(const ValueListImpl * lldb_object_ptr)87435933ddSDimitry Andric SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) : m_opaque_ap() {
88ac7ddfbfSEd Maste Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
89ac7ddfbfSEd Maste
90ac7ddfbfSEd Maste if (lldb_object_ptr)
91ac7ddfbfSEd Maste m_opaque_ap.reset(new ValueListImpl(*lldb_object_ptr));
92ac7ddfbfSEd Maste
93435933ddSDimitry Andric if (log) {
94ac7ddfbfSEd Maste log->Printf("SBValueList::SBValueList (lldb_object_ptr=%p) => this.ap = %p",
950127ef0fSEd Maste static_cast<const void *>(lldb_object_ptr),
960127ef0fSEd Maste static_cast<void *>(m_opaque_ap.get()));
97ac7ddfbfSEd Maste }
98ac7ddfbfSEd Maste }
99ac7ddfbfSEd Maste
~SBValueList()100435933ddSDimitry Andric SBValueList::~SBValueList() {}
101ac7ddfbfSEd Maste
IsValid() const102*b5893f02SDimitry Andric bool SBValueList::IsValid() const { return (m_opaque_ap != NULL); }
103ac7ddfbfSEd Maste
Clear()104435933ddSDimitry Andric void SBValueList::Clear() { m_opaque_ap.reset(); }
105ac7ddfbfSEd Maste
operator =(const SBValueList & rhs)106435933ddSDimitry Andric const SBValueList &SBValueList::operator=(const SBValueList &rhs) {
107435933ddSDimitry Andric if (this != &rhs) {
108ac7ddfbfSEd Maste if (rhs.IsValid())
109ac7ddfbfSEd Maste m_opaque_ap.reset(new ValueListImpl(*rhs));
110ac7ddfbfSEd Maste else
111ac7ddfbfSEd Maste m_opaque_ap.reset();
112ac7ddfbfSEd Maste }
113ac7ddfbfSEd Maste return *this;
114ac7ddfbfSEd Maste }
115ac7ddfbfSEd Maste
operator ->()116435933ddSDimitry Andric ValueListImpl *SBValueList::operator->() { return m_opaque_ap.get(); }
117435933ddSDimitry Andric
operator *()118435933ddSDimitry Andric ValueListImpl &SBValueList::operator*() { return *m_opaque_ap; }
119435933ddSDimitry Andric
operator ->() const120435933ddSDimitry Andric const ValueListImpl *SBValueList::operator->() const {
121ac7ddfbfSEd Maste return m_opaque_ap.get();
122ac7ddfbfSEd Maste }
123ac7ddfbfSEd Maste
operator *() const124435933ddSDimitry Andric const ValueListImpl &SBValueList::operator*() const { return *m_opaque_ap; }
125ac7ddfbfSEd Maste
Append(const SBValue & val_obj)126435933ddSDimitry Andric void SBValueList::Append(const SBValue &val_obj) {
127ac7ddfbfSEd Maste CreateIfNeeded();
128ac7ddfbfSEd Maste m_opaque_ap->Append(val_obj);
129ac7ddfbfSEd Maste }
130ac7ddfbfSEd Maste
Append(lldb::ValueObjectSP & val_obj_sp)131435933ddSDimitry Andric void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) {
132435933ddSDimitry Andric if (val_obj_sp) {
133ac7ddfbfSEd Maste CreateIfNeeded();
134ac7ddfbfSEd Maste m_opaque_ap->Append(SBValue(val_obj_sp));
135ac7ddfbfSEd Maste }
136ac7ddfbfSEd Maste }
137ac7ddfbfSEd Maste
Append(const lldb::SBValueList & value_list)138435933ddSDimitry Andric void SBValueList::Append(const lldb::SBValueList &value_list) {
139435933ddSDimitry Andric if (value_list.IsValid()) {
140ac7ddfbfSEd Maste CreateIfNeeded();
141ac7ddfbfSEd Maste m_opaque_ap->Append(*value_list);
142ac7ddfbfSEd Maste }
143ac7ddfbfSEd Maste }
144ac7ddfbfSEd Maste
GetValueAtIndex(uint32_t idx) const145435933ddSDimitry Andric SBValue SBValueList::GetValueAtIndex(uint32_t idx) const {
146ac7ddfbfSEd Maste Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
147ac7ddfbfSEd Maste
148ac7ddfbfSEd Maste // if (log)
149435933ddSDimitry Andric // log->Printf ("SBValueList::GetValueAtIndex (uint32_t idx) idx = %d",
150435933ddSDimitry Andric // idx);
151ac7ddfbfSEd Maste
152ac7ddfbfSEd Maste SBValue sb_value;
153*b5893f02SDimitry Andric if (m_opaque_ap)
154ac7ddfbfSEd Maste sb_value = m_opaque_ap->GetValueAtIndex(idx);
155ac7ddfbfSEd Maste
156435933ddSDimitry Andric if (log) {
157ac7ddfbfSEd Maste SBStream sstr;
158ac7ddfbfSEd Maste sb_value.GetDescription(sstr);
159435933ddSDimitry Andric log->Printf("SBValueList::GetValueAtIndex (this.ap=%p, idx=%d) => SBValue "
160435933ddSDimitry Andric "(this.sp = %p, '%s')",
1610127ef0fSEd Maste static_cast<void *>(m_opaque_ap.get()), idx,
1620127ef0fSEd Maste static_cast<void *>(sb_value.GetSP().get()), sstr.GetData());
163ac7ddfbfSEd Maste }
164ac7ddfbfSEd Maste
165ac7ddfbfSEd Maste return sb_value;
166ac7ddfbfSEd Maste }
167ac7ddfbfSEd Maste
GetSize() const168435933ddSDimitry Andric uint32_t SBValueList::GetSize() const {
169ac7ddfbfSEd Maste Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
170ac7ddfbfSEd Maste
171ac7ddfbfSEd Maste // if (log)
172ac7ddfbfSEd Maste // log->Printf ("SBValueList::GetSize ()");
173ac7ddfbfSEd Maste
174ac7ddfbfSEd Maste uint32_t size = 0;
175*b5893f02SDimitry Andric if (m_opaque_ap)
176ac7ddfbfSEd Maste size = m_opaque_ap->GetSize();
177ac7ddfbfSEd Maste
178ac7ddfbfSEd Maste if (log)
1790127ef0fSEd Maste log->Printf("SBValueList::GetSize (this.ap=%p) => %d",
1800127ef0fSEd Maste static_cast<void *>(m_opaque_ap.get()), size);
181ac7ddfbfSEd Maste
182ac7ddfbfSEd Maste return size;
183ac7ddfbfSEd Maste }
184ac7ddfbfSEd Maste
CreateIfNeeded()185435933ddSDimitry Andric void SBValueList::CreateIfNeeded() {
186*b5893f02SDimitry Andric if (m_opaque_ap == NULL)
187ac7ddfbfSEd Maste m_opaque_ap.reset(new ValueListImpl());
188ac7ddfbfSEd Maste }
189ac7ddfbfSEd Maste
FindValueObjectByUID(lldb::user_id_t uid)190435933ddSDimitry Andric SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) {
191ac7ddfbfSEd Maste SBValue sb_value;
192*b5893f02SDimitry Andric if (m_opaque_ap)
193ac7ddfbfSEd Maste sb_value = m_opaque_ap->FindValueByUID(uid);
194ac7ddfbfSEd Maste return sb_value;
195ac7ddfbfSEd Maste }
196ac7ddfbfSEd Maste
GetFirstValueByName(const char * name) const197435933ddSDimitry Andric SBValue SBValueList::GetFirstValueByName(const char *name) const {
1987aa51b79SEd Maste SBValue sb_value;
199*b5893f02SDimitry Andric if (m_opaque_ap)
2007aa51b79SEd Maste sb_value = m_opaque_ap->GetFirstValueByName(name);
2017aa51b79SEd Maste return sb_value;
2027aa51b79SEd Maste }
2037aa51b79SEd Maste
opaque_ptr()204435933ddSDimitry Andric void *SBValueList::opaque_ptr() { return m_opaque_ap.get(); }
205ac7ddfbfSEd Maste
ref()206435933ddSDimitry Andric ValueListImpl &SBValueList::ref() {
207ac7ddfbfSEd Maste CreateIfNeeded();
208*b5893f02SDimitry Andric return *m_opaque_ap;
209ac7ddfbfSEd Maste }
210