| 1 |
#ifndef SETTING_HH |
| 2 |
#define SETTING_HH |
| 3 |
|
| 4 |
#include "Subject.hh" |
| 5 |
#include "TclObject.hh" |
| 6 |
#include "static_string_view.hh" |
| 7 |
#include "strCat.hh" |
| 8 |
|
| 9 |
#include <cassert> |
| 10 |
#include <cstdint> |
| 11 |
#include <functional> |
| 12 |
#include <optional> |
| 13 |
#include <string_view> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
namespace openmsx { |
| 17 |
|
| 18 |
class CommandController; |
| 19 |
class GlobalCommandController; |
| 20 |
class Interpreter; |
| 21 |
|
| 22 |
class BaseSetting |
| 23 |
{ |
| 24 |
protected: |
| 25 |
explicit BaseSetting(std::string_view name); |
| 26 |
explicit BaseSetting(TclObject name); |
| 27 |
~BaseSetting() = default; |
| 28 |
|
| 29 |
public: |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
[[nodiscard]] const TclObject& getFullNameObj() const { return fullName; } |
| 38 |
[[nodiscard]] const TclObject& getBaseNameObj() const { return baseName; } |
| 39 |
[[nodiscard]] std::string_view getFullName() const { return fullName.getString(); } |
| 40 |
[[nodiscard]] std::string_view getBaseName() const { return baseName.getString(); } |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
void setPrefix(std::string_view prefix) { |
| 45 |
assert(prefix.starts_with("::")); |
| 46 |
fullName = tmpStrCat(prefix, getBaseName()); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
void info(TclObject& result) const; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
[[nodiscard]] virtual std::string_view getDescription() const = 0; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
[[nodiscard]] virtual std::string_view getTypeString() const = 0; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
virtual void additionalInfo(TclObject& result) const = 0; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
virtual void tabCompletion(std::vector<std::string>& tokens) const = 0; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
[[nodiscard]] virtual const TclObject& getValue() const = 0; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
[[nodiscard]] virtual std::optional<TclObject> getOptionalValue() const = 0; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
[[nodiscard]] virtual TclObject getDefaultValue() const = 0; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
virtual void setValue(const TclObject& value) = 0; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
virtual void setValueDirect(const TclObject& value) = 0; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
[[nodiscard]] virtual bool needLoadSave() const = 0; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
[[nodiscard]] virtual bool needTransfer() const = 0; |
| 112 |
|
| 113 |
private: |
| 114 |
TclObject fullName; |
| 115 |
const TclObject baseName; |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
class Setting : public BaseSetting, public Subject<Setting> |
| 120 |
{ |
| 121 |
public: |
| 122 |
enum class Save : uint8_t { |
| 123 |
YES, |
| 124 |
NO, |
| 125 |
NO_AND_DONT_TRANSFER, |
| 126 |
}; |
| 127 |
|
| 128 |
Setting(const Setting&) = delete; |
| 129 |
Setting(Setting&&) = delete; |
| 130 |
Setting& operator=(const Setting&) = delete; |
| 131 |
Setting& operator=(Setting&&) = delete; |
| 132 |
virtual ~Setting(); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
[[nodiscard]] const TclObject& getValue() const final { return value; } |
| 137 |
[[nodiscard]] std::optional<TclObject> getOptionalValue() const final { return value; } |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
void setChecker(std::function<void(TclObject&)> checkFunc_) { |
| 149 |
checkFunc = std::move(checkFunc_); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
void setValue(const TclObject& newValue) final; |
| 154 |
[[nodiscard]] std::string_view getDescription() const final; |
| 155 |
[[nodiscard]] TclObject getDefaultValue() const final { return defaultValue; } |
| 156 |
void setValueDirect(const TclObject& newValue) final; |
| 157 |
void tabCompletion(std::vector<std::string>& tokens) const override; |
| 158 |
[[nodiscard]] bool needLoadSave() const final; |
| 159 |
void additionalInfo(TclObject& result) const override; |
| 160 |
[[nodiscard]] bool needTransfer() const final; |
| 161 |
|
| 162 |
|
| 163 |
[[nodiscard]] CommandController& getCommandController() const { return commandController; } |
| 164 |
[[nodiscard]] Interpreter& getInterpreter() const; |
| 165 |
|
| 166 |
protected: |
| 167 |
Setting(CommandController& commandController, |
| 168 |
std::string_view name, static_string_view description, |
| 169 |
const TclObject& initialValue, Save save = Save::YES); |
| 170 |
void init(); |
| 171 |
void notifyPropertyChange() const; |
| 172 |
|
| 173 |
private: |
| 174 |
[[nodiscard]] GlobalCommandController& getGlobalCommandController() const; |
| 175 |
void notify() const; |
| 176 |
|
| 177 |
private: |
| 178 |
CommandController& commandController; |
| 179 |
const static_string_view description; |
| 180 |
std::function<void(TclObject&)> checkFunc; |
| 181 |
TclObject value; |
| 182 |
const TclObject defaultValue; |
| 183 |
const Save save; |
| 184 |
}; |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
#endif |