OptionValueString.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===-- OptionValueString.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. #ifndef LLDB_INTERPRETER_OPTIONVALUESTRING_H
  9. #define LLDB_INTERPRETER_OPTIONVALUESTRING_H
  10. #include <string>
  11. #include "lldb/Utility/Flags.h"
  12. #include "lldb/Interpreter/OptionValue.h"
  13. namespace lldb_private {
  14. class OptionValueString : public Cloneable<OptionValueString, OptionValue> {
  15. public:
  16. typedef Status (*ValidatorCallback)(const char *string, void *baton);
  17. enum Options { eOptionEncodeCharacterEscapeSequences = (1u << 0) };
  18. OptionValueString() = default;
  19. OptionValueString(ValidatorCallback validator, void *baton = nullptr)
  20. : m_validator(validator), m_validator_baton(baton) {}
  21. OptionValueString(const char *value) {
  22. if (value && value[0]) {
  23. m_current_value.assign(value);
  24. m_default_value.assign(value);
  25. }
  26. }
  27. OptionValueString(const char *current_value, const char *default_value) {
  28. if (current_value && current_value[0])
  29. m_current_value.assign(current_value);
  30. if (default_value && default_value[0])
  31. m_default_value.assign(default_value);
  32. }
  33. OptionValueString(const char *value, ValidatorCallback validator,
  34. void *baton = nullptr)
  35. : m_validator(validator), m_validator_baton(baton) {
  36. if (value && value[0]) {
  37. m_current_value.assign(value);
  38. m_default_value.assign(value);
  39. }
  40. }
  41. OptionValueString(const char *current_value, const char *default_value,
  42. ValidatorCallback validator, void *baton = nullptr)
  43. : m_validator(validator), m_validator_baton(baton) {
  44. if (current_value && current_value[0])
  45. m_current_value.assign(current_value);
  46. if (default_value && default_value[0])
  47. m_default_value.assign(default_value);
  48. }
  49. ~OptionValueString() override = default;
  50. // Virtual subclass pure virtual overrides
  51. OptionValue::Type GetType() const override { return eTypeString; }
  52. void DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
  53. uint32_t dump_mask) override;
  54. Status
  55. SetValueFromString(llvm::StringRef value,
  56. VarSetOperationType op = eVarSetOperationAssign) override;
  57. void Clear() override {
  58. m_current_value = m_default_value;
  59. m_value_was_set = false;
  60. }
  61. // Subclass specific functions
  62. Flags &GetOptions() { return m_options; }
  63. const Flags &GetOptions() const { return m_options; }
  64. const char *operator=(const char *value) {
  65. SetCurrentValue(value);
  66. return m_current_value.c_str();
  67. }
  68. const char *GetCurrentValue() const { return m_current_value.c_str(); }
  69. llvm::StringRef GetCurrentValueAsRef() const { return m_current_value; }
  70. const char *GetDefaultValue() const { return m_default_value.c_str(); }
  71. llvm::StringRef GetDefaultValueAsRef() const { return m_default_value; }
  72. Status SetCurrentValue(llvm::StringRef value);
  73. Status AppendToCurrentValue(const char *value);
  74. void SetDefaultValue(const char *value) {
  75. if (value && value[0])
  76. m_default_value.assign(value);
  77. else
  78. m_default_value.clear();
  79. }
  80. bool IsCurrentValueEmpty() const { return m_current_value.empty(); }
  81. bool IsDefaultValueEmpty() const { return m_default_value.empty(); }
  82. protected:
  83. std::string m_current_value;
  84. std::string m_default_value;
  85. Flags m_options;
  86. ValidatorCallback m_validator = nullptr;
  87. void *m_validator_baton = nullptr;
  88. };
  89. } // namespace lldb_private
  90. #endif // LLDB_INTERPRETER_OPTIONVALUESTRING_H