Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | #include "lldb/API/SBValueList.h" |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 11 | #include "lldb/API/SBStream.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBValue.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Core/ValueObjectList.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 14 | #include "lldb/Utility/Log.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | |
Enrico Granata | 85425d7 | 2013-02-07 18:23:56 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | using namespace lldb; |
| 19 | using namespace lldb_private; |
| 20 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | class ValueListImpl { |
Enrico Granata | 19f0e8c | 2013-04-22 22:57:56 +0000 | [diff] [blame] | 22 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | ValueListImpl() : m_values() {} |
| 24 | |
| 25 | ValueListImpl(const ValueListImpl &rhs) : m_values(rhs.m_values) {} |
| 26 | |
| 27 | ValueListImpl &operator=(const ValueListImpl &rhs) { |
| 28 | if (this == &rhs) |
| 29 | return *this; |
| 30 | m_values = rhs.m_values; |
| 31 | return *this; |
| 32 | } |
| 33 | |
| 34 | uint32_t GetSize() { return m_values.size(); } |
| 35 | |
| 36 | void Append(const lldb::SBValue &sb_value) { m_values.push_back(sb_value); } |
| 37 | |
| 38 | void Append(const ValueListImpl &list) { |
| 39 | for (auto val : list.m_values) |
| 40 | Append(val); |
| 41 | } |
| 42 | |
| 43 | lldb::SBValue GetValueAtIndex(uint32_t index) { |
| 44 | if (index >= GetSize()) |
| 45 | return lldb::SBValue(); |
| 46 | return m_values[index]; |
| 47 | } |
| 48 | |
| 49 | lldb::SBValue FindValueByUID(lldb::user_id_t uid) { |
| 50 | for (auto val : m_values) { |
| 51 | if (val.IsValid() && val.GetID() == uid) |
| 52 | return val; |
Enrico Granata | 19f0e8c | 2013-04-22 22:57:56 +0000 | [diff] [blame] | 53 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | return lldb::SBValue(); |
| 55 | } |
| 56 | |
| 57 | lldb::SBValue GetFirstValueByName(const char *name) const { |
| 58 | if (name) { |
| 59 | for (auto val : m_values) { |
| 60 | if (val.IsValid() && val.GetName() && strcmp(name, val.GetName()) == 0) |
| 61 | return val; |
| 62 | } |
Enrico Granata | 19f0e8c | 2013-04-22 22:57:56 +0000 | [diff] [blame] | 63 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | return lldb::SBValue(); |
| 65 | } |
Enrico Granata | 85425d7 | 2013-02-07 18:23:56 +0000 | [diff] [blame] | 66 | |
Enrico Granata | 19f0e8c | 2013-04-22 22:57:56 +0000 | [diff] [blame] | 67 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | std::vector<lldb::SBValue> m_values; |
Enrico Granata | 19f0e8c | 2013-04-22 22:57:56 +0000 | [diff] [blame] | 69 | }; |
Enrico Granata | 85425d7 | 2013-02-07 18:23:56 +0000 | [diff] [blame] | 70 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | SBValueList::SBValueList() : m_opaque_ap() {} |
| 72 | |
| 73 | SBValueList::SBValueList(const SBValueList &rhs) : m_opaque_ap() { |
| 74 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 75 | |
| 76 | if (rhs.IsValid()) |
| 77 | m_opaque_ap.reset(new ValueListImpl(*rhs)); |
| 78 | |
| 79 | if (log) { |
| 80 | log->Printf( |
| 81 | "SBValueList::SBValueList (rhs.ap=%p) => this.ap = %p", |
| 82 | static_cast<void *>(rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL), |
| 83 | static_cast<void *>(m_opaque_ap.get())); |
| 84 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) : m_opaque_ap() { |
| 88 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | if (lldb_object_ptr) |
| 91 | m_opaque_ap.reset(new ValueListImpl(*lldb_object_ptr)); |
| 92 | |
| 93 | if (log) { |
| 94 | log->Printf("SBValueList::SBValueList (lldb_object_ptr=%p) => this.ap = %p", |
| 95 | static_cast<const void *>(lldb_object_ptr), |
| 96 | static_cast<void *>(m_opaque_ap.get())); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | SBValueList::~SBValueList() {} |
| 101 | |
| 102 | bool SBValueList::IsValid() const { return (m_opaque_ap.get() != NULL); } |
| 103 | |
| 104 | void SBValueList::Clear() { m_opaque_ap.reset(); } |
| 105 | |
| 106 | const SBValueList &SBValueList::operator=(const SBValueList &rhs) { |
| 107 | if (this != &rhs) { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | if (rhs.IsValid()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | m_opaque_ap.reset(new ValueListImpl(*rhs)); |
| 110 | else |
| 111 | m_opaque_ap.reset(); |
| 112 | } |
| 113 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | ValueListImpl *SBValueList::operator->() { return m_opaque_ap.get(); } |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | ValueListImpl &SBValueList::operator*() { return *m_opaque_ap; } |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | const ValueListImpl *SBValueList::operator->() const { |
| 121 | return m_opaque_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | const ValueListImpl &SBValueList::operator*() const { return *m_opaque_ap; } |
| 125 | |
| 126 | void SBValueList::Append(const SBValue &val_obj) { |
| 127 | CreateIfNeeded(); |
| 128 | m_opaque_ap->Append(val_obj); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) { |
| 132 | if (val_obj_sp) { |
Greg Clayton | dea8cb4 | 2011-06-29 22:09:02 +0000 | [diff] [blame] | 133 | CreateIfNeeded(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | m_opaque_ap->Append(SBValue(val_obj_sp)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void SBValueList::Append(const lldb::SBValueList &value_list) { |
| 139 | if (value_list.IsValid()) { |
| 140 | CreateIfNeeded(); |
| 141 | m_opaque_ap->Append(*value_list); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | SBValue SBValueList::GetValueAtIndex(uint32_t idx) const { |
| 146 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 147 | |
| 148 | // if (log) |
| 149 | // log->Printf ("SBValueList::GetValueAtIndex (uint32_t idx) idx = %d", |
| 150 | // idx); |
| 151 | |
| 152 | SBValue sb_value; |
| 153 | if (m_opaque_ap.get()) |
| 154 | sb_value = m_opaque_ap->GetValueAtIndex(idx); |
| 155 | |
| 156 | if (log) { |
| 157 | SBStream sstr; |
| 158 | sb_value.GetDescription(sstr); |
| 159 | log->Printf("SBValueList::GetValueAtIndex (this.ap=%p, idx=%d) => SBValue " |
| 160 | "(this.sp = %p, '%s')", |
| 161 | static_cast<void *>(m_opaque_ap.get()), idx, |
| 162 | static_cast<void *>(sb_value.GetSP().get()), sstr.GetData()); |
| 163 | } |
| 164 | |
| 165 | return sb_value; |
| 166 | } |
| 167 | |
| 168 | uint32_t SBValueList::GetSize() const { |
| 169 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 170 | |
| 171 | // if (log) |
| 172 | // log->Printf ("SBValueList::GetSize ()"); |
| 173 | |
| 174 | uint32_t size = 0; |
| 175 | if (m_opaque_ap.get()) |
| 176 | size = m_opaque_ap->GetSize(); |
| 177 | |
| 178 | if (log) |
| 179 | log->Printf("SBValueList::GetSize (this.ap=%p) => %d", |
| 180 | static_cast<void *>(m_opaque_ap.get()), size); |
| 181 | |
| 182 | return size; |
| 183 | } |
| 184 | |
| 185 | void SBValueList::CreateIfNeeded() { |
| 186 | if (m_opaque_ap.get() == NULL) |
| 187 | m_opaque_ap.reset(new ValueListImpl()); |
| 188 | } |
| 189 | |
| 190 | SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) { |
| 191 | SBValue sb_value; |
| 192 | if (m_opaque_ap.get()) |
| 193 | sb_value = m_opaque_ap->FindValueByUID(uid); |
| 194 | return sb_value; |
| 195 | } |
| 196 | |
| 197 | SBValue SBValueList::GetFirstValueByName(const char *name) const { |
| 198 | SBValue sb_value; |
| 199 | if (m_opaque_ap.get()) |
| 200 | sb_value = m_opaque_ap->GetFirstValueByName(name); |
| 201 | return sb_value; |
| 202 | } |
| 203 | |
| 204 | void *SBValueList::opaque_ptr() { return m_opaque_ap.get(); } |
| 205 | |
| 206 | ValueListImpl &SBValueList::ref() { |
| 207 | CreateIfNeeded(); |
| 208 | return *m_opaque_ap.get(); |
Greg Clayton | dea8cb4 | 2011-06-29 22:09:02 +0000 | [diff] [blame] | 209 | } |