1 //===-- OptionValue.h -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_OptionValue_h_
11 #define liblldb_OptionValue_h_
15 // Other libraries and framework includes
17 #include "lldb/Core/FormatEntity.h"
18 #include "lldb/Utility/ConstString.h"
19 #include "lldb/Utility/Status.h"
20 #include "lldb/lldb-defines.h"
21 #include "lldb/lldb-private-enumerations.h"
22 #include "lldb/lldb-private-interfaces.h"
24 namespace lldb_private {
26 //---------------------------------------------------------------------
28 //---------------------------------------------------------------------
55 eDumpOptionName = (1u << 0),
56 eDumpOptionType = (1u << 1),
57 eDumpOptionValue = (1u << 2),
58 eDumpOptionDescription = (1u << 3),
59 eDumpOptionRaw = (1u << 4),
60 eDumpGroupValue = (eDumpOptionName | eDumpOptionType | eDumpOptionValue),
62 (eDumpOptionName | eDumpOptionType | eDumpOptionDescription)
66 : m_callback(nullptr), m_baton(nullptr), m_value_was_set(false) {}
68 OptionValue(const OptionValue &rhs)
69 : m_callback(rhs.m_callback), m_baton(rhs.m_baton),
70 m_value_was_set(rhs.m_value_was_set) {}
72 virtual ~OptionValue() = default;
74 //-----------------------------------------------------------------
75 // Subclasses should override these functions
76 //-----------------------------------------------------------------
77 virtual Type GetType() const = 0;
79 // If this value is always hidden, the avoid showing any info on this
80 // value, just show the info for the child values.
81 virtual bool ValueIsTransparent() const {
82 return GetType() == eTypeProperties;
85 virtual const char *GetTypeAsCString() const {
86 return GetBuiltinTypeAsCString(GetType());
89 static const char *GetBuiltinTypeAsCString(Type t);
91 virtual void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
92 uint32_t dump_mask) = 0;
95 SetValueFromString(llvm::StringRef value,
96 VarSetOperationType op = eVarSetOperationAssign);
98 virtual bool Clear() = 0;
100 virtual lldb::OptionValueSP DeepCopy() const = 0;
102 virtual size_t AutoComplete(CommandInterpreter &interpreter,
103 llvm::StringRef s, int match_start_point,
104 int max_return_elements, bool &word_complete,
105 StringList &matches);
107 //-----------------------------------------------------------------
108 // Subclasses can override these functions
109 //-----------------------------------------------------------------
110 virtual lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx,
111 llvm::StringRef name,
113 Status &error) const {
114 error.SetErrorStringWithFormat("'%s' is not a value subvalue", name.str().c_str());
115 return lldb::OptionValueSP();
118 virtual Status SetSubValue(const ExecutionContext *exe_ctx,
119 VarSetOperationType op, llvm::StringRef name,
120 llvm::StringRef value);
122 virtual bool IsAggregateValue() const { return false; }
124 virtual ConstString GetName() const { return ConstString(); }
126 virtual bool DumpQualifiedName(Stream &strm) const;
128 //-----------------------------------------------------------------
129 // Subclasses should NOT override these functions as they use the
130 // above functions to implement functionality
131 //-----------------------------------------------------------------
132 uint32_t GetTypeAsMask() { return 1u << GetType(); }
134 static uint32_t ConvertTypeToMask(OptionValue::Type type) {
138 static OptionValue::Type ConvertTypeMaskToType(uint32_t type_mask) {
139 // If only one bit is set, then return an appropriate enumeration
141 case 1u << eTypeArch:
143 case 1u << eTypeArgs:
145 case 1u << eTypeArray:
147 case 1u << eTypeBoolean:
149 case 1u << eTypeChar:
151 case 1u << eTypeDictionary:
152 return eTypeDictionary;
153 case 1u << eTypeEnum:
155 case 1u << eTypeFileSpec:
156 return eTypeFileSpec;
157 case 1u << eTypeFileSpecList:
158 return eTypeFileSpecList;
159 case 1u << eTypeFormat:
161 case 1u << eTypeLanguage:
162 return eTypeLanguage;
163 case 1u << eTypePathMap:
165 case 1u << eTypeProperties:
166 return eTypeProperties;
167 case 1u << eTypeRegex:
169 case 1u << eTypeSInt64:
171 case 1u << eTypeString:
173 case 1u << eTypeUInt64:
175 case 1u << eTypeUUID:
178 // Else return invalid
182 static lldb::OptionValueSP
183 CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask,
186 // Get this value as a uint64_t value if it is encoded as a boolean,
187 // uint64_t or int64_t. Other types will cause "fail_value" to be
189 uint64_t GetUInt64Value(uint64_t fail_value, bool *success_ptr);
191 OptionValueArch *GetAsArch();
193 const OptionValueArch *GetAsArch() const;
195 OptionValueArray *GetAsArray();
197 const OptionValueArray *GetAsArray() const;
199 OptionValueArgs *GetAsArgs();
201 const OptionValueArgs *GetAsArgs() const;
203 OptionValueBoolean *GetAsBoolean();
205 OptionValueChar *GetAsChar();
207 const OptionValueBoolean *GetAsBoolean() const;
209 const OptionValueChar *GetAsChar() const;
211 OptionValueDictionary *GetAsDictionary();
213 const OptionValueDictionary *GetAsDictionary() const;
215 OptionValueEnumeration *GetAsEnumeration();
217 const OptionValueEnumeration *GetAsEnumeration() const;
219 OptionValueFileSpec *GetAsFileSpec();
221 const OptionValueFileSpec *GetAsFileSpec() const;
223 OptionValueFileSpecList *GetAsFileSpecList();
225 const OptionValueFileSpecList *GetAsFileSpecList() const;
227 OptionValueFormat *GetAsFormat();
229 const OptionValueFormat *GetAsFormat() const;
231 OptionValueLanguage *GetAsLanguage();
233 const OptionValueLanguage *GetAsLanguage() const;
235 OptionValuePathMappings *GetAsPathMappings();
237 const OptionValuePathMappings *GetAsPathMappings() const;
239 OptionValueProperties *GetAsProperties();
241 const OptionValueProperties *GetAsProperties() const;
243 OptionValueRegex *GetAsRegex();
245 const OptionValueRegex *GetAsRegex() const;
247 OptionValueSInt64 *GetAsSInt64();
249 const OptionValueSInt64 *GetAsSInt64() const;
251 OptionValueString *GetAsString();
253 const OptionValueString *GetAsString() const;
255 OptionValueUInt64 *GetAsUInt64();
257 const OptionValueUInt64 *GetAsUInt64() const;
259 OptionValueUUID *GetAsUUID();
261 const OptionValueUUID *GetAsUUID() const;
263 OptionValueFormatEntity *GetAsFormatEntity();
265 const OptionValueFormatEntity *GetAsFormatEntity() const;
267 bool GetBooleanValue(bool fail_value = false) const;
269 bool SetBooleanValue(bool new_value);
271 char GetCharValue(char fail_value) const;
273 char SetCharValue(char new_value);
275 int64_t GetEnumerationValue(int64_t fail_value = -1) const;
277 bool SetEnumerationValue(int64_t value);
279 FileSpec GetFileSpecValue() const;
281 bool SetFileSpecValue(const FileSpec &file_spec);
283 FileSpecList GetFileSpecListValue() const;
286 GetFormatValue(lldb::Format fail_value = lldb::eFormatDefault) const;
288 bool SetFormatValue(lldb::Format new_value);
290 lldb::LanguageType GetLanguageValue(
291 lldb::LanguageType fail_value = lldb::eLanguageTypeUnknown) const;
293 bool SetLanguageValue(lldb::LanguageType new_language);
295 const FormatEntity::Entry *GetFormatEntity() const;
297 const RegularExpression *GetRegexValue() const;
299 int64_t GetSInt64Value(int64_t fail_value = 0) const;
301 bool SetSInt64Value(int64_t new_value);
303 llvm::StringRef GetStringValue(llvm::StringRef fail_value) const;
304 llvm::StringRef GetStringValue() const { return GetStringValue(llvm::StringRef()); }
306 bool SetStringValue(llvm::StringRef new_value);
308 uint64_t GetUInt64Value(uint64_t fail_value = 0) const;
310 bool SetUInt64Value(uint64_t new_value);
312 UUID GetUUIDValue() const;
314 bool SetUUIDValue(const UUID &uuid);
316 bool OptionWasSet() const { return m_value_was_set; }
318 void SetOptionWasSet() { m_value_was_set = true; }
320 void SetParent(const lldb::OptionValueSP &parent_sp) {
321 m_parent_wp = parent_sp;
324 void SetValueChangedCallback(OptionValueChangedCallback callback,
326 assert(m_callback == nullptr);
327 m_callback = callback;
331 void NotifyValueChanged() {
333 m_callback(m_baton, this);
337 lldb::OptionValueWP m_parent_wp;
338 OptionValueChangedCallback m_callback;
340 bool m_value_was_set; // This can be used to see if a value has been set
341 // by a call to SetValueFromCString(). It is often
342 // handy to know if an option value was set from
343 // the command line or as a setting, versus if we
344 // just have the default value that was already
345 // populated in the option value.
348 } // namespace lldb_private
350 #endif // liblldb_OptionValue_h_