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"
7#include "strCat.hh"
8#include <cassert>
9#include <functional>
10#include <optional>
11#include <string_view>
12#include <vector>
13
14namespace openmsx {
15
16class CommandController;
17class GlobalCommandController;
18class Interpreter;
19
21{
22protected:
23 explicit BaseSetting(std::string_view name);
24 explicit BaseSetting(TclObject name);
25 ~BaseSetting() = default;
26
27public:
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(prefix.starts_with("::"));
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
87 [[nodiscard]] virtual TclObject getDefaultValue() const = 0;
88
95 virtual void setValue(const TclObject& value) = 0;
96
101 virtual void setValueDirect(const TclObject& value) = 0;
102
105 [[nodiscard]] virtual bool needLoadSave() const = 0;
106
109 [[nodiscard]] virtual bool needTransfer() const = 0;
110
111private:
112 TclObject fullName;
113 const TclObject baseName;
114};
115
116
117class Setting : public BaseSetting, public Subject<Setting>
118{
119public:
120 Setting(const Setting&) = delete;
121 Setting& operator=(const Setting&) = delete;
122
124 SAVE, // save, transfer
125 DONT_SAVE, // no-save, transfer
126 DONT_TRANSFER, // no-save, no-transfer
127 };
128
129 virtual ~Setting();
130
133 [[nodiscard]] const TclObject& getValue() const final { return value; }
134 [[nodiscard]] std::optional<TclObject> getOptionalValue() const final { return value; }
135
145 void setChecker(std::function<void(TclObject&)> checkFunc_) {
146 checkFunc = std::move(checkFunc_);
147 }
148
149 // BaseSetting
150 void setValue(const TclObject& newValue) final;
151 [[nodiscard]] std::string_view getDescription() const final;
152 [[nodiscard]] TclObject getDefaultValue() const final { return defaultValue; }
153 void setValueDirect(const TclObject& newValue) final;
154 void tabCompletion(std::vector<std::string>& tokens) const override;
155 [[nodiscard]] bool needLoadSave() const final;
156 void additionalInfo(TclObject& result) const override;
157 [[nodiscard]] bool needTransfer() const final;
158
159 // convenience functions
160 [[nodiscard]] CommandController& getCommandController() const { return commandController; }
161 [[nodiscard]] Interpreter& getInterpreter() const;
162
163protected:
164 Setting(CommandController& commandController,
165 std::string_view name, static_string_view description,
166 const TclObject& initialValue, SaveSetting save = SAVE);
167 void init();
168 void notifyPropertyChange() const;
169
170private:
171 [[nodiscard]] GlobalCommandController& getGlobalCommandController() const;
172 void notify() const;
173
174private:
175 CommandController& commandController;
176 const static_string_view description;
177 std::function<void(TclObject&)> checkFunc;
178 TclObject value; // TODO can we share the underlying Tcl var storage?
179 const TclObject defaultValue;
180 const SaveSetting save;
181};
182
183} // namespace openmsx
184
185#endif
virtual void setValue(const TclObject &value)=0
Change the value of this setting to the given value.
virtual std::optional< TclObject > getOptionalValue() const =0
Like getValue(), but in case of error returns an empty optional instead of throwing an exception.
virtual void additionalInfo(TclObject &result) const =0
Helper method for info().
void info(TclObject &result) const
For SettingInfo.
Definition Setting.cc:27
virtual void setValueDirect(const TclObject &value)=0
Similar to setValue(), but doesn't trigger Tcl traces.
virtual const TclObject & getValue() const =0
Get current value as a TclObject.
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
const TclObject & getFullNameObj() const
Get the name of this setting.
Definition Setting.hh:35
virtual void tabCompletion(std::vector< std::string > &tokens) const =0
Complete a partly typed value.
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
const TclObject & getBaseNameObj() const
Definition Setting.hh:36
virtual ~Setting()
Definition Setting.cc:70
void setChecker(std::function< void(TclObject &)> checkFunc_)
Set value-check-callback.
Definition Setting.hh:145
std::string_view getDescription() const final
pure virtual methods ///
Definition Setting.cc:76
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:134
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
Setting & operator=(const Setting &)=delete
CommandController & getCommandController() const
Definition Setting.hh:160
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:152
const TclObject & getValue() const final
Gets the current value of this setting as a TclObject.
Definition Setting.hh:133
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
Generic Gang-of-Four Subject class of the Observer pattern, templatized edition.
Definition Subject.hh:18
zstring_view getString() const
Definition TclObject.cc:141
static_string_view
This file implemented 3 utility functions:
Definition Autofire.cc:11
TemporaryString tmpStrCat(Ts &&... ts)
Definition strCat.hh:742