1 //===-- OptionValue.h --------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_OptionValue_h_ 11 #define liblldb_OptionValue_h_ 12 13 // C Includes 14 // C++ Includes 15 // Other libraries and framework includes 16 // Project includes 17 #include "lldb/lldb-defines.h" 18 #include "lldb/Core/ConstString.h" 19 #include "lldb/Core/Error.h" 20 #include "lldb/Core/FormatEntity.h" 21 22 namespace lldb_private { 23 24 //--------------------------------------------------------------------- 25 // OptionValue 26 //--------------------------------------------------------------------- 27 class OptionValue 28 { 29 public: 30 typedef enum 31 { 32 eTypeInvalid = 0, 33 eTypeArch, 34 eTypeArgs, 35 eTypeArray, 36 eTypeBoolean, 37 eTypeChar, 38 eTypeDictionary, 39 eTypeEnum, 40 eTypeFileSpec, 41 eTypeFileSpecList, 42 eTypeFormat, 43 eTypeLanguage, 44 eTypePathMap, 45 eTypeProperties, 46 eTypeRegex, 47 eTypeSInt64, 48 eTypeString, 49 eTypeUInt64, 50 eTypeUUID, 51 eTypeFormatEntity 52 } Type; 53 54 enum { 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), 61 eDumpGroupHelp = (eDumpOptionName | eDumpOptionType | eDumpOptionDescription) 62 }; 63 64 OptionValue()65 OptionValue () : 66 m_callback (nullptr), 67 m_baton(nullptr), 68 m_value_was_set (false) 69 { 70 } 71 OptionValue(const OptionValue & rhs)72 OptionValue (const OptionValue &rhs) : 73 m_callback (rhs.m_callback), 74 m_baton (rhs.m_baton), 75 m_value_was_set (rhs.m_value_was_set) 76 { 77 } 78 ~OptionValue()79 virtual ~OptionValue () 80 { 81 } 82 //----------------------------------------------------------------- 83 // Subclasses should override these functions 84 //----------------------------------------------------------------- 85 virtual Type 86 GetType () const = 0; 87 88 // If this value is always hidden, the avoid showing any info on this 89 // value, just show the info for the child values. 90 virtual bool ValueIsTransparent()91 ValueIsTransparent () const 92 { 93 return GetType() == eTypeProperties; 94 } 95 96 virtual const char * GetTypeAsCString()97 GetTypeAsCString () const 98 { 99 return GetBuiltinTypeAsCString(GetType()); 100 } 101 102 103 static const char * 104 GetBuiltinTypeAsCString (Type t); 105 106 virtual void 107 DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) = 0; 108 109 virtual Error 110 SetValueFromString (llvm::StringRef value, VarSetOperationType op = eVarSetOperationAssign); 111 112 virtual bool 113 Clear () = 0; 114 115 virtual lldb::OptionValueSP 116 DeepCopy () const = 0; 117 118 virtual size_t 119 AutoComplete (CommandInterpreter &interpreter, 120 const char *s, 121 int match_start_point, 122 int max_return_elements, 123 bool &word_complete, 124 StringList &matches); 125 126 //----------------------------------------------------------------- 127 // Subclasses can override these functions 128 //----------------------------------------------------------------- 129 virtual lldb::OptionValueSP GetSubValue(const ExecutionContext * exe_ctx,const char * name,bool will_modify,Error & error)130 GetSubValue (const ExecutionContext *exe_ctx, 131 const char *name, 132 bool will_modify, 133 Error &error) const 134 { 135 error.SetErrorStringWithFormat("'%s' is not a value subvalue", name); 136 return lldb::OptionValueSP(); 137 } 138 139 virtual Error 140 SetSubValue (const ExecutionContext *exe_ctx, 141 VarSetOperationType op, 142 const char *name, 143 const char *value); 144 145 virtual bool IsAggregateValue()146 IsAggregateValue () const 147 { 148 return false; 149 } 150 151 virtual ConstString GetName()152 GetName() const 153 { 154 return ConstString(); 155 } 156 157 virtual bool 158 DumpQualifiedName (Stream &strm) const; 159 //----------------------------------------------------------------- 160 // Subclasses should NOT override these functions as they use the 161 // above functions to implement functionality 162 //----------------------------------------------------------------- 163 uint32_t GetTypeAsMask()164 GetTypeAsMask () 165 { 166 return 1u << GetType(); 167 } 168 169 static uint32_t ConvertTypeToMask(OptionValue::Type type)170 ConvertTypeToMask (OptionValue::Type type) 171 { 172 return 1u << type; 173 } 174 175 static OptionValue::Type ConvertTypeMaskToType(uint32_t type_mask)176 ConvertTypeMaskToType (uint32_t type_mask) 177 { 178 // If only one bit is set, then return an appropriate enumeration 179 switch (type_mask) 180 { 181 case 1u << eTypeArch: return eTypeArch; 182 case 1u << eTypeArgs: return eTypeArgs; 183 case 1u << eTypeArray: return eTypeArray; 184 case 1u << eTypeBoolean: return eTypeBoolean; 185 case 1u << eTypeChar: return eTypeChar; 186 case 1u << eTypeDictionary: return eTypeDictionary; 187 case 1u << eTypeEnum: return eTypeEnum; 188 case 1u << eTypeFileSpec: return eTypeFileSpec; 189 case 1u << eTypeFileSpecList: return eTypeFileSpecList; 190 case 1u << eTypeFormat: return eTypeFormat; 191 case 1u << eTypeLanguage: return eTypeLanguage; 192 case 1u << eTypePathMap: return eTypePathMap; 193 case 1u << eTypeProperties: return eTypeProperties; 194 case 1u << eTypeRegex: return eTypeRegex; 195 case 1u << eTypeSInt64: return eTypeSInt64; 196 case 1u << eTypeString: return eTypeString; 197 case 1u << eTypeUInt64: return eTypeUInt64; 198 case 1u << eTypeUUID: return eTypeUUID; 199 } 200 // Else return invalid 201 return eTypeInvalid; 202 } 203 204 static lldb::OptionValueSP 205 CreateValueFromCStringForTypeMask (const char *value_cstr, 206 uint32_t type_mask, 207 Error &error); 208 209 // Get this value as a uint64_t value if it is encoded as a boolean, 210 // uint64_t or int64_t. Other types will cause "fail_value" to be 211 // returned 212 uint64_t 213 GetUInt64Value (uint64_t fail_value, bool *success_ptr); 214 215 OptionValueArch * 216 GetAsArch (); 217 218 const OptionValueArch * 219 GetAsArch () const; 220 221 OptionValueArray * 222 GetAsArray (); 223 224 const OptionValueArray * 225 GetAsArray () const; 226 227 OptionValueArgs * 228 GetAsArgs (); 229 230 const OptionValueArgs * 231 GetAsArgs () const; 232 233 OptionValueBoolean * 234 GetAsBoolean (); 235 236 OptionValueChar * 237 GetAsChar (); 238 239 const OptionValueBoolean * 240 GetAsBoolean () const; 241 242 const OptionValueChar * 243 GetAsChar () const; 244 245 OptionValueDictionary * 246 GetAsDictionary (); 247 248 const OptionValueDictionary * 249 GetAsDictionary () const; 250 251 OptionValueEnumeration * 252 GetAsEnumeration (); 253 254 const OptionValueEnumeration * 255 GetAsEnumeration () const; 256 257 OptionValueFileSpec * 258 GetAsFileSpec (); 259 260 const OptionValueFileSpec * 261 GetAsFileSpec () const; 262 263 OptionValueFileSpecList * 264 GetAsFileSpecList (); 265 266 const OptionValueFileSpecList * 267 GetAsFileSpecList () const; 268 269 OptionValueFormat * 270 GetAsFormat (); 271 272 const OptionValueFormat * 273 GetAsFormat () const; 274 275 OptionValueLanguage * 276 GetAsLanguage (); 277 278 const OptionValueLanguage * 279 GetAsLanguage () const; 280 281 OptionValuePathMappings * 282 GetAsPathMappings (); 283 284 const OptionValuePathMappings * 285 GetAsPathMappings () const; 286 287 OptionValueProperties * 288 GetAsProperties (); 289 290 const OptionValueProperties * 291 GetAsProperties () const; 292 293 OptionValueRegex * 294 GetAsRegex (); 295 296 const OptionValueRegex * 297 GetAsRegex () const; 298 299 OptionValueSInt64 * 300 GetAsSInt64 (); 301 302 const OptionValueSInt64 * 303 GetAsSInt64 () const; 304 305 OptionValueString * 306 GetAsString (); 307 308 const OptionValueString * 309 GetAsString () const; 310 311 OptionValueUInt64 * 312 GetAsUInt64 (); 313 314 const OptionValueUInt64 * 315 GetAsUInt64 () const; 316 317 OptionValueUUID * 318 GetAsUUID (); 319 320 const OptionValueUUID * 321 GetAsUUID () const; 322 323 OptionValueFormatEntity * 324 GetAsFormatEntity (); 325 326 const OptionValueFormatEntity * 327 GetAsFormatEntity () const; 328 329 bool 330 GetBooleanValue (bool fail_value = false) const; 331 332 bool 333 SetBooleanValue (bool new_value); 334 335 char GetCharValue(char fail_value) const; 336 337 char SetCharValue(char new_value); 338 339 int64_t 340 GetEnumerationValue (int64_t fail_value = -1) const; 341 342 bool 343 SetEnumerationValue (int64_t value); 344 345 FileSpec 346 GetFileSpecValue () const; 347 348 bool 349 SetFileSpecValue (const FileSpec &file_spec); 350 351 FileSpecList 352 GetFileSpecListValue () const; 353 354 lldb::Format 355 GetFormatValue (lldb::Format fail_value = lldb::eFormatDefault) const; 356 357 bool 358 SetFormatValue (lldb::Format new_value); 359 360 lldb::LanguageType 361 GetLanguageValue (lldb::LanguageType fail_value = lldb::eLanguageTypeUnknown) const; 362 363 bool 364 SetLanguageValue (lldb::LanguageType new_language); 365 366 const FormatEntity::Entry * 367 GetFormatEntity () const; 368 369 const RegularExpression * 370 GetRegexValue () const; 371 372 int64_t 373 GetSInt64Value (int64_t fail_value = 0) const; 374 375 bool 376 SetSInt64Value (int64_t new_value); 377 378 const char * 379 GetStringValue (const char *fail_value = NULL) const; 380 381 bool 382 SetStringValue (const char *new_value); 383 384 uint64_t 385 GetUInt64Value (uint64_t fail_value = 0) const; 386 387 bool 388 SetUInt64Value (uint64_t new_value); 389 390 UUID 391 GetUUIDValue () const; 392 393 bool 394 SetUUIDValue (const UUID &uuid); 395 396 bool OptionWasSet()397 OptionWasSet () const 398 { 399 return m_value_was_set; 400 } 401 402 void SetOptionWasSet()403 SetOptionWasSet () 404 { 405 m_value_was_set = true; 406 } 407 408 void SetParent(const lldb::OptionValueSP & parent_sp)409 SetParent (const lldb::OptionValueSP &parent_sp) 410 { 411 m_parent_wp = parent_sp; 412 } 413 414 void SetValueChangedCallback(OptionValueChangedCallback callback,void * baton)415 SetValueChangedCallback (OptionValueChangedCallback callback, 416 void *baton) 417 { 418 assert (m_callback == NULL); 419 m_callback = callback; 420 m_baton = baton; 421 } 422 423 void NotifyValueChanged()424 NotifyValueChanged () 425 { 426 if (m_callback) 427 m_callback (m_baton, this); 428 } 429 protected: 430 lldb::OptionValueWP m_parent_wp; 431 OptionValueChangedCallback m_callback; 432 void *m_baton; 433 bool m_value_was_set; // This can be used to see if a value has been set 434 // by a call to SetValueFromCString(). It is often 435 // handy to know if an option value was set from 436 // the command line or as a setting, versus if we 437 // just have the default value that was already 438 // populated in the option value. 439 440 }; 441 442 } // namespace lldb_private 443 444 #endif // liblldb_OptionValue_h_ 445