Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 9 | #ifndef LLDB_INTERPRETER_OPTIONVALUESTRING_H |
| 10 | #define LLDB_INTERPRETER_OPTIONVALUESTRING_H |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 11 | |
| 12 | #include <string> |
| 13 | |
| 14 | #include "lldb/Utility/Flags.h" |
| 15 | |
| 16 | #include "lldb/Interpreter/OptionValue.h" |
| 17 | |
| 18 | namespace lldb_private { |
| 19 | |
| 20 | class OptionValueString : public OptionValue { |
| 21 | public: |
| 22 | typedef Status (*ValidatorCallback)(const char *string, void *baton); |
| 23 | |
| 24 | enum Options { eOptionEncodeCharacterEscapeSequences = (1u << 0) }; |
| 25 | |
| 26 | OptionValueString() |
| 27 | : OptionValue(), m_current_value(), m_default_value(), m_options(), |
| 28 | m_validator(), m_validator_baton() {} |
| 29 | |
| 30 | OptionValueString(ValidatorCallback validator, void *baton = nullptr) |
| 31 | : OptionValue(), m_current_value(), m_default_value(), m_options(), |
| 32 | m_validator(validator), m_validator_baton(baton) {} |
| 33 | |
| 34 | OptionValueString(const char *value) |
| 35 | : OptionValue(), m_current_value(), m_default_value(), m_options(), |
| 36 | m_validator(), m_validator_baton() { |
| 37 | if (value && value[0]) { |
| 38 | m_current_value.assign(value); |
| 39 | m_default_value.assign(value); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | OptionValueString(const char *current_value, const char *default_value) |
| 44 | : OptionValue(), m_current_value(), m_default_value(), m_options(), |
| 45 | m_validator(), m_validator_baton() { |
| 46 | if (current_value && current_value[0]) |
| 47 | m_current_value.assign(current_value); |
| 48 | if (default_value && default_value[0]) |
| 49 | m_default_value.assign(default_value); |
| 50 | } |
| 51 | |
| 52 | OptionValueString(const char *value, ValidatorCallback validator, |
| 53 | void *baton = nullptr) |
| 54 | : OptionValue(), m_current_value(), m_default_value(), m_options(), |
| 55 | m_validator(validator), m_validator_baton(baton) { |
| 56 | if (value && value[0]) { |
| 57 | m_current_value.assign(value); |
| 58 | m_default_value.assign(value); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | OptionValueString(const char *current_value, const char *default_value, |
| 63 | ValidatorCallback validator, void *baton = nullptr) |
| 64 | : OptionValue(), m_current_value(), m_default_value(), m_options(), |
| 65 | m_validator(validator), m_validator_baton(baton) { |
| 66 | if (current_value && current_value[0]) |
| 67 | m_current_value.assign(current_value); |
| 68 | if (default_value && default_value[0]) |
| 69 | m_default_value.assign(default_value); |
| 70 | } |
| 71 | |
| 72 | ~OptionValueString() override = default; |
| 73 | |
| 74 | // Virtual subclass pure virtual overrides |
| 75 | |
| 76 | OptionValue::Type GetType() const override { return eTypeString; } |
| 77 | |
| 78 | void DumpValue(const ExecutionContext *exe_ctx, Stream &strm, |
| 79 | uint32_t dump_mask) override; |
| 80 | |
| 81 | Status |
| 82 | SetValueFromString(llvm::StringRef value, |
| 83 | VarSetOperationType op = eVarSetOperationAssign) override; |
| 84 | Status |
| 85 | SetValueFromString(const char *, |
| 86 | VarSetOperationType = eVarSetOperationAssign) = delete; |
| 87 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 88 | void Clear() override { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 89 | m_current_value = m_default_value; |
| 90 | m_value_was_set = false; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | lldb::OptionValueSP DeepCopy() const override; |
| 94 | |
| 95 | // Subclass specific functions |
| 96 | |
| 97 | Flags &GetOptions() { return m_options; } |
| 98 | |
| 99 | const Flags &GetOptions() const { return m_options; } |
| 100 | |
| 101 | const char *operator=(const char *value) { |
| 102 | SetCurrentValue(llvm::StringRef::withNullAsEmpty(value)); |
| 103 | return m_current_value.c_str(); |
| 104 | } |
| 105 | |
| 106 | const char *GetCurrentValue() const { return m_current_value.c_str(); } |
| 107 | llvm::StringRef GetCurrentValueAsRef() const { return m_current_value; } |
| 108 | |
| 109 | const char *GetDefaultValue() const { return m_default_value.c_str(); } |
| 110 | llvm::StringRef GetDefaultValueAsRef() const { return m_default_value; } |
| 111 | |
| 112 | Status SetCurrentValue(const char *) = delete; |
| 113 | Status SetCurrentValue(llvm::StringRef value); |
| 114 | |
| 115 | Status AppendToCurrentValue(const char *value); |
| 116 | |
| 117 | void SetDefaultValue(const char *value) { |
| 118 | if (value && value[0]) |
| 119 | m_default_value.assign(value); |
| 120 | else |
| 121 | m_default_value.clear(); |
| 122 | } |
| 123 | |
| 124 | bool IsCurrentValueEmpty() const { return m_current_value.empty(); } |
| 125 | |
| 126 | bool IsDefaultValueEmpty() const { return m_default_value.empty(); } |
| 127 | |
| 128 | protected: |
| 129 | std::string m_current_value; |
| 130 | std::string m_default_value; |
| 131 | Flags m_options; |
| 132 | ValidatorCallback m_validator; |
| 133 | void *m_validator_baton; |
| 134 | }; |
| 135 | |
| 136 | } // namespace lldb_private |
| 137 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 138 | #endif // LLDB_INTERPRETER_OPTIONVALUESTRING_H |