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 "StringOp.hh"
8 #include <functional>
9 #include <string_view>
10 #include <vector>
11 
12 namespace openmsx {
13 
14 class CommandController;
15 class GlobalCommandController;
16 class Interpreter;
17 
19 {
20 protected:
21  explicit BaseSetting(std::string_view name);
22  explicit BaseSetting(const TclObject& name);
23  ~BaseSetting() = default;
24 
25 public:
33  const TclObject& getFullNameObj() const { return fullName; }
34  const TclObject& getBaseNameObj() const { return baseName; }
35  std::string_view getFullName() const { return fullName.getString(); }
36  std::string_view getBaseName() const { return baseName.getString(); }
37 
40  void setPrefix(std::string_view prefix) {
41  assert(StringOp::startsWith(prefix, "::"));
42  fullName = strCat(prefix, getBaseName());
43  }
44 
47  void info(TclObject& result) const;
48 
50 
53  virtual std::string_view getDescription() const = 0;
54 
58  virtual std::string_view getTypeString() const = 0;
59 
62  virtual void additionalInfo(TclObject& result) const = 0;
63 
69  virtual void tabCompletion(std::vector<std::string>& tokens) const = 0;
70 
73  virtual const TclObject& getValue() const = 0;
74 
79  virtual TclObject getDefaultValue() const = 0;
80 
85  virtual TclObject getRestoreValue() const = 0;
86 
93  virtual void setValue(const TclObject& value) = 0;
94 
99  virtual void setValueDirect(const TclObject& value) = 0;
100 
103  virtual bool needLoadSave() const = 0;
104 
107  virtual bool needTransfer() const = 0;
108 
111  virtual void setDontSaveValue(const TclObject& dontSaveValue) = 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  Setting(const Setting&) = delete;
123  Setting& operator=(const Setting&) = delete;
124 
125  enum SaveSetting {
126  SAVE, // save, transfer
127  DONT_SAVE, // no-save, transfer
128  DONT_TRANSFER, // no-save, no-transfer
129  };
130 
131  virtual ~Setting();
132 
135  const TclObject& getValue() const final override { return value; }
136 
139  void setRestoreValue(const TclObject& newRestoreValue) {
140  restoreValue = newRestoreValue;
141  }
142 
152  void setChecker(std::function<void(TclObject&)> checkFunc_) {
153  checkFunc = std::move(checkFunc_);
154  }
155 
156  // BaseSetting
157  void setValue(const TclObject& newValue) final override;
158  std::string_view getDescription() const final override;
159  TclObject getDefaultValue() const final override { return defaultValue; }
160  TclObject getRestoreValue() const final override { return restoreValue; }
161  void setValueDirect(const TclObject& newValue) final override;
162  void tabCompletion(std::vector<std::string>& tokens) const override;
163  bool needLoadSave() const final override;
164  void additionalInfo(TclObject& result) const override;
165  bool needTransfer() const final override;
166  void setDontSaveValue(const TclObject& dontSaveValue) final override;
167 
168  // convenience functions
169  CommandController& getCommandController() const { return commandController; }
170  Interpreter& getInterpreter() const;
171 
172 protected:
173  Setting(CommandController& commandController,
174  std::string_view name, std::string_view description,
175  const TclObject& initialValue, SaveSetting save = SAVE);
176  void init();
177  void notifyPropertyChange() const;
178 
179 private:
180  GlobalCommandController& getGlobalCommandController() const;
181  void notify() const;
182 
183 private:
184  CommandController& commandController;
185  const std::string description;
186  std::function<void(TclObject&)> checkFunc;
187  TclObject value; // TODO can we share the underlying Tcl var storage?
188  const TclObject defaultValue;
189  TclObject restoreValue;
190  TclObject dontSaveValue;
191  const SaveSetting save;
192 };
193 
194 } // namespace openmsx
195 
196 #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:33
void setChecker(std::function< void(TclObject &)> checkFunc_)
Set value-check-callback.
Definition: Setting.hh:152
virtual const TclObject & getValue() const =0
Get current value as a TclObject.
BaseSetting(std::string_view name)
void setPrefix(std::string_view prefix)
Set a machine specific prefix.
Definition: Setting.hh:40
bool startsWith(string_view total, string_view part)
Definition: StringOp.cc:71
const TclObject & getBaseNameObj() const
Definition: Setting.hh:34
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.
virtual std::string_view getDescription() const =0
pure virtual methods ///
const TclObject & getValue() const final override
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:135
std::string_view getBaseName() const
Definition: Setting.hh:36
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:160
virtual void setDontSaveValue(const TclObject &dontSaveValue)=0
This value will never end up in the settings.xml file.
std::string_view getString() const
Definition: TclObject.cc:102
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 ...
void info(TclObject &result) const
For SettingInfo.
Definition: Setting.cc:31
Generic Gang-of-Four Subject class of the Observer pattern, templatized edition.
Definition: Subject.hh:17
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:139
TclObject getDefaultValue() const final override
Get the default value of this setting.
Definition: Setting.hh:159
std::string_view getFullName() const
Definition: Setting.hh:35
std::string strCat(Ts &&...ts)
Definition: strCat.hh:573
virtual void additionalInfo(TclObject &result) const =0
Helper method for info().
CommandController & getCommandController() const
Definition: Setting.hh:169
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.