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/CompletionRequest.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/Utility/Status.h"
21 #include "lldb/lldb-defines.h"
22 #include "lldb/lldb-private-enumerations.h"
23 #include "lldb/lldb-private-interfaces.h"
25 namespace lldb_private {
27 //---------------------------------------------------------------------
29 //---------------------------------------------------------------------
56 eDumpOptionName = (1u << 0),
57 eDumpOptionType = (1u << 1),
58 eDumpOptionValue = (1u << 2),
59 eDumpOptionDescription = (1u << 3),
60 eDumpOptionRaw = (1u << 4),
61 eDumpGroupValue = (eDumpOptionName | eDumpOptionType | eDumpOptionValue),
63 (eDumpOptionName | eDumpOptionType | eDumpOptionDescription)
67 : m_callback(nullptr), m_baton(nullptr), m_value_was_set(false) {}
69 OptionValue(const OptionValue &rhs)
70 : m_callback(rhs.m_callback), m_baton(rhs.m_baton),
71 m_value_was_set(rhs.m_value_was_set) {}
73 virtual ~OptionValue() = default;
75 //-----------------------------------------------------------------
76 // Subclasses should override these functions
77 //-----------------------------------------------------------------
78 virtual Type GetType() const = 0;
80 // If this value is always hidden, the avoid showing any info on this value,
81 // just show the info for the child values.
82 virtual bool ValueIsTransparent() const {
83 return GetType() == eTypeProperties;
86 virtual const char *GetTypeAsCString() const {
87 return GetBuiltinTypeAsCString(GetType());
90 static const char *GetBuiltinTypeAsCString(Type t);
92 virtual void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
93 uint32_t dump_mask) = 0;
96 SetValueFromString(llvm::StringRef value,
97 VarSetOperationType op = eVarSetOperationAssign);
99 virtual bool Clear() = 0;
101 virtual lldb::OptionValueSP DeepCopy() const = 0;
103 virtual size_t AutoComplete(CommandInterpreter &interpreter,
104 CompletionRequest &request);
106 //-----------------------------------------------------------------
107 // Subclasses can override these functions
108 //-----------------------------------------------------------------
109 virtual lldb::OptionValueSP GetSubValue(const ExecutionContext *exe_ctx,
110 llvm::StringRef name,
112 Status &error) const {
113 error.SetErrorStringWithFormat("'%s' is not a value subvalue", name.str().c_str());
114 return lldb::OptionValueSP();
117 virtual Status SetSubValue(const ExecutionContext *exe_ctx,
118 VarSetOperationType op, llvm::StringRef name,
119 llvm::StringRef value);
121 virtual bool IsAggregateValue() const { return false; }
123 virtual ConstString GetName() const { return ConstString(); }
125 virtual bool DumpQualifiedName(Stream &strm) const;
127 //-----------------------------------------------------------------
128 // Subclasses should NOT override these functions as they use the above
129 // functions to implement functionality
130 //-----------------------------------------------------------------
131 uint32_t GetTypeAsMask() { return 1u << GetType(); }
133 static uint32_t ConvertTypeToMask(OptionValue::Type type) {
137 static OptionValue::Type ConvertTypeMaskToType(uint32_t type_mask) {
138 // If only one bit is set, then return an appropriate enumeration
140 case 1u << eTypeArch:
142 case 1u << eTypeArgs:
144 case 1u << eTypeArray:
146 case 1u << eTypeBoolean:
148 case 1u << eTypeChar:
150 case 1u << eTypeDictionary:
151 return eTypeDictionary;
152 case 1u << eTypeEnum:
154 case 1u << eTypeFileSpec:
155 return eTypeFileSpec;
156 case 1u << eTypeFileSpecList:
157 return eTypeFileSpecList;
158 case 1u << eTypeFormat:
160 case 1u << eTypeLanguage:
161 return eTypeLanguage;
162 case 1u << eTypePathMap:
164 case 1u << eTypeProperties:
165 return eTypeProperties;
166 case 1u << eTypeRegex:
168 case 1u << eTypeSInt64:
170 case 1u << eTypeString:
172 case 1u << eTypeUInt64:
174 case 1u << eTypeUUID:
177 // Else return invalid
181 static lldb::OptionValueSP
182 CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask,
185 // Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
186 // or int64_t. Other types will cause "fail_value" to be returned
187 uint64_t GetUInt64Value(uint64_t fail_value, bool *success_ptr);
189 OptionValueArch *GetAsArch();
191 const OptionValueArch *GetAsArch() const;
193 OptionValueArray *GetAsArray();
195 const OptionValueArray *GetAsArray() const;
197 OptionValueArgs *GetAsArgs();
199 const OptionValueArgs *GetAsArgs() const;
201 OptionValueBoolean *GetAsBoolean();
203 OptionValueChar *GetAsChar();
205 const OptionValueBoolean *GetAsBoolean() const;
207 const OptionValueChar *GetAsChar() const;
209 OptionValueDictionary *GetAsDictionary();
211 const OptionValueDictionary *GetAsDictionary() const;
213 OptionValueEnumeration *GetAsEnumeration();
215 const OptionValueEnumeration *GetAsEnumeration() const;
217 OptionValueFileSpec *GetAsFileSpec();
219 const OptionValueFileSpec *GetAsFileSpec() const;
221 OptionValueFileSpecList *GetAsFileSpecList();
223 const OptionValueFileSpecList *GetAsFileSpecList() const;
225 OptionValueFormat *GetAsFormat();
227 const OptionValueFormat *GetAsFormat() const;
229 OptionValueLanguage *GetAsLanguage();
231 const OptionValueLanguage *GetAsLanguage() const;
233 OptionValuePathMappings *GetAsPathMappings();
235 const OptionValuePathMappings *GetAsPathMappings() const;
237 OptionValueProperties *GetAsProperties();
239 const OptionValueProperties *GetAsProperties() const;
241 OptionValueRegex *GetAsRegex();
243 const OptionValueRegex *GetAsRegex() const;
245 OptionValueSInt64 *GetAsSInt64();
247 const OptionValueSInt64 *GetAsSInt64() const;
249 OptionValueString *GetAsString();
251 const OptionValueString *GetAsString() const;
253 OptionValueUInt64 *GetAsUInt64();
255 const OptionValueUInt64 *GetAsUInt64() const;
257 OptionValueUUID *GetAsUUID();
259 const OptionValueUUID *GetAsUUID() const;
261 OptionValueFormatEntity *GetAsFormatEntity();
263 const OptionValueFormatEntity *GetAsFormatEntity() const;
265 bool GetBooleanValue(bool fail_value = false) const;
267 bool SetBooleanValue(bool new_value);
269 char GetCharValue(char fail_value) const;
271 char SetCharValue(char new_value);
273 int64_t GetEnumerationValue(int64_t fail_value = -1) const;
275 bool SetEnumerationValue(int64_t value);
277 FileSpec GetFileSpecValue() const;
279 bool SetFileSpecValue(const FileSpec &file_spec);
281 FileSpecList GetFileSpecListValue() const;
284 GetFormatValue(lldb::Format fail_value = lldb::eFormatDefault) const;
286 bool SetFormatValue(lldb::Format new_value);
288 lldb::LanguageType GetLanguageValue(
289 lldb::LanguageType fail_value = lldb::eLanguageTypeUnknown) const;
291 bool SetLanguageValue(lldb::LanguageType new_language);
293 const FormatEntity::Entry *GetFormatEntity() const;
295 const RegularExpression *GetRegexValue() const;
297 int64_t GetSInt64Value(int64_t fail_value = 0) const;
299 bool SetSInt64Value(int64_t new_value);
301 llvm::StringRef GetStringValue(llvm::StringRef fail_value) const;
302 llvm::StringRef GetStringValue() const { return GetStringValue(llvm::StringRef()); }
304 bool SetStringValue(llvm::StringRef new_value);
306 uint64_t GetUInt64Value(uint64_t fail_value = 0) const;
308 bool SetUInt64Value(uint64_t new_value);
310 UUID GetUUIDValue() const;
312 bool SetUUIDValue(const UUID &uuid);
314 bool OptionWasSet() const { return m_value_was_set; }
316 void SetOptionWasSet() { m_value_was_set = true; }
318 void SetParent(const lldb::OptionValueSP &parent_sp) {
319 m_parent_wp = parent_sp;
322 void SetValueChangedCallback(OptionValueChangedCallback callback,
324 assert(m_callback == nullptr);
325 m_callback = callback;
329 void NotifyValueChanged() {
331 m_callback(m_baton, this);
335 lldb::OptionValueWP m_parent_wp;
336 OptionValueChangedCallback m_callback;
338 bool m_value_was_set; // This can be used to see if a value has been set
339 // by a call to SetValueFromCString(). It is often
340 // handy to know if an option value was set from the
341 // command line or as a setting, versus if we just have
342 // the default value that was already populated in the
346 } // namespace lldb_private
348 #endif // liblldb_OptionValue_h_