openMSX
EnumSetting.hh
Go to the documentation of this file.
1#ifndef ENUMSETTING_HH
2#define ENUMSETTING_HH
3
4#include "Setting.hh"
5#include "view.hh"
6#include <concepts>
7#include <iterator>
8#include <utility>
9#include <vector>
10
11namespace openmsx {
12
13class TclObject;
14
15// non-templatized base class
17{
18public:
19 struct MapEntry {
20 template<typename Enum>
21 MapEntry(std::string_view name_, Enum value_)
22 : name(name_), value(static_cast<int>(value_)) {}
23
24 std::string name; // cannot be string_view because of the 'default_machine' setting
25 int value;
26 };
27 using Map = std::vector<MapEntry>;
28 [[nodiscard]] const auto& getMap() const { return baseMap; }
29
30protected:
31
32 explicit EnumSettingBase(Map&& m);
33
34 [[nodiscard]] int fromStringBase(std::string_view str) const;
35 [[nodiscard]] std::string_view toStringBase(int value) const;
36
37 [[nodiscard]] auto getPossibleValues() const {
38 return view::transform(baseMap,
39 [](const auto& e) -> std::string_view { return e.name; });
40 }
41
42 void additionalInfoBase(TclObject& result) const;
43 void tabCompletionBase(std::vector<std::string>& tokens) const;
44
45private:
46 Map baseMap;
47};
48
49template<typename T>
50concept EnumSettingValue = requires(T t, int i) {
51 static_cast<T>(i);
52 static_cast<int>(t);
53};
54
55template<EnumSettingValue T> class EnumSetting final : public EnumSettingBase, public Setting
56{
57public:
59
60 EnumSetting(CommandController& commandController, std::string_view name,
61 static_string_view description, T initialValue,
62 Map&& map_, SaveSetting save = SAVE);
63
64 [[nodiscard]] std::string_view getTypeString() const override;
65 void additionalInfo(TclObject& result) const override;
66 void tabCompletion(std::vector<std::string>& tokens) const override;
67
68 [[nodiscard]] T getEnum() const noexcept;
69 void setEnum(T e);
70 [[nodiscard]] std::string_view getString() const;
71
72private:
73 std::string_view toString(T e) const;
74};
75
76
77//-------------
78
79
80template<EnumSettingValue T>
82 CommandController& commandController_, std::string_view name,
83 static_string_view description_, T initialValue,
84 Map&& map, SaveSetting save_)
85 : EnumSettingBase(std::move(map))
86 , Setting(commandController_, name, description_,
87 TclObject(EnumSettingBase::toStringBase(static_cast<int>(initialValue))), save_)
88{
89 setChecker([this](TclObject& newValue) {
90 (void)fromStringBase(newValue.getString()); // may throw
91 });
92 init();
93}
94
95template<EnumSettingValue T>
96std::string_view EnumSetting<T>::getTypeString() const
97{
98 return "enumeration";
99}
100
101template<EnumSettingValue T>
103{
104 additionalInfoBase(result);
105}
106
107template<EnumSettingValue T>
108void EnumSetting<T>::tabCompletion(std::vector<std::string>& tokens) const
109{
110 tabCompletionBase(tokens);
111}
112
113template<EnumSettingValue T>
114T EnumSetting<T>::getEnum() const noexcept
115{
116 return static_cast<T>(fromStringBase(getValue().getString()));
117}
118template<> inline bool EnumSetting<bool>::getEnum() const noexcept
119{
120 // _exactly_ the same functionality as above, but suppress VS warning
121 return fromStringBase(getValue().getString()) != 0;
122}
123
124template<EnumSettingValue T>
126{
127 setValue(TclObject(toString(e)));
128}
129
130template<EnumSettingValue T>
131std::string_view EnumSetting<T>::getString() const
132{
133 return getValue().getString();
134}
135
136template<EnumSettingValue T>
137std::string_view EnumSetting<T>::toString(T e) const
138{
139 return toStringBase(static_cast<int>(e));
140}
141
142} // namespace openmsx
143
144#endif
TclObject t
std::vector< MapEntry > Map
void tabCompletionBase(std::vector< std::string > &tokens) const
void additionalInfoBase(TclObject &result) const
std::string_view toStringBase(int value) const
int fromStringBase(std::string_view str) const
auto getPossibleValues() const
const auto & getMap() const
EnumSettingBase::Map Map
void additionalInfo(TclObject &result) const override
Helper method for info().
std::string_view getTypeString() const override
Returns a string describing the setting type (integer, string, ..) Could be used in a GUI to pick an ...
std::string_view getString() const
void tabCompletion(std::vector< std::string > &tokens) const override
Complete a partly typed value.
EnumSetting(CommandController &commandController, std::string_view name, static_string_view description, T initialValue, Map &&map_, SaveSetting save=SAVE)
T getEnum() const noexcept
void setChecker(std::function< void(TclObject &)> checkFunc_)
Set value-check-callback.
Definition Setting.hh:145
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
zstring_view getString() const
Definition TclObject.cc:142
static_string_view
This file implemented 3 utility functions:
Definition Autofire.cc:9
STL namespace.
constexpr auto transform(Range &&range, UnaryOp op)
Definition view.hh:520
MapEntry(std::string_view name_, Enum value_)