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 
22 int EnumSettingBase::fromStringBase(std::string_view str) const
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 
32 std::string_view EnumSettingBase::toStringBase(int value) const
33 {
34  for (const auto& [name, val] : baseMap) {
35  if (val == value) {
36  return name;
37  }
38  }
39  UNREACHABLE; return {};
40 }
41 
42 std::vector<std::string_view> EnumSettingBase::getPossibleValues() const
43 {
44  return to_vector<std::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
openmsx::CommandException
Definition: CommandException.hh:9
openmsx::EnumSettingBase::tabCompletionBase
void tabCompletionBase(std::vector< std::string > &tokens) const
Definition: EnumSetting.cc:54
openmsx::EnumSettingBase::toStringBase
std::string_view toStringBase(int value) const
Definition: EnumSetting.cc:32
ranges::sort
void sort(RandomAccessRange &&range)
Definition: ranges.hh:35
TclObject.hh
ranges::lower_bound
auto lower_bound(ForwardRange &&range, const T &value)
Definition: ranges.hh:71
StringOp::casecmp
Definition: StringOp.hh:68
ranges.hh
openmsx::EnumSettingBase::additionalInfoBase
void additionalInfoBase(TclObject &result) const
Definition: EnumSetting.cc:47
openmsx::EnumSettingBase::EnumSettingBase
EnumSettingBase(BaseMap &&m)
Definition: EnumSetting.cc:16
openmsx::EnumSettingBase::getPossibleValues
std::vector< std::string_view > getPossibleValues() const
Definition: EnumSetting.cc:42
openmsx::TclObject::addListElements
void addListElements(ITER first, ITER last)
Definition: TclObject.hh:122
openmsx::Completer::completeString
static void completeString(std::vector< std::string > &tokens, ITER begin, ITER end, bool caseSensitive=true)
Definition: Completer.hh:125
openmsx::EnumSettingBase::fromStringBase
int fromStringBase(std::string_view str) const
Definition: EnumSetting.cc:22
UNREACHABLE
#define UNREACHABLE
Definition: unreachable.hh:38
openmsx::TclObject::addListElement
void addListElement(T t)
Definition: TclObject.hh:121
view::keys
auto keys(Map &&map)
Definition: view.hh:311
view.hh
EnumSetting.hh
openmsx::Comp
CmpTupleElement< 0, StringOp::caseless > Comp
Definition: EnumSetting.cc:14
CmpTupleElement
Definition: stl.hh:29
StringOp.hh
stringsp.hh
stl.hh
openmsx::TclObject
Definition: TclObject.hh:22
openmsx::EnumSettingBase::BaseMap
std::vector< std::pair< std::string, int > > BaseMap
Definition: EnumSetting.hh:18
unreachable.hh
CommandException.hh
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
Completer.hh