openMSX
Setting.hh
Go to the documentation of this file.
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 #include "StringOp.hh"
9 #include <functional>
10 #include <optional>
11 #include <string_view>
12 #include <vector>
13 
14 namespace openmsx {
15 
16 class CommandController;
17 class GlobalCommandController;
18 class Interpreter;
19 
21 {
22 protected:
23  explicit BaseSetting(std::string_view name);
24  explicit BaseSetting(TclObject name);
25  ~BaseSetting() = default;
26 
27 public:
35  [[nodiscard]] const TclObject& getFullNameObj() const { return fullName; }
36  [[nodiscard]] const TclObject& getBaseNameObj() const { return baseName; }
37  [[nodiscard]] std::string_view getFullName() const { return fullName.getString(); }
38  [[nodiscard]] std::string_view getBaseName() const { return baseName.getString(); }
39 
42  void setPrefix(std::string_view prefix) {
43  assert(StringOp::startsWith(prefix, "::"));
44  fullName = tmpStrCat(prefix, getBaseName());
45  }
46 
49  void info(TclObject& result) const;
50 
52 
55  [[nodiscard]] virtual std::string_view getDescription() const = 0;
56 
60  [[nodiscard]] virtual std::string_view getTypeString() const = 0;
61 
64  virtual void additionalInfo(TclObject& result) const = 0;
65 
71  virtual void tabCompletion(std::vector<std::string>& tokens) const = 0;
72 
75  [[nodiscard]] virtual const TclObject& getValue() const = 0;
76 
80  [[nodiscard]] virtual std::optional<TclObject> getOptionalValue() const = 0;
81 
86  [[nodiscard]] virtual TclObject getDefaultValue() const = 0;
87 
92  [[nodiscard]] virtual TclObject getRestoreValue() const = 0;
93 
100  virtual void setValue(const TclObject& value) = 0;
101 
106  virtual void setValueDirect(const TclObject& value) = 0;
107 
110  [[nodiscard]] virtual bool needLoadSave() const = 0;
111 
114  [[nodiscard]] virtual bool needTransfer() const = 0;
115 
118  virtual void setDontSaveValue(const TclObject& dontSaveValue) = 0;
119 
120 private:
121  TclObject fullName;
122  const TclObject baseName;
123 };
124 
125 
126 class Setting : public BaseSetting, public Subject<Setting>
127 {
128 public:
129  Setting(const Setting&) = delete;
130  Setting& operator=(const Setting&) = delete;
131 
132  enum SaveSetting {
133  SAVE, // save, transfer
134  DONT_SAVE, // no-save, transfer
135  DONT_TRANSFER, // no-save, no-transfer
136  };
137 
138  virtual ~Setting();
139 
142  [[nodiscard]] const TclObject& getValue() const final { return value; }
143  [[nodiscard]] std::optional<TclObject> getOptionalValue() const final { return value; }
144 
147  void setRestoreValue(const TclObject& newRestoreValue) {
148  restoreValue = newRestoreValue;
149  }
150 
160  void setChecker(std::function<void(TclObject&)> checkFunc_) {
161  checkFunc = std::move(checkFunc_);
162  }
163 
164  // BaseSetting
165  void setValue(const TclObject& newValue) final;
166  [[nodiscard]] std::string_view getDescription() const final;
167  [[nodiscard]] TclObject getDefaultValue() const final { return defaultValue; }
168  [[nodiscard]] TclObject getRestoreValue() const final { return restoreValue; }
169  void setValueDirect(const TclObject& newValue) final;
170  void tabCompletion(std::vector<std::string>& tokens) const override;
171  [[nodiscard]] bool needLoadSave() const final;
172  void additionalInfo(TclObject& result) const override;
173  [[nodiscard]] bool needTransfer() const final;
174  void setDontSaveValue(const TclObject& dontSaveValue) final;
175 
176  // convenience functions
177  [[nodiscard]] CommandController& getCommandController() const { return commandController; }
178  [[nodiscard]] Interpreter& getInterpreter() const;
179 
180 protected:
181  Setting(CommandController& commandController,
182  std::string_view name, static_string_view description,
183  const TclObject& initialValue, SaveSetting save = SAVE);
184  void init();
185  void notifyPropertyChange() const;
186 
187 private:
188  [[nodiscard]] GlobalCommandController& getGlobalCommandController() const;
189  void notify() const;
190 
191 private:
192  CommandController& commandController;
193  const static_string_view description;
194  std::function<void(TclObject&)> checkFunc;
195  TclObject value; // TODO can we share the underlying Tcl var storage?
196  const TclObject defaultValue;
197  TclObject restoreValue;
198  TclObject dontSaveValue;
199  const SaveSetting save;
200 };
201 
202 } // namespace openmsx
203 
204 #endif
virtual void setValue(const TclObject &value)=0
Change the value of this setting to the given value.
virtual void additionalInfo(TclObject &result) const =0
Helper method for info().
BaseSetting(std::string_view name)
Definition: Setting.cc:15
void info(TclObject &result) const
For SettingInfo.
Definition: Setting.cc:27
const TclObject & getBaseNameObj() const
Definition: Setting.hh:36
virtual void setValueDirect(const TclObject &value)=0
Similar to setValue(), but doesn't trigger Tcl traces.
virtual void setDontSaveValue(const TclObject &dontSaveValue)=0
This value will never end up in the settings.xml file.
virtual bool needLoadSave() const =0
Does this setting need to be loaded or saved (settings.xml).
virtual std::string_view getTypeString() const =0
Returns a string describing the setting type (integer, string, ..) Could be used in a GUI to pick an ...
virtual TclObject getDefaultValue() const =0
Get the default value of this setting.
void setPrefix(std::string_view prefix)
Set a machine specific prefix.
Definition: Setting.hh:42
virtual void tabCompletion(std::vector< std::string > &tokens) const =0
Complete a partly typed value.
virtual TclObject getRestoreValue() const =0
Get the value that will be set after a Tcl 'unset' command.
virtual std::optional< TclObject > getOptionalValue() const =0
Like getValue(), but in case of error returns an empty optional instead of throwing an exception.
const TclObject & getFullNameObj() const
Get the name of this setting.
Definition: Setting.hh:35
virtual const TclObject & getValue() const =0
Get current value as a TclObject.
virtual bool needTransfer() const =0
Does this setting need to be transfered on reverse.
virtual std::string_view getDescription() const =0
pure virtual methods ///
std::string_view getBaseName() const
Definition: Setting.hh:38
std::string_view getFullName() const
Definition: Setting.hh:37
virtual ~Setting()
Definition: Setting.cc:71
void setChecker(std::function< void(TclObject &)> checkFunc_)
Set value-check-callback.
Definition: Setting.hh:160
Setting & operator=(const Setting &)=delete
std::string_view getDescription() const final
pure virtual methods ///
Definition: Setting.cc:77
void tabCompletion(std::vector< std::string > &tokens) const override
Complete a partly typed value.
Definition: Setting.cc:153
const TclObject & getValue() const final
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:142
void setRestoreValue(const TclObject &newRestoreValue)
Set restore value.
Definition: Setting.hh:147
bool needLoadSave() const final
Does this setting need to be loaded or saved (settings.xml).
Definition: Setting.cc:123
std::optional< TclObject > getOptionalValue() const final
Like getValue(), but in case of error returns an empty optional instead of throwing an exception.
Definition: Setting.hh:143
void setDontSaveValue(const TclObject &dontSaveValue) final
This value will never end up in the settings.xml file.
Definition: Setting.cc:132
bool needTransfer() const final
Does this setting need to be transfered on reverse.
Definition: Setting.cc:127
Interpreter & getInterpreter() const
Definition: Setting.cc:148
TclObject getRestoreValue() const final
Get the value that will be set after a Tcl 'unset' command.
Definition: Setting.hh:168
TclObject getDefaultValue() const final
Get the default value of this setting.
Definition: Setting.hh:167
CommandController & getCommandController() const
Definition: Setting.hh:177
void setValue(const TclObject &newValue) final
Change the value of this setting to the given value.
Definition: Setting.cc:82
void notifyPropertyChange() const
Definition: Setting.cc:115
void additionalInfo(TclObject &result) const override
Helper method for info().
Definition: Setting.cc:158
Setting(const Setting &)=delete
void setValueDirect(const TclObject &newValue) final
Similar to setValue(), but doesn't trigger Tcl traces.
Definition: Setting.cc:164
Generic Gang-of-Four Subject class of the Observer pattern, templatized edition.
Definition: Subject.hh:18
zstring_view getString() const
Definition: TclObject.cc:111
static_string_view
bool startsWith(string_view total, string_view part)
Definition: StringOp.cc:29
This file implemented 3 utility functions:
Definition: Autofire.cc:9
TemporaryString tmpStrCat(Ts &&... ts)
Definition: strCat.hh:659