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