1 //===-- OptionValueArray.h --------------------------------------*- 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 #ifndef LLDB_INTERPRETER_OPTIONVALUEARRAY_H
10 #define LLDB_INTERPRETER_OPTIONVALUEARRAY_H
11 
12 #include <vector>
13 
14 #include "lldb/Interpreter/OptionValue.h"
15 
16 namespace lldb_private {
17 
18 class OptionValueArray : public Cloneable<OptionValueArray, OptionValue> {
19 public:
20   OptionValueArray(uint32_t type_mask = UINT32_MAX, bool raw_value_dump = false)
21       : m_type_mask(type_mask), m_raw_value_dump(raw_value_dump) {}
22 
23   ~OptionValueArray() override = default;
24 
25   // Virtual subclass pure virtual overrides
26 
27   OptionValue::Type GetType() const override { return eTypeArray; }
28 
29   void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
30                  uint32_t dump_mask) override;
31 
32   llvm::json::Value ToJSON(const ExecutionContext *exe_ctx) override;
33 
34   Status
35   SetValueFromString(llvm::StringRef value,
36                      VarSetOperationType op = eVarSetOperationAssign) override;
37 
38   void Clear() override {
39     m_values.clear();
40     m_value_was_set = false;
41   }
42 
43   lldb::OptionValueSP
44   DeepCopy(const lldb::OptionValueSP &new_parent) const override;
45 
46   bool IsAggregateValue() const override { return true; }
47 
48   lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx,
49                                   llvm::StringRef name, bool will_modify,
50                                   Status &error) const override;
51 
52   // Subclass specific functions
53 
54   size_t GetSize() const { return m_values.size(); }
55 
56   lldb::OptionValueSP operator[](size_t idx) const {
57     lldb::OptionValueSP value_sp;
58     if (idx < m_values.size())
59       value_sp = m_values[idx];
60     return value_sp;
61   }
62 
63   lldb::OptionValueSP GetValueAtIndex(size_t idx) const {
64     lldb::OptionValueSP value_sp;
65     if (idx < m_values.size())
66       value_sp = m_values[idx];
67     return value_sp;
68   }
69 
70   bool AppendValue(const lldb::OptionValueSP &value_sp) {
71     // Make sure the value_sp object is allowed to contain values of the type
72     // passed in...
73     if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) {
74       m_values.push_back(value_sp);
75       return true;
76     }
77     return false;
78   }
79 
80   bool InsertValue(size_t idx, const lldb::OptionValueSP &value_sp) {
81     // Make sure the value_sp object is allowed to contain values of the type
82     // passed in...
83     if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) {
84       if (idx < m_values.size())
85         m_values.insert(m_values.begin() + idx, value_sp);
86       else
87         m_values.push_back(value_sp);
88       return true;
89     }
90     return false;
91   }
92 
93   bool ReplaceValue(size_t idx, const lldb::OptionValueSP &value_sp) {
94     // Make sure the value_sp object is allowed to contain values of the type
95     // passed in...
96     if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) {
97       if (idx < m_values.size()) {
98         m_values[idx] = value_sp;
99         return true;
100       }
101     }
102     return false;
103   }
104 
105   bool DeleteValue(size_t idx) {
106     if (idx < m_values.size()) {
107       m_values.erase(m_values.begin() + idx);
108       return true;
109     }
110     return false;
111   }
112 
113   size_t GetArgs(Args &args) const;
114 
115   Status SetArgs(const Args &args, VarSetOperationType op);
116 
117 protected:
118   typedef std::vector<lldb::OptionValueSP> collection;
119 
120   uint32_t m_type_mask;
121   collection m_values;
122   bool m_raw_value_dump;
123 };
124 
125 } // namespace lldb_private
126 
127 #endif // LLDB_INTERPRETER_OPTIONVALUEARRAY_H
128