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