1 //===-- ValueObjectList.cpp -------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Core/ValueObjectList.h"
10 
11 #include "lldb/Core/ValueObject.h"
12 #include "lldb/Utility/ConstString.h"
13 #include "lldb/Utility/SharingPtr.h"
14 
15 #include <utility>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 ValueObjectList::ValueObjectList() : m_value_objects() {}
21 
22 ValueObjectList::ValueObjectList(const ValueObjectList &rhs)
23     : m_value_objects(rhs.m_value_objects) {}
24 
25 ValueObjectList::~ValueObjectList() {}
26 
27 const ValueObjectList &ValueObjectList::operator=(const ValueObjectList &rhs) {
28   if (this != &rhs)
29     m_value_objects = rhs.m_value_objects;
30   return *this;
31 }
32 
33 void ValueObjectList::Append(const ValueObjectSP &val_obj_sp) {
34   m_value_objects.push_back(val_obj_sp);
35 }
36 
37 void ValueObjectList::Append(const ValueObjectList &valobj_list) {
38   std::copy(valobj_list.m_value_objects.begin(), // source begin
39             valobj_list.m_value_objects.end(),   // source end
40             back_inserter(m_value_objects));     // destination
41 }
42 
43 size_t ValueObjectList::GetSize() const { return m_value_objects.size(); }
44 
45 void ValueObjectList::Resize(size_t size) { m_value_objects.resize(size); }
46 
47 lldb::ValueObjectSP ValueObjectList::GetValueObjectAtIndex(size_t idx) {
48   lldb::ValueObjectSP valobj_sp;
49   if (idx < m_value_objects.size())
50     valobj_sp = m_value_objects[idx];
51   return valobj_sp;
52 }
53 
54 lldb::ValueObjectSP ValueObjectList::RemoveValueObjectAtIndex(size_t idx) {
55   lldb::ValueObjectSP valobj_sp;
56   if (idx < m_value_objects.size()) {
57     valobj_sp = m_value_objects[idx];
58     m_value_objects.erase(m_value_objects.begin() + idx);
59   }
60   return valobj_sp;
61 }
62 
63 void ValueObjectList::SetValueObjectAtIndex(size_t idx,
64                                             const ValueObjectSP &valobj_sp) {
65   if (idx >= m_value_objects.size())
66     m_value_objects.resize(idx + 1);
67   m_value_objects[idx] = valobj_sp;
68 }
69 
70 ValueObjectSP ValueObjectList::FindValueObjectByValueName(const char *name) {
71   ConstString name_const_str(name);
72   ValueObjectSP val_obj_sp;
73   collection::iterator pos, end = m_value_objects.end();
74   for (pos = m_value_objects.begin(); pos != end; ++pos) {
75     ValueObject *valobj = (*pos).get();
76     if (valobj && valobj->GetName() == name_const_str) {
77       val_obj_sp = *pos;
78       break;
79     }
80   }
81   return val_obj_sp;
82 }
83 
84 ValueObjectSP ValueObjectList::FindValueObjectByUID(lldb::user_id_t uid) {
85   ValueObjectSP valobj_sp;
86   collection::iterator pos, end = m_value_objects.end();
87 
88   for (pos = m_value_objects.begin(); pos != end; ++pos) {
89     // Watch out for NULL objects in our list as the list might get resized to
90     // a specific size and lazily filled in
91     ValueObject *valobj = (*pos).get();
92     if (valobj && valobj->GetID() == uid) {
93       valobj_sp = *pos;
94       break;
95     }
96   }
97   return valobj_sp;
98 }
99 
100 ValueObjectSP
101 ValueObjectList::FindValueObjectByPointer(ValueObject *find_valobj) {
102   ValueObjectSP valobj_sp;
103   collection::iterator pos, end = m_value_objects.end();
104 
105   for (pos = m_value_objects.begin(); pos != end; ++pos) {
106     ValueObject *valobj = (*pos).get();
107     if (valobj && valobj == find_valobj) {
108       valobj_sp = *pos;
109       break;
110     }
111   }
112   return valobj_sp;
113 }
114 
115 void ValueObjectList::Swap(ValueObjectList &value_object_list) {
116   m_value_objects.swap(value_object_list.m_value_objects);
117 }
118