1 //===-- SBStringList.cpp --------------------------------------------------===//
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/API/SBStringList.h"
10 #include "lldb/Utility/ReproducerInstrumentation.h"
11 #include "Utils.h"
12 #include "lldb/Utility/StringList.h"
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
17 SBStringList::SBStringList() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStringList); }
18 
19 SBStringList::SBStringList(const lldb_private::StringList *lldb_strings_ptr) {
20   if (lldb_strings_ptr)
21     m_opaque_up = std::make_unique<StringList>(*lldb_strings_ptr);
22 }
23 
24 SBStringList::SBStringList(const SBStringList &rhs) {
25   LLDB_RECORD_CONSTRUCTOR(SBStringList, (const lldb::SBStringList &), rhs);
26 
27   m_opaque_up = clone(rhs.m_opaque_up);
28 }
29 
30 const SBStringList &SBStringList::operator=(const SBStringList &rhs) {
31   LLDB_RECORD_METHOD(const lldb::SBStringList &,
32                      SBStringList, operator=,(const lldb::SBStringList &), rhs);
33 
34   if (this != &rhs)
35     m_opaque_up = clone(rhs.m_opaque_up);
36   return *this;
37 }
38 
39 SBStringList::~SBStringList() = default;
40 
41 const lldb_private::StringList *SBStringList::operator->() const {
42   return m_opaque_up.get();
43 }
44 
45 const lldb_private::StringList &SBStringList::operator*() const {
46   return *m_opaque_up;
47 }
48 
49 bool SBStringList::IsValid() const {
50   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStringList, IsValid);
51   return this->operator bool();
52 }
53 SBStringList::operator bool() const {
54   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStringList, operator bool);
55 
56   return (m_opaque_up != nullptr);
57 }
58 
59 void SBStringList::AppendString(const char *str) {
60   LLDB_RECORD_METHOD(void, SBStringList, AppendString, (const char *), str);
61 
62   if (str != nullptr) {
63     if (IsValid())
64       m_opaque_up->AppendString(str);
65     else
66       m_opaque_up = std::make_unique<lldb_private::StringList>(str);
67   }
68 }
69 
70 void SBStringList::AppendList(const char **strv, int strc) {
71   LLDB_RECORD_METHOD(void, SBStringList, AppendList, (const char **, int), strv,
72                      strc);
73 
74   if ((strv != nullptr) && (strc > 0)) {
75     if (IsValid())
76       m_opaque_up->AppendList(strv, strc);
77     else
78       m_opaque_up = std::make_unique<lldb_private::StringList>(strv, strc);
79   }
80 }
81 
82 void SBStringList::AppendList(const SBStringList &strings) {
83   LLDB_RECORD_METHOD(void, SBStringList, AppendList,
84                      (const lldb::SBStringList &), strings);
85 
86   if (strings.IsValid()) {
87     if (!IsValid())
88       m_opaque_up = std::make_unique<lldb_private::StringList>();
89     m_opaque_up->AppendList(*(strings.m_opaque_up));
90   }
91 }
92 
93 void SBStringList::AppendList(const StringList &strings) {
94   if (!IsValid())
95     m_opaque_up = std::make_unique<lldb_private::StringList>();
96   m_opaque_up->AppendList(strings);
97 }
98 
99 uint32_t SBStringList::GetSize() const {
100   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBStringList, GetSize);
101 
102   if (IsValid()) {
103     return m_opaque_up->GetSize();
104   }
105   return 0;
106 }
107 
108 const char *SBStringList::GetStringAtIndex(size_t idx) {
109   LLDB_RECORD_METHOD(const char *, SBStringList, GetStringAtIndex, (size_t),
110                      idx);
111 
112   if (IsValid()) {
113     return m_opaque_up->GetStringAtIndex(idx);
114   }
115   return nullptr;
116 }
117 
118 const char *SBStringList::GetStringAtIndex(size_t idx) const {
119   LLDB_RECORD_METHOD_CONST(const char *, SBStringList, GetStringAtIndex,
120                            (size_t), idx);
121 
122   if (IsValid()) {
123     return m_opaque_up->GetStringAtIndex(idx);
124   }
125   return nullptr;
126 }
127 
128 void SBStringList::Clear() {
129   LLDB_RECORD_METHOD_NO_ARGS(void, SBStringList, Clear);
130 
131   if (IsValid()) {
132     m_opaque_up->Clear();
133   }
134 }
135