openMSX
EnumSetting.cc
Go to the documentation of this file.
1 #include "EnumSetting.hh"
2 #include "TclObject.hh"
3 #include "Completer.hh"
4 #include "CommandException.hh"
5 #include "StringOp.hh"
6 #include "ranges.hh"
7 #include "stl.hh"
8 #include "stringsp.hh"
9 #include "view.hh"
10 #include "unreachable.hh"
11 
12 namespace openmsx {
13 
15 
17  : baseMap(std::move(map))
18 {
19  ranges::sort(baseMap, Comp());
20 }
21 
23 {
24  auto it = ranges::lower_bound(baseMap, str, Comp());
26  if ((it == end(baseMap)) || !cmp(it->first, str)) {
27  throw CommandException("not a valid value: ", str);
28  }
29  return it->second;
30 }
31 
33 {
34  for (auto& p : baseMap) {
35  if (p.second == value) {
36  return p.first;
37  }
38  }
39  UNREACHABLE; return {};
40 }
41 
42 std::vector<string_view> EnumSettingBase::getPossibleValues() const
43 {
44  return to_vector<string_view>(view::keys(baseMap));
45 }
46 
48 {
49  TclObject valueList;
51  result.addListElement(valueList);
52 }
53 
54 void EnumSettingBase::tabCompletionBase(std::vector<std::string>& tokens) const
55 {
57  false); // case insensitive
58 }
59 
60 } // namespace openmsx
std::vector< std::pair< std::string, int > > BaseMap
Definition: EnumSetting.hh:18
std::vector< string_view > getPossibleValues() const
Definition: EnumSetting.cc:42
EnumSettingBase(BaseMap &&m)
Definition: EnumSetting.cc:16
STL namespace.
auto keys(Map &&map)
Definition: view.hh:317
void sort(RandomAccessRange &&range)
Definition: ranges.hh:35
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
string_view toStringBase(int value) const
Definition: EnumSetting.cc:32
void tabCompletionBase(std::vector< std::string > &tokens) const
Definition: EnumSetting.cc:54
static void completeString(std::vector< std::string > &tokens, ITER begin, ITER end, bool caseSensitive=true)
Definition: Completer.hh:124
CmpTupleElement< 0, StringOp::caseless > Comp
Definition: EnumSetting.cc:14
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
void addListElement(T t)
Definition: TclObject.hh:121
auto lower_bound(ForwardRange &&range, const T &value)
Definition: ranges.hh:71
int fromStringBase(string_view str) const
Definition: EnumSetting.cc:22
void additionalInfoBase(TclObject &result) const
Definition: EnumSetting.cc:47
void addListElements(ITER first, ITER last)
Definition: TclObject.hh:122
auto end(const string_view &x)
Definition: string_view.hh:152
#define UNREACHABLE
Definition: unreachable.hh:38