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