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(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
Subject.hh
openmsx::Setting::getInterpreter
Interpreter & getInterpreter() const
Definition: Setting.cc:162
StringOp::startsWith
bool startsWith(string_view total, string_view part)
Definition: StringOp.cc:33
openmsx::BaseSetting::setDontSaveValue
virtual void setDontSaveValue(const TclObject &dontSaveValue)=0
This value will never end up in the settings.xml file.
openmsx::Setting::getDefaultValue
TclObject getDefaultValue() const final override
Get the default value of this setting.
Definition: Setting.hh:159
openmsx::CommandController
Definition: CommandController.hh:18
openmsx::BaseSetting::getTypeString
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 ...
TclObject.hh
openmsx::BaseSetting::getBaseName
std::string_view getBaseName() const
Definition: Setting.hh:36
openmsx::Setting::getValue
const TclObject & getValue() const final override
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:135
openmsx::Setting::tabCompletion
void tabCompletion(std::vector< std::string > &tokens) const override
Complete a partly typed value.
Definition: Setting.cc:167
openmsx::Setting::needTransfer
bool needTransfer() const final override
Does this setting need to be transfered on reverse.
Definition: Setting.cc:141
openmsx::Setting::setValue
void setValue(const TclObject &newValue) final override
Change the value of this setting to the given value.
Definition: Setting.cc:89
openmsx::Setting::needLoadSave
bool needLoadSave() const final override
Does this setting need to be loaded or saved (settings.xml).
Definition: Setting.cc:137
openmsx::Setting::DONT_SAVE
@ DONT_SAVE
Definition: Setting.hh:127
openmsx::Setting::getCommandController
CommandController & getCommandController() const
Definition: Setting.hh:169
openmsx::BaseSetting::info
void info(TclObject &result) const
For SettingInfo.
Definition: Setting.cc:31
openmsx::Setting::Setting
Setting(const Setting &)=delete
openmsx::Setting::setDontSaveValue
void setDontSaveValue(const TclObject &dontSaveValue) final override
This value will never end up in the settings.xml file.
Definition: Setting.cc:146
openmsx::BaseSetting::getFullName
std::string_view getFullName() const
Definition: Setting.hh:35
openmsx::Setting
Definition: Setting.hh:120
openmsx::BaseSetting::additionalInfo
virtual void additionalInfo(TclObject &result) const =0
Helper method for info().
openmsx::BaseSetting::needLoadSave
virtual bool needLoadSave() const =0
Does this setting need to be loaded or saved (settings.xml).
openmsx::Setting::operator=
Setting & operator=(const Setting &)=delete
openmsx::BaseSetting::getRestoreValue
virtual TclObject getRestoreValue() const =0
Get the value that will be set after a Tcl 'unset' command.
openmsx::Setting::setRestoreValue
void setRestoreValue(const TclObject &newRestoreValue)
Set restore value.
Definition: Setting.hh:139
openmsx::BaseSetting::setValueDirect
virtual void setValueDirect(const TclObject &value)=0
Similar to setValue(), but doesn't trigger Tcl traces.
openmsx::Setting::getDescription
std::string_view getDescription() const final override
pure virtual methods ///
Definition: Setting.cc:84
openmsx::BaseSetting::getFullNameObj
const TclObject & getFullNameObj() const
Get the name of this setting.
Definition: Setting.hh:33
openmsx::BaseSetting::needTransfer
virtual bool needTransfer() const =0
Does this setting need to be transfered on reverse.
openmsx::BaseSetting::BaseSetting
BaseSetting(std::string_view name)
Definition: Setting.cc:19
openmsx::BaseSetting::~BaseSetting
~BaseSetting()=default
openmsx::BaseSetting
Definition: Setting.hh:19
openmsx::Setting::additionalInfo
void additionalInfo(TclObject &result) const override
Helper method for info().
Definition: Setting.cc:172
openmsx::Setting::init
void init()
Definition: Setting.cc:54
openmsx::BaseSetting::tabCompletion
virtual void tabCompletion(std::vector< std::string > &tokens) const =0
Complete a partly typed value.
openmsx::Setting::DONT_TRANSFER
@ DONT_TRANSFER
Definition: Setting.hh:128
openmsx::Setting::~Setting
virtual ~Setting()
Definition: Setting.cc:78
openmsx::Subject
Generic Gang-of-Four Subject class of the Observer pattern, templatized edition.
Definition: Subject.hh:18
openmsx::Setting::setValueDirect
void setValueDirect(const TclObject &newValue) final override
Similar to setValue(), but doesn't trigger Tcl traces.
Definition: Setting.cc:178
openmsx::BaseSetting::getValue
virtual const TclObject & getValue() const =0
Get current value as a TclObject.
openmsx::Setting::SaveSetting
SaveSetting
Definition: Setting.hh:125
openmsx::BaseSetting::setPrefix
void setPrefix(std::string_view prefix)
Set a machine specific prefix.
Definition: Setting.hh:40
StringOp.hh
strCat.hh
openmsx::Setting::getRestoreValue
TclObject getRestoreValue() const final override
Get the value that will be set after a Tcl 'unset' command.
Definition: Setting.hh:160
openmsx::Setting::SAVE
@ SAVE
Definition: Setting.hh:126
openmsx::TclObject::getString
std::string_view getString() const
Definition: TclObject.cc:102
openmsx::Setting::notifyPropertyChange
void notifyPropertyChange() const
Definition: Setting.cc:129
openmsx::TclObject
Definition: TclObject.hh:22
openmsx::Setting::setChecker
void setChecker(std::function< void(TclObject &)> checkFunc_)
Set value-check-callback.
Definition: Setting.hh:152
openmsx::BaseSetting::getBaseNameObj
const TclObject & getBaseNameObj() const
Definition: Setting.hh:34
openmsx::BaseSetting::setValue
virtual void setValue(const TclObject &value)=0
Change the value of this setting to the given value.
openmsx::BaseSetting::getDescription
virtual std::string_view getDescription() const =0
pure virtual methods ///
openmsx::Interpreter
Definition: Interpreter.hh:17
strCat
std::string strCat(Ts &&...ts)
Definition: strCat.hh:573
openmsx::GlobalCommandController
Definition: GlobalCommandController.hh:37
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::BaseSetting::getDefaultValue
virtual TclObject getDefaultValue() const =0
Get the default value of this setting.