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 "strCat.hh"
7 #include "string_view.hh"
8 #include <functional>
9 #include <vector>
10 
11 namespace openmsx {
12 
13 class CommandController;
14 class GlobalCommandController;
15 class Interpreter;
16 
18 {
19 protected:
20  explicit BaseSetting(string_view name);
21  explicit BaseSetting(const TclObject& name);
22  ~BaseSetting() = default;
23 
24 public:
32  const TclObject& getFullNameObj() const { return fullName; }
33  const TclObject& getBaseNameObj() const { return baseName; }
34  string_view getFullName() const { return fullName.getString(); }
35  string_view getBaseName() const { return baseName.getString(); }
36 
39  void setPrefix(string_view prefix) {
40  assert(prefix.starts_with("::"));
41  fullName = strCat(prefix, getBaseName());
42  }
43 
46  void info(TclObject& result) const;
47 
49 
52  virtual string_view getDescription() const = 0;
53 
57  virtual string_view getTypeString() const = 0;
58 
61  virtual void additionalInfo(TclObject& result) const = 0;
62 
68  virtual void tabCompletion(std::vector<std::string>& tokens) const = 0;
69 
72  virtual const TclObject& getValue() const = 0;
73 
78  virtual TclObject getDefaultValue() const = 0;
79 
84  virtual TclObject getRestoreValue() const = 0;
85 
92  virtual void setValue(const TclObject& value) = 0;
93 
98  virtual void setValueDirect(const TclObject& value) = 0;
99 
102  virtual bool needLoadSave() const = 0;
103 
106  virtual bool needTransfer() const = 0;
107 
110  virtual void setDontSaveValue(const TclObject& dontSaveValue) = 0;
111 
112 private:
113  TclObject fullName;
114  const TclObject baseName;
115 };
116 
117 
118 class Setting : public BaseSetting, public Subject<Setting>
119 {
120 public:
121  Setting(const Setting&) = delete;
122  Setting& operator=(const Setting&) = delete;
123 
124  enum SaveSetting {
125  SAVE, // save, transfer
126  DONT_SAVE, // no-save, transfer
127  DONT_TRANSFER, // no-save, no-transfer
128  };
129 
130  virtual ~Setting();
131 
134  const TclObject& getValue() const final override { return value; }
135 
138  void setRestoreValue(const TclObject& newRestoreValue) {
139  restoreValue = newRestoreValue;
140  }
141 
151  void setChecker(std::function<void(TclObject&)> checkFunc_) {
152  checkFunc = std::move(checkFunc_);
153  }
154 
155  // BaseSetting
156  void setValue(const TclObject& newValue) final override;
157  string_view getDescription() const final override;
158  TclObject getDefaultValue() const final override { return defaultValue; }
159  TclObject getRestoreValue() const final override { return restoreValue; }
160  void setValueDirect(const TclObject& newValue) final override;
161  void tabCompletion(std::vector<std::string>& tokens) const override;
162  bool needLoadSave() const final override;
163  void additionalInfo(TclObject& result) const override;
164  bool needTransfer() const final override;
165  void setDontSaveValue(const TclObject& dontSaveValue) final override;
166 
167  // convenience functions
168  CommandController& getCommandController() const { return commandController; }
169  Interpreter& getInterpreter() const;
170 
171 protected:
172  Setting(CommandController& commandController,
173  string_view name, string_view description,
174  const TclObject& initialValue, SaveSetting save = SAVE);
175  void init();
176  void notifyPropertyChange() const;
177 
178 private:
179  GlobalCommandController& getGlobalCommandController() const;
180  void notify() const;
181 
182 private:
183  CommandController& commandController;
184  const std::string description;
185  std::function<void(TclObject&)> checkFunc;
186  TclObject value; // TODO can we share the underlying Tcl var storage?
187  const TclObject defaultValue;
188  TclObject restoreValue;
189  TclObject dontSaveValue;
190  const SaveSetting save;
191 };
192 
193 } // namespace openmsx
194 
195 #endif
virtual void tabCompletion(std::vector< std::string > &tokens) const =0
Complete a partly typed value.
const TclObject & getFullNameObj() const
Get the name of this setting.
Definition: Setting.hh:32
void setChecker(std::function< void(TclObject &)> checkFunc_)
Set value-check-callback.
Definition: Setting.hh:151
bool starts_with(string_view x) const
Definition: string_view.cc:116
virtual const TclObject & getValue() const =0
Get current value as a TclObject.
virtual string_view getTypeString() const =0
Returns a string describing the setting type (integer, string, ..) Could be used in a GUI to pick an ...
string_view getString() const
Definition: TclObject.cc:102
virtual string_view getDescription() const =0
pure virtual methods ///
const TclObject & getBaseNameObj() const
Definition: Setting.hh:33
virtual void setValue(const TclObject &value)=0
Change the value of this setting to the given value.
virtual TclObject getDefaultValue() const =0
Get the default value of this setting.
const TclObject & getValue() const final override
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:134
string_view getFullName() const
Definition: Setting.hh:34
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
TclObject getRestoreValue() const final override
Get the value that will be set after a Tcl &#39;unset&#39; command.
Definition: Setting.hh:159
virtual void setDontSaveValue(const TclObject &dontSaveValue)=0
This value will never end up in the settings.xml file.
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
void info(TclObject &result) const
For SettingInfo.
Definition: Setting.cc:30
Generic Gang-of-Four Subject class of the Observer pattern, templatized edition.
Definition: Subject.hh:17
BaseSetting(string_view name)
Definition: Setting.cc:18
virtual bool needLoadSave() const =0
Does this setting need to be loaded or saved (settings.xml).
void setRestoreValue(const TclObject &newRestoreValue)
Set restore value.
Definition: Setting.hh:138
TclObject getDefaultValue() const final override
Get the default value of this setting.
Definition: Setting.hh:158
string_view getBaseName() const
Definition: Setting.hh:35
void setPrefix(string_view prefix)
Set a machine specific prefix.
Definition: Setting.hh:39
std::string strCat(Ts &&...ts)
Definition: strCat.hh:577
virtual void additionalInfo(TclObject &result) const =0
Helper method for info().
CommandController & getCommandController() const
Definition: Setting.hh:168
virtual bool needTransfer() const =0
Does this setting need to be transfered on reverse.
virtual TclObject getRestoreValue() const =0
Get the value that will be set after a Tcl &#39;unset&#39; command.
virtual void setValueDirect(const TclObject &value)=0
Similar to setValue(), but doesn&#39;t trigger Tcl traces.