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 enum class Save {
121 YES, // save, transfer
122 NO, // no-save, transfer
123 NO_AND_DONT_TRANSFER, // no-save, no-transfer
124 };
125
126 Setting(const Setting&) = delete;
127 Setting(Setting&&) = delete;
128 Setting& operator=(const Setting&) = delete;
130 virtual ~Setting();
131
134 [[nodiscard]] const TclObject& getValue() const final { return value; }
135 [[nodiscard]] std::optional<TclObject> getOptionalValue() const final { return value; }
136
146 void setChecker(std::function<void(TclObject&)> checkFunc_) {
147 checkFunc = std::move(checkFunc_);
148 }
149
150 // BaseSetting
151 void setValue(const TclObject& newValue) final;
152 [[nodiscard]] std::string_view getDescription() const final;
153 [[nodiscard]] TclObject getDefaultValue() const final { return defaultValue; }
154 void setValueDirect(const TclObject& newValue) final;
155 void tabCompletion(std::vector<std::string>& tokens) const override;
156 [[nodiscard]] bool needLoadSave() const final;
157 void additionalInfo(TclObject& result) const override;
158 [[nodiscard]] bool needTransfer() const final;
159
160 // convenience functions
161 [[nodiscard]] CommandController& getCommandController() const { return commandController; }
162 [[nodiscard]] Interpreter& getInterpreter() const;
163
164protected:
165 Setting(CommandController& commandController,
166 std::string_view name, static_string_view description,
167 const TclObject& initialValue, Save save = Save::YES);
168 void init();
169 void notifyPropertyChange() const;
170
171private:
172 [[nodiscard]] GlobalCommandController& getGlobalCommandController() const;
173 void notify() const;
174
175private:
176 CommandController& commandController;
177 const static_string_view description;
178 std::function<void(TclObject&)> checkFunc;
179 TclObject value; // TODO can we share the underlying Tcl var storage?
180 const TclObject defaultValue;
181 const Save save;
182};
183
184} // namespace openmsx
185
186#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:146
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:135
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: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
Setting & operator=(Setting &&)=delete
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
Setting(Setting &&)=delete
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