openMSX
Setting.cc
Go to the documentation of this file.
1#include "Setting.hh"
5#include "SettingsConfig.hh"
6#include "TclObject.hh"
7#include "CliComm.hh"
8#include "MSXException.hh"
9#include "checked_cast.hh"
10
11namespace openmsx {
12
13// class BaseSetting
14
15BaseSetting::BaseSetting(std::string_view name)
16 : fullName(name)
17 , baseName(fullName)
18{
19}
20
22 : fullName(std::move(name))
23 , baseName(fullName)
24{
25}
26
27void BaseSetting::info(TclObject& result) const
28{
30 additionalInfo(result);
31}
32
33
34// class Setting
35
37 std::string_view name, static_string_view description_,
38 const TclObject& initialValue, Save save_)
39 : BaseSetting(name)
40 , commandController(commandController_)
41 , description(description_)
42 , value(initialValue)
43 , defaultValue(initialValue)
44 , save(save_)
45{
46 checkFunc = [](const TclObject&) { /* nothing */ };
47}
48
50{
51 if (needLoadSave()) {
52 if (const auto* savedValue =
53 getGlobalCommandController().getSettingsConfig()
54 .getValueForSetting(getBaseName())) {
55 try {
56 setValueDirect(TclObject(*savedValue));
57 } catch (MSXException&) {
58 // saved value no longer valid, just keep default
59 }
60 }
61 }
63
64 // This is needed to for example inform catapult of the new setting
65 // value when a setting was destroyed/recreated (by a machine switch
66 // for example).
67 notify();
68}
69
74
75
76std::string_view Setting::getDescription() const
77{
78 return description;
79}
80
81void Setting::setValue(const TclObject& newValue)
82{
84}
85
86void Setting::notify() const
87{
88 // Notify all subsystems of a change in the setting value. There
89 // are actually quite a few subsystems involved with the settings:
90 // - the setting classes themselves
91 // - the Tcl variables (and possibly traces on those variables)
92 // - Subject/Observers
93 // - CliComm setting-change events (for external GUIs)
94 // - SettingsConfig (keeps values, also of not yet created settings)
95 // This method takes care of the last 3 in this list.
97 auto base = getBaseName();
98 TclObject val = getValue();
99 commandController.getCliComm().updateFiltered(
101
102 // Always keep SettingsConfig in sync.
103 auto& config = getGlobalCommandController().getSettingsConfig();
104 if (!needLoadSave() || (val == getDefaultValue())) {
105 config.removeValueForSetting(base);
106 } else {
107 config.setValueForSetting(base, val.getString());
108 }
109}
110
112{
113 TclObject result;
114 info(result);
115 commandController.getCliComm().updateFiltered(
117}
118
120{
121 return save == Save::YES;
122}
124{
125 return save != Save::NO_AND_DONT_TRANSFER;
126}
127
128GlobalCommandController& Setting::getGlobalCommandController() const
129{
130 if (auto* globalCommandController =
131 dynamic_cast<GlobalCommandController*>(&commandController)) {
132 return *globalCommandController;
133 } else {
134 return checked_cast<MSXCommandController*>(&commandController)
135 ->getGlobalCommandController();
136 }
137}
138
140{
141 return commandController.getInterpreter();
142}
143
144void Setting::tabCompletion(std::vector<std::string>& /*tokens*/) const
145{
146 // nothing
147}
148
149void Setting::additionalInfo(TclObject& /*result*/) const
150{
151 // nothing
152}
153
154
155void Setting::setValueDirect(const TclObject& newValue_)
156{
157 TclObject newValue = newValue_;
158 checkFunc(newValue);
159 if (newValue != value) {
160 value = newValue;
161 notify();
162 }
163
164 // synchronize proxy
165 const auto* controller = dynamic_cast<MSXCommandController*>(
167 if (!controller) {
168 // This is not a machine specific setting.
169 return;
170 }
171 if (!controller->isActive()) {
172 // This setting does not belong to the active machine.
173 return;
174 }
175
176 // Tcl already makes sure this doesn't result in an endless loop.
177 try {
179 } catch (MSXException&) {
180 // ignore
181 }
182}
183
184} // namespace openmsx
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
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.
const TclObject & getFullNameObj() const
Get the name of this setting.
Definition Setting.hh:35
std::string_view getBaseName() const
Definition Setting.hh:38
const TclObject & getBaseNameObj() const
Definition Setting.hh:36
virtual void updateFiltered(UpdateType type, std::string_view name, std::string_view value)=0
Same as update(), but checks that the value for type-name is the same as in the previous call.
virtual Interpreter & getInterpreter()=0
virtual CliComm & getCliComm()=0
virtual void registerSetting(Setting &setting)=0
TODO.
virtual void unregisterSetting(Setting &setting)=0
void setVariable(const TclObject &name, const TclObject &value)
virtual ~Setting()
Definition Setting.cc:70
std::string_view getDescription() const final
pure virtual methods ///
Definition Setting.cc:76
void tabCompletion(std::vector< std::string > &tokens) const override
Complete a partly typed value.
Definition Setting.cc:144
bool needLoadSave() const final
Does this setting need to be loaded or saved (settings.xml).
Definition Setting.cc:119
CommandController & getCommandController() const
Definition Setting.hh:161
bool needTransfer() const final
Does this setting need to be transfered on reverse.
Definition Setting.cc:123
Interpreter & getInterpreter() const
Definition Setting.cc:139
TclObject getDefaultValue() const final
Get the default value of this setting.
Definition Setting.hh:153
const TclObject & getValue() const final
Gets the current value of this setting as a TclObject.
Definition Setting.hh:134
void setValue(const TclObject &newValue) final
Change the value of this setting to the given value.
Definition Setting.cc:81
void notifyPropertyChange() const
Definition Setting.cc:111
void additionalInfo(TclObject &result) const override
Helper method for info().
Definition Setting.cc:149
Setting(const Setting &)=delete
void setValueDirect(const TclObject &newValue) final
Similar to setValue(), but doesn't trigger Tcl traces.
Definition Setting.cc:155
void removeValueForSetting(std::string_view setting)
void notify() const
Definition Subject.hh:71
void addListElement(const T &t)
Definition TclObject.hh:133
zstring_view getString() const
Definition TclObject.cc:141
static_string_view
This file implemented 3 utility functions:
Definition Autofire.cc:11
STL namespace.