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